首页 技术 正文
技术 2022年11月11日
0 收藏 534 点赞 3,753 浏览 3430 个字

前两篇分别讲解了创建菜单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 写个小例子,给大家看一下

代码:

 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();
}
}

效果图:

Unity3D编辑器扩展(三)——使用GUI绘制窗口

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298