最近由于闲来无事,无意中看到vectrosity这个画线插件,就想研究一下,下载下来之后发现导入到unity里之后出现错误,由于自己是蠢新所以改了一通也没改对,所以就放弃了,大家需要插件的话待会我会上下载链接,自己可以试试,我的版本是5.2.2的,在找到http://blog.csdn.net/awnuxcvbn/article/details/17279837这个利用GL画线的,但是感觉效果不好,所以自己修改了一下,改成了画线框的,其实就是确定了四个点而已,而且修改成了与鼠标同步画线的效果,大家有兴趣的可以看看,共同进步···········
using UnityEngine; using System.Collections; public class Line : MonoBehaviour { public Material mat; public Color color = Color.red; public Vector3 pos1; public Vector3 pos2; void Start() { mat.color = color; } void Update() { if (Input.GetMouseButtonDown(0)) { pos1 = Input.mousePosition; } if (Input.GetMouseButton(0)) { pos2 = Input.mousePosition; } } void OnPostRender() { GL.PushMatrix(); mat.SetPass(0); GL.LoadOrtho(); GL.Begin(GL.LINES); GL.Color(color); GL.Vertex3(pos1.x / Screen.width, pos1.y / Screen.height, 0); GL.Vertex3(pos2.x / Screen.width, pos1.y / Screen.height, 0); GL.Vertex3(pos2.x / Screen.width, pos1.y / Screen.height, 0); GL.Vertex3(pos2.x / Screen.width, pos2.y / Screen.height, 0); GL.Vertex3(pos2.x / Screen.width, pos2.y / Screen.height, 0); GL.Vertex3(pos1.x / Screen.width, pos2.y / Screen.height, 0); GL.Vertex3(pos1.x / Screen.width, pos2.y / Screen.height, 0); GL.Vertex3(pos1.x / Screen.width, pos1.y / Screen.height, 0); GL.End(); GL.PopMatrix(); } }