把鼠标的移动轨迹以曲线的方式显示出来。
示例代码如下:
- public Material material;//材质,必须要有
- private List<Vector3> lineInfo;//鼠标坐标的集合
- void Start ()
- {
- lineInfo = new List<Vector3> ();//初始化集合
- }
- // Update is called once per frame
- void Update ()
- {
- // if (Input.GetMouseButton (0)) {
- //
- // lineInfo.Add (Input.mousePosition);
- // }
- lineInfo.Add (Input.mousePosition);//将鼠标坐标加入到集合当中
- }
- void OnPostRender ()
- {
- // if (Input.GetMouseButton (0)) {
- if (!material) {
- Debug.LogError ("请给材质赋值");
- return;
- }
- GL.PushMatrix ();
- material.SetPass (0);
- //material.color = Color.white;
- GL.LoadOrtho ();//绘制对象显示在平面上
- GL.Begin (GL.LINES);//开始划线
- GL.Color (Color.red);//线的颜色,我这边颜色是不会改变的,还没找出问题,希望有人能帮我搞定这个颜色不变得问题。
- int size = lineInfo.Count;
- for (int i = 0; i < size - 1; i++) {
- Vector3 start = lineInfo [i];
- Vector3 end = lineInfo [i + 1];
- DrawLine (start.x, start.y, end.x, end.y);
- }
- GL.End ();
- GL.PopMatrix ();
- // }
- }
- void DrawLine (float x1, float y1, float x2, float y2)
- {
- GL.Vertex (new Vector3 (x1 / Screen.width, y1 / Screen.height, 0));
- GL.Vertex (new Vector3 (x2 / Screen.width, y2 / Screen.height, 0));
- }
上面颜色不会改变,希望看到的小伙伴能帮忙搞定。 这个脚本要挂载在摄像机上。