unity視圖擴展的一些用法

最近總結了unity視圖編輯的一些用法:

一.劃線

     1.有任何腳本中添加public void OnDrawGizmos ()這個方法,然後在方法中Gizmos.DrawLine調用這個方法可以在Scene視圖中劃線條或是其它圖形

     2.在運行時Game視圖,調用_LineRenderer.SetVertexCount(_Nav.path.corners.Length)這個方法也可以劃線條

二.有關GUI相關知識

    1.在任何腳本中添加這個private void OnGUI()方法,在這個方法中可以調用GUI相關的組件,可以在Game視圖看到相關的組件,  例如

      GUIStyle style = new GUIStyle

        {

            border = new RectOffset(10, 10, 10, 10),

            fontSize = 50,

            fontStyle = FontStyle.BoldAndItalic,

        };

        // normal:Rendering settings for when the component is displayed normally.

        style.normal.textColor = new Color(200/255f, 180/255f, 150/255f);    // 需要除以255,因爲範圍是0-1

        GUI.Label(new Rect(100, 100, 200, 80), "aaa", style);

        GUI.Label(new Rect(Screen.width - 100, Screen.height - 100, 200, 80),

            "<color=#00ff00><size=30>"+"aaa"+"</size></color>", style);

   2.繼承Editor類,添加標籤([CustomEditor(typeof(TileMousePosition))],[ExecuteInEditMode]),在類中重        寫 public override void OnInspectorGUI() 方法可以自定義insprector中的組件:test.mRectValue = EditorGUILayout.RectField("窗口座標", test.mRectValue),方法 void OnSceneGUI()可以在Scene視圖中繪製GUI組件:

         Handles.BeginGUI();

        GUIStyle style = new GUIStyle(); 

        style.normal.textColor = Color.red; 

        //規定GUI顯示區域

        GUILayout.BeginArea(new Rect(0, 0, 500, 100));

        //GUI繪製文本框

        GUILayout.Label("世界座標:" + "X: " + worldPosition.x + " Y: " + -worldPosition.y*2 + " Z: " + worldPosition.z,style);  

        GUILayout.Label("格子座標:" + "X: " + cellPosition.x + " Y: " + -cellPosition.y + " Z: " + cellPosition.z,style);   

 

        GUILayout.EndArea();

    

        Handles.EndGUI();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章