Unity3D編輯器擴展(三)——使用GUI繪制窗口


前兩篇分別講解了創建菜單https://www.cnblogs.com/xiaoyulong/p/10115053.html和創建窗口https://www.cnblogs.com/xiaoyulong/p/10120565.html

這一篇我們講解使用 GUI 來繪制我們的窗口,使窗口內容更豐富、美觀

繪制窗口我們一般會使用下面四個類:GUI、GUILayout、EditorGUI、EditorGUILayout。

這四個類大同小異,基本上沒什么差別,和我們經常使用的 UGUINGUI 相比,UGUI 和 NGUI 有圖形化的操作,而這四個類是純代碼操作。

GUI、EditorGUI  GUILayout、EditorGUILayout 的區別是:前者是固定布局,布局需要我們寫代碼控制,后者是自動布局。

GUI、GUILayout EditorGUI、EditorGUILayout 的區別是:前者更多的用在平常調試中,后者在編輯器中使用。前者也可以使用在編輯器中。

其他的也不多說了,全是一些 API 的使用,下面我就用 EditorGUI 和 EditorGUILayout 寫個小例子,給大家看一下

代碼:

 1 using UnityEngine;
 2 using UnityEditor;
 3 
 4 public class EditorGUILayoutTest : EditorWindow
 5 {
 6     private Vector3 startPoint;//起點
 7     private Vector3 endPoint;//終點
 8     private float distance = 0f;//起點到終點的距離
 9 
10     private bool val;
11     private Color color = Color.red;
12 
13     private AnimationCurve curveX = AnimationCurve.Linear(0, 0, 1, 0);
14     private AnimationCurve curveY = AnimationCurve.Linear(0, 0, 1, 1);
15     private AnimationCurve curveZ = AnimationCurve.Linear(0, 0, 1, 0);
16 
17     private int sliderValue = 1;
18     private bool showClose = true;
19     private bool showToggleLeft = true;
20     private string textfieldtest = "textfieldtest";
21     private string password = "pwd123456";
22 
23     private float minVal = -10.0f;
24     private float minLimit = -20.0f;
25     private float maxVal = 10.0f;
26     private float maxLimit = 20.0f;
27 
28     [MenuItem("MyWindow/WindowTest")]
29     private static void Init()
30     {
31         EditorGUILayoutTest window = (EditorGUILayoutTest)EditorWindow.GetWindow(typeof(EditorGUILayoutTest));
32         window.Show();
33     }
34 
35     private void OnGUI()
36     {
37         //Vector3類型數據的顯示
38         startPoint = EditorGUILayout.Vector3Field("Start Point:", startPoint);
39         endPoint = EditorGUILayout.Vector3Field("End Point:", endPoint);
40         //只讀的標簽
41         EditorGUILayout.LabelField("Distance:", Vector3.Distance(startPoint, endPoint).ToString("f2"));
42         //勾選框
43         val = EditorGUILayout.Toggle("Can Jump", val);
44         //是否開啟禁用功能。false表示禁用關閉,true表示開啟禁用--灰色狀態
45         EditorGUI.BeginDisabledGroup(val);//
46         //float類型文本Text
47         EditorGUILayout.FloatField("跳躍高度:", 100.0f);
48         //結束禁用
49         EditorGUI.EndDisabledGroup();
50         EditorGUILayout.FloatField("跳躍頻率:", 1.85f);
51         //Bounds輸入框
52         EditorGUILayout.BoundsField("BoundsField", new Bounds(new Vector3(50, 50, 50), new Vector3(150, 150, 150)));
53         //顏色選擇框
54         color = EditorGUILayout.ColorField("顏色:", color);
55         //按鈕
56         if (GUILayout.Button("Close"))
57         {
58             this.Close();
59         }
60         //帶有陰影的Label
61         EditorGUI.DropShadowLabel(new Rect(0, 250, position.width, 20), "帶有陰影的Label");
62         //動畫曲線
63         curveX = EditorGUI.CurveField(new Rect(3, 320, position.width - 6, 50), "Animation on X", curveX);
64         curveY = EditorGUI.CurveField(new Rect(3, 370, position.width - 6, 50), "Animation on Y", curveY);
65         curveZ = EditorGUI.CurveField(new Rect(3, 420, position.width - 6, 50), "Animation on Z", curveZ);
66         //數字輸入
67         EditorGUI.DelayedDoubleField(new Rect(3, 480, position.width - 6, 20), "DelayedDoubleField1", 25.0);
68         EditorGUI.DelayedFloatField(new Rect(3, 500, position.width - 6, 20), "DelayedFloatField", 25.0f);
69         EditorGUI.DelayedIntField(new Rect(3, 520, position.width - 6, 20), "DelayedIntField", 25);
70         EditorGUI.DelayedTextField(new Rect(3, 540, position.width - 6, 20), "DelayedTextField");
71         //繪畫矩形
72         EditorGUI.DrawRect(new Rect(3, 570, position.width - 60, 20), Color.green);
73         //滑動條。輸入框(值不能滑動): 注意左邊必須要有值接收這個值,否則不能滑動
74         sliderValue = EditorGUI.IntSlider(new Rect(3, 600, position.width - 60, 30), sliderValue, 0, 100);
75         //幫助盒子信息框
76         EditorGUI.HelpBox(new Rect(3, 645, position.width - 60, 40), "HelpBox幫助盒子", MessageType.Info);
77         //Toggle 開關
78         showClose = EditorGUI.Toggle(new Rect(3, 685, position.width - 60, 20), "Toggle", showClose);
79         showToggleLeft = EditorGUI.ToggleLeft(new Rect(3, 710, position.width - 60, 20), "ToggleLeft", showToggleLeft);
80         textfieldtest = EditorGUI.TextField(new Rect(3, 735, position.width - 60, 20), "TextField", textfieldtest);
81         password = EditorGUI.PasswordField(new Rect(3, 760, position.width - 60, 20), "密碼框:", password);
82         //最大值和最小值滑塊
83         EditorGUI.MinMaxSlider(new Rect(3, 790, position.width - 60, 20), ref minVal, ref maxVal, minLimit, maxLimit);
84     }
85 
86     private void OnInspectorUpdate()
87     {
88         this.Repaint();
89     }
90 }

效果圖:

 


免責聲明!

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



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