Unity使用GL画线
脚本需挂在相机上,如果你的脚本,编辑器报错了,Matrix stack full depth reached,加上这个方法试试GL.LoadPixelMatrix();
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class GridLine : MonoBehaviour { 6 7 public static bool isShowGridLine=false; 8 Material lineMaterial; 9 private void Awake() 10 { 11 if (!lineMaterial) 12 { 13 lineMaterial = new Material(Shader.Find("Particles/Alpha Blended")); 14 lineMaterial.hideFlags = HideFlags.HideAndDontSave; 15 lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave; 16 } 17 } 18 void OnPostRender() 19 { 20 if (isShowGridLine) { 21 GL.PushMatrix(); 22 lineMaterial.SetPass (0); 23 //如果报错的话,将这句话取消注释后,再试试 24 // GL.LoadPixelMatrix (); 25 GL.LoadOrtho (); 26 GL.Begin (GL.LINES); 27 for (int i = 0; i < 10; i++) { 28 GL.Color (Color.green); 29 //横线 30 GL.Vertex3 (0,i/10f,0); 31 GL.Vertex3 (1,i/10f,0); 32 //竖线 33 GL.Vertex3 (i/10f,0,0); 34 GL.Vertex3 (i/10f,1,0); 35 } 36 // //斜线 37 // GL.Vertex3(0,0,0); 38 // GL.Vertex3 (1,1,0); 39 40 GL.End (); 41 GL.PopMatrix (); 42 } 43 44 } 45 void Update() 46 { 47 if (Input.GetMouseButtonDown(1)) { 48 isShowGridLine = true; 49 } 50 if (Input.GetKeyDown(KeyCode.Escape)) { 51 isShowGridLine = false; 52 } 53 54 } 55 }
![](https://images2015.cnblogs.com/blog/1030317/201702/1030317-20170227182540579-890346871.png)
博客园Jason_c微信打赏码
如果本篇文档对你有帮助,打赏Jason_c根华子吧,他的私房钱被老婆没收了,呜呜!
![](https://files-cdn.cnblogs.com/files/Jason-c/%E5%BE%AE%E4%BF%A1%E6%89%93%E8%B5%8F%E7%A0%81.bmp)