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