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://www.cnblogs.com/Jason-c/p/6474843.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值