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 }


 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM