Unity3D中使用GL畫拉選框(繪制多選框)


問題分析:

需要根據鼠標事件,摁下鼠標開始繪制選擇框,抬起鼠標結束繪制。

實現思路:

該需求是屏幕畫線,Unity內置了GL類  封裝了OpenGL,可以通過GL類來實現一些簡單的畫圖操作,這里也是使用GL實現。

分析:

代碼中有這樣一個回調是屬於屏幕渲染的,需要在API里了解一下public void OnRenderObject(),以及OnGUI都可以實現,了解下Unity的生命周期執行順序中,屏幕渲染會在GameLogic后會執行其中有幾個屏幕渲染回調API來了解一下:

 

 

注意:

GL.Push  和  GL.Pop  之間寫GL代碼

GL.Begin 和 GL.End  之間寫畫線邏輯Begin和End 之間的兩個Vector3表示起點和終點

 代碼:

  1     /// <summary>
  2     /// 繪制選擇框
  3     /// </summary>
  4     class Selectbox : MonoBehaviour
  5     {
  6         #region Fields & Attribute
  7 
  8         //畫筆顏色
  9         private Color brushColor=Color.white;
 10 
 11         //畫線的材質
 12         private Material drawMaterial;
 13 
 14         //開始繪制標志
 15         private bool isStartDraw=false;
 16 
 17         //開始和結束繪制點
 18         private Vector3 mouseStartPos, mouseEndPos;
 19 
 20         //設置選擇區域的Color
 21         private Color selectionAreaColor = Color.green;
 22 
 23         //獲取繪制狀態(開始繪制標志)
 24         public bool IsStartDraw { get => isStartDraw; set => isStartDraw = value; }
 25 
 26         //繪制開始坐標
 27         public Vector3 MouseStartPos { get => mouseStartPos; set => mouseStartPos = value; }
 28 
 29         //繪制結束坐標
 30         public Vector3 MouseEndPos { get => mouseEndPos; set => mouseEndPos = value; }
 31 
 32         //設置畫筆顏色
 33         public Color BrushColor { get => brushColor; set => brushColor = value; }
 34 
 35         //設置選擇區域的Color
 36         public Color SelectionAreaColor { get => selectionAreaColor; set => selectionAreaColor = value; }
 37  
 38         #endregion
 39 
 40         #region Public Methods
 41 
 42         /// <summary>
 43         /// 繪制選擇框構造函數
 44         /// </summary>
 45         public Selectbox()
 46         {
 47 
 48         }
 49 
 50         /// <summary>
 51         /// 初始化
 52         /// </summary>
 53         public void Awake()
 54         {
 55             drawMaterial = new Material(Shader.Find("UI/Default"));
 56             this.drawMaterial.hideFlags = HideFlags.HideAndDontSave;
 57             this.drawMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
 58         }
 59 
 60         #endregion
 61 
 62         #region Private Methods
 63 
 64         /// <summary>
 65         /// 繪制邏輯
 66         /// </summary>
 67         private void OnGUI()
 68         {
 69             if (IsStartDraw)
 70             {
//材質通道,0為默認。
71 drawMaterial.SetPass(0); 72 GL.LoadOrtho(); 73 //設置用屏幕坐標繪圖 74 GL.LoadPixelMatrix(); 75 DrawRect(); 76 DrawRectLine(); 77 } 78 } 79 80 /// <summary> 81 /// 繪制框選區 82 /// </summary> 83 private void DrawRect() 84 { 85 GL.Begin(GL.QUADS); 86 //設置顏色和透明度 87 GL.Color(selectionAreaColor); 88 if ((MouseStartPos.x > MouseEndPos.x && MouseStartPos.y > MouseEndPos.y) || (MouseStartPos.x < MouseEndPos.x && MouseStartPos.y < MouseEndPos.y)) 89 { 90 GL.Vertex3(MouseStartPos.x, MouseStartPos.y, 0); 91 GL.Vertex3(MouseStartPos.x, MouseEndPos.y, 0); 92 GL.Vertex3(MouseEndPos.x, MouseEndPos.y, 0); 93 GL.Vertex3(MouseEndPos.x, MouseStartPos.y, 0); 94 95 } 96 else 97 { 98 GL.Vertex3(MouseStartPos.x, MouseStartPos.y, 0); 99 GL.Vertex3(MouseEndPos.x, MouseStartPos.y, 0); 100 GL.Vertex3(MouseEndPos.x, MouseEndPos.y, 0); 101 GL.Vertex3(MouseStartPos.x, MouseEndPos.y, 0); 102 } 103 GL.End(); 104 } 105 106 /// <summary> 107 /// 繪制框選邊框 108 /// </summary> 109 private void DrawRectLine() 110 { 111 GL.Begin(GL.LINES); 112 //設置方框的邊框顏色 邊框不透明 113 GL.Color(BrushColor); 114 GL.Vertex3(MouseStartPos.x, MouseStartPos.y, 0); 115 GL.Vertex3(MouseEndPos.x, MouseStartPos.y, 0); 116 GL.Vertex3(MouseEndPos.x, MouseStartPos.y, 0); 117 GL.Vertex3(MouseEndPos.x, MouseEndPos.y, 0); 118 GL.Vertex3(MouseEndPos.x, MouseEndPos.y, 0); 119 GL.Vertex3(MouseStartPos.x, MouseEndPos.y, 0); 120 GL.Vertex3(MouseStartPos.x, MouseEndPos.y, 0); 121 GL.Vertex3(MouseStartPos.x, MouseStartPos.y, 0); 122 GL.End(); 123 } 124 125 #endregion 126 }

這個地方,在畫面時,發現只有兩個象限是正常的,其他兩個象限都畫不出來,原來是一三和二四象限中構建面(點)的順序不同,相反。還有shader選默認的就好,有顏色即可。

 

 即這段。

到這完成了就,歡迎指正。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM