前两篇分别讲解了创建菜单https://www.cnblogs.com/xiaoyulong/p/10115053.html和创建窗口https://www.cnblogs.com/xiaoyulong/p/10120565.html
这一篇我们讲解使用 GUI 来绘制我们的窗口,使窗口内容更丰富、美观
绘制窗口我们一般会使用下面四个类:GUI、GUILayout、EditorGUI、EditorGUILayout。
这四个类大同小异,基本上没什么差别,和我们经常使用的 UGUI 和 NGUI 相比,UGUI 和 NGUI 有图形化的操作,而这四个类是纯代码操作。
GUI、EditorGUI 和 GUILayout、EditorGUILayout 的区别是:前者是固定布局,布局需要我们写代码控制,后者是自动布局。
GUI、GUILayout 和 EditorGUI、EditorGUILayout 的区别是:前者更多的用在平常调试中,后者在编辑器中使用。前者也可以使用在编辑器中。
其他的也不多说了,全是一些 API 的使用,下面我就用 EditorGUI 和 EditorGUILayout 写个小例子,给大家看一下
代码:
using UnityEngine;
using UnityEditor; public class EditorGUILayoutTest : EditorWindow
{
private Vector3 startPoint;//起点
private Vector3 endPoint;//终点
private float distance = 0f;//起点到终点的距离 private bool val;
private Color color = Color.red; private AnimationCurve curveX = AnimationCurve.Linear(, , , );
private AnimationCurve curveY = AnimationCurve.Linear(, , , );
private AnimationCurve curveZ = AnimationCurve.Linear(, , , ); private int sliderValue = ;
private bool showClose = true;
private bool showToggleLeft = true;
private string textfieldtest = "textfieldtest";
private string password = "pwd123456"; private float minVal = -10.0f;
private float minLimit = -20.0f;
private float maxVal = 10.0f;
private float maxLimit = 20.0f; [MenuItem("MyWindow/WindowTest")]
private static void Init()
{
EditorGUILayoutTest window = (EditorGUILayoutTest)EditorWindow.GetWindow(typeof(EditorGUILayoutTest));
window.Show();
} private void OnGUI()
{
//Vector3类型数据的显示
startPoint = EditorGUILayout.Vector3Field("Start Point:", startPoint);
endPoint = EditorGUILayout.Vector3Field("End Point:", endPoint);
//只读的标签
EditorGUILayout.LabelField("Distance:", Vector3.Distance(startPoint, endPoint).ToString("f2"));
//勾选框
val = EditorGUILayout.Toggle("Can Jump", val);
//是否开启禁用功能。false表示禁用关闭,true表示开启禁用--灰色状态
EditorGUI.BeginDisabledGroup(val);//
//float类型文本Text
EditorGUILayout.FloatField("跳跃高度:", 100.0f);
//结束禁用
EditorGUI.EndDisabledGroup();
EditorGUILayout.FloatField("跳跃频率:", 1.85f);
//Bounds输入框
EditorGUILayout.BoundsField("BoundsField", new Bounds(new Vector3(, , ), new Vector3(, , )));
//颜色选择框
color = EditorGUILayout.ColorField("颜色:", color);
//按钮
if (GUILayout.Button("Close"))
{
this.Close();
}
//带有阴影的Label
EditorGUI.DropShadowLabel(new Rect(, , position.width, ), "带有阴影的Label");
//动画曲线
curveX = EditorGUI.CurveField(new Rect(, , position.width - , ), "Animation on X", curveX);
curveY = EditorGUI.CurveField(new Rect(, , position.width - , ), "Animation on Y", curveY);
curveZ = EditorGUI.CurveField(new Rect(, , position.width - , ), "Animation on Z", curveZ);
//数字输入
EditorGUI.DelayedDoubleField(new Rect(, , position.width - , ), "DelayedDoubleField1", 25.0);
EditorGUI.DelayedFloatField(new Rect(, , position.width - , ), "DelayedFloatField", 25.0f);
EditorGUI.DelayedIntField(new Rect(, , position.width - , ), "DelayedIntField", );
EditorGUI.DelayedTextField(new Rect(, , position.width - , ), "DelayedTextField");
//绘画矩形
EditorGUI.DrawRect(new Rect(, , position.width - , ), Color.green);
//滑动条。输入框(值不能滑动): 注意左边必须要有值接收这个值,否则不能滑动
sliderValue = EditorGUI.IntSlider(new Rect(, , position.width - , ), sliderValue, , );
//帮助盒子信息框
EditorGUI.HelpBox(new Rect(, , position.width - , ), "HelpBox帮助盒子", MessageType.Info);
//Toggle 开关
showClose = EditorGUI.Toggle(new Rect(, , position.width - , ), "Toggle", showClose);
showToggleLeft = EditorGUI.ToggleLeft(new Rect(, , position.width - , ), "ToggleLeft", showToggleLeft);
textfieldtest = EditorGUI.TextField(new Rect(, , position.width - , ), "TextField", textfieldtest);
password = EditorGUI.PasswordField(new Rect(, , position.width - , ), "密码框:", password);
//最大值和最小值滑块
EditorGUI.MinMaxSlider(new Rect(, , position.width - , ), ref minVal, ref maxVal, minLimit, maxLimit);
} private void OnInspectorUpdate()
{
this.Repaint();
}
}
效果图: