NRE的编程笔记

导航

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 }

效果图:

 

posted on 2018-12-17 15:28  NRE  阅读(6994)  评论(0编辑  收藏  举报