unity3d GL绘制线段

unity GL库的坐标左下角(0,0),右上角(1,1)

示例代码如下:

  1. public Material material;//材质,一定要有
  2.     void OnPostRender ()
  3.     {
  4.         if (!material) {
  5.             Debug.LogError ("请给材质赋值");
  6.             return;
  7.         }
  8.         material.SetPass (0);
  9.         GL.LoadOrtho ();//绘制对象显示在平面上
  10.         GL.Begin (GL.LINES);//画的是线段
  11.  
  12.         DrawLine (0, 0, 200, 200);
  13.      
  14.         GL.End ();
  15.     }
  16.     void DrawLine (float x1, float y1, float x2, float y2)
  17.     {
  18.         GL.Vertex (new Vector3 (x1 / Screen.width, y1 / Screen.height, 0));
  19.         GL.Vertex (new Vector3 (x2 / Screen.width, y2 / Screen.height, 0));
  20.     }

这个脚本一定要挂载在摄像机上。 

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/82190927