Unity3D中畫拉選框(繪製多選框)

問題分析:

須要根據鼠標事件,摁下鼠標開始繪製選擇框,擡起鼠標結束繪製。ide

實現思路:

該需求是屏幕畫線,Unity內置了GL類  封裝了OpenGL,能夠經過GL類來實現一些簡單的畫圖操做,這裏也是使用GL實現。函數

分析:

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

 

 

注意:spa

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

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

 代碼:

  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選默認的就好,有顏色便可。生命週期

 

 即這段。事件

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

相關文章
相關標籤/搜索