1.签名,改变不了GL画线的颜色 (shader改成 particle/additive)
using UnityEngine; using System.Collections; public class joint{ public Vector3 org; public Vector3 end; } public class example : MonoBehaviour { Event e; private Vector3 orgPos; private Vector3 endPos; private bool canDrawLines = false; ArrayList posAL; ArrayList temppos; public Material lineMaterial; void Start() { temppos=new ArrayList(); posAL=new ArrayList(); } void Update() { if(Input.GetMouseButton(0)) { canDrawLines = true; } if(e.type!=null &canDrawLines) { if(e.type == EventType.MouseDown) { orgPos=Input.mousePosition; endPos=Input.mousePosition; } if(e.type==EventType.MouseDrag) { endPos=Input.mousePosition; //鼠标位置信息存入数组 temppos.Add(Input.mousePosition); GLDrawLine(orgPos,endPos); orgPos=Input.mousePosition; print(temppos.Count); } if(e.type==EventType.MouseUp) { // orgPos=Input.mousePosition; endPos=Input.mousePosition; } } } void GLDrawLine(Vector3 beg ,Vector3 end ) { if(!canDrawLines) return; GL.PushMatrix (); GL.LoadOrtho (); beg.x=beg.x/Screen.width; end.x=end.x/Screen.width; beg.y=beg.y/Screen.height; end.y=end.y/Screen.height; joint tmpJoint = new joint(); tmpJoint.org=beg; tmpJoint.end=end; posAL.Add(tmpJoint); lineMaterial.SetPass( 0 ); GL.Begin( GL.LINES ); GL.Color( new Color(1,1,1,0.5f) ); for(int i= 0;i<posAL.Count;i++) { joint tj =(joint)posAL[i]; Vector3 tmpBeg = tj.org; Vector3 tmpEnd=tj.end; GL.Vertex3( tmpBeg.x,tmpBeg.y,tmpBeg.z ); GL.Vertex3( tmpEnd.x,tmpEnd.y,tmpEnd.z ); } GL.End(); GL.PopMatrix (); } void OnGUI() { e = Event.current; if(GUI.Button(new Rect(150,0,100,50),"End Lines")) { ClearLines(); } } void ClearLines() { canDrawLines = false; posAL.Clear(); } void OnPostRender() { GLDrawLine(orgPos,endPos); } }
2.这个方法有点解锁的感觉
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { public Material mat; public Color color = Color.red; public Vector3 pos1; public Vector3 pos2; public bool isReady = false; void Start() { mat.color = color; } void Update() { if (Input.GetMouseButtonDown(0)) { pos1 = Input.mousePosition; } if (Input.GetMouseButtonUp(0)) { pos2 = Input.mousePosition; isReady = true; } } void OnPostRender() { if (isReady) { GL.PushMatrix(); mat.SetPass(0); GL.LoadOrtho(); GL.Begin(GL.LINES); GL.Color(color); GL.Vertex3(pos1.x / Screen.width, pos1.y / Screen.height, pos1.z); GL.Vertex3(pos2.x / Screen.width, pos2.y / Screen.height, pos2.z); GL.End(); GL.PopMatrix(); } } }3. lineRender 画线
增加一个 LineRenderer 组建,shader类型改成 particle/additive类型,不然画线会没有颜色
新建一个脚本
代码如下:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { private GameObject clone; private LineRenderer line; int i; //带有LineRender物体 public GameObject target; void Start() { } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { //实例化对象 clone = (GameObject)Instantiate(target, target.transform.position, Quaternion.identity); //获得该物体上的LineRender组件 line = clone.GetComponent<LineRenderer>(); //设置起始和结束的颜色 line.SetColors(Color.red, Color.blue); //设置起始和结束的宽度 line.SetWidth(0.4f, 0.35f); //计数 i = 0; } if (Input.GetMouseButton(0)) { //每一帧检测,按下鼠标的时间越长,计数越多 i++; //设置顶点数 line.SetVertexCount(i); //设置顶点位置(顶点的索引,将鼠标点击的屏幕坐标转换为世界坐标) line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15))); } } }
效果图如下: