Unity3d LineRenderer畫線
來源:程序員人生 發(fā)布時(shí)間:2015-03-09 08:26:52 閱讀次數(shù):5779次
【狗刨學(xué)習(xí)網(wǎng)】
1、 畫多條線
畫多條線需要在場(chǎng)景中放置多個(gè)GameObject,由于1個(gè)GameObject只能添加1個(gè)LineRenderer Component。
2、 線段樣式
線段的樣式由LineRenderer組件的材質(zhì)控制
3、 坐標(biāo)系
LineRenderer使用了2種坐標(biāo)系:World和Location。在設(shè)置目標(biāo)點(diǎn)的時(shí)候需要注意你使用的坐標(biāo)系是哪種。
Location:
-
transform.LookAt(rh.point); //GameObject始終面對(duì)目標(biāo)點(diǎn)
-
lineRenderer = (LineRenderer)gameObject.GetComponent ("LineRenderer");
-
lineRenderer.useWorldSpace=false;
-
lineRenderer.SetWidth(0.1f,0.1f);
-
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
-
if(Physics.Raycast(ray,out rh)){
-
lineRenderer.SetPosition(0, Vector3.forward*rh.distance);//方向*距離,典型的Location坐標(biāo)系用法
-
}
復(fù)制代碼
World:
-
void Start () {
-
gameObject.SetActive(false);
-
lr = gameObject.GetComponent(typeof(LineRenderer)) as LineRenderer;
-
lr.SetWidth(0.1f,0.1f);
-
}
-
-
// Update is called once per frame
-
void Update () {
-
if(Input.GetMouseButtonUp(0)){
-
-
//Get click position
-
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
-
lr.SetVertexCount(lineSeg+1);
-
if(Physics.Raycast(ray,out rh)){
-
pointPos.Add(DrawLine(rh));
-
}
-
}
-
-
if(Input.GetMouseButtonUp(1)){
-
//destroy point
-
DestroyLine();
-
}
-
-
-
}
-
-
-
GameObject DrawLine(RaycastHit pointPos){
-
-
//Display point
-
GameObject gb_pointer = GameObject.Instantiate(pointer) as GameObject;
-
gb_pointer.transform.position =pointPos.point + (transform.position - pointPos.point) * 0.01f;
-
gb_pointer.transform.rotation = Quaternion.LookRotation (pointPos.normal, Camera.mainCamera.transform.up);
-
Vector3 laserpos = new Vector3();
-
laserpos.x= 90.0f;
-
laserpos.y= gb_pointer.transform.position.y;
-
laserpos.z= gb_pointer.transform.position.z;
-
gb_pointer.transform.eulerAngles = laserpos;
-
lr.SetPosition(lineSeg,pointPos.point); //設(shè)置目標(biāo)點(diǎn)的坐標(biāo),使用的是world坐標(biāo)系
-
lineSeg++;
-
return gb_pointer;
-
}
-
-
void DestroyLine(){
-
-
int arrayLength = pointPos.Count;
-
if(arrayLength > 0){
-
GameObject.Destroy(pointPos[arrayLength⑴]);
-
pointPos.RemoveAt(arrayLength⑴);
-
lr.SetVertexCount(--lineSeg);
-
}
-
}
復(fù)制代碼
聲明:此篇文檔時(shí)來自于【狗刨學(xué)習(xí)網(wǎng)】社區(qū),是網(wǎng)友自行發(fā)布的Unity3D學(xué)習(xí)文章,如果有甚么內(nèi)容侵犯了你的相干權(quán)益,請(qǐng)與官方溝通,我們會(huì)即時(shí)處理。
更多精彩內(nèi)容:www.gopedu.com
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)