Unity编辑器拓展(二)-GUI控件的使用

一、效果图

在这里插入图片描述

二、创建窗口

首先我们先用第一节介绍的MenuItem来创建窗口

  public class LearnWindow : EditorWindow
    {
   
        [MenuItem("Learn/Open LearnWindow %q")]
        public static void OpenWindow()
        {
   
            LearnWindow window = UnityEditor.EditorWindow.GetWindow<LearnWindow>();
        }
    }   

创建窗口

三、使用控件来绘制窗口

接下来我们介绍一些控制来绘制窗口

不可输入控件

GUILayout.Label 标签

GUILayout.Label("Label",EditorStyles.boldLabel);

效果如下
在这里插入图片描述

可输入控件

GUILayout系列

GUILayout.TextField 单行文本
GUILayout.TextArea 多行文本
        private string textFieldVal;
        private string textAreaVal;
        private void OnGUI()
        {
                                   
            //TextField输入框(最多只能输入一行,不管高度多高)
            textFieldVal = GUILayout.TextField(textFieldVal);
            //TextArea输入框(可以输入多行)
            textAreaVal = GUILayout.TextArea(textAreaVal,GUILayout.Height(50));
         }            

效果如下,上面的是TextField,下面的是TextArea
在这里插入图片描述

EditorGUILayout系列

上面GUILayout画出来就是一个纯输入框,我们一般需要在输入框加点标题做提示。这时候就需要用到EditorGUILayout系列控件;

EditorGUILayout.IntField() 输入整数
EditorGUILayout.FloatField() 输入浮点数
EditorGUILayout.TextField() 输入单行文本
EditorGUILayout.TextArea() 输入多行文本
        private int editorLabelInt;
        private float editorLabelFloat;
        private string editorLabelText;
        private string editorLabelArea;
        private void OnGUI()
        {
              
            //EditorGUILayout(既可以显示标题,又可以承接输入内容)
            editorLabelInt = EditorGUILayout.IntField("输入int:",editorLabelInt);
            editorLabelFloat = EditorGUILayout.FloatField("输入float:",editorLabelFloat);
            editorLabelText = EditorGUILayout.TextField("输入string",editorLabelText);
            editorLabelArea = EditorGUILayout.TextArea("输入多行文本",GUILayout.Height(50));
        }            

效果如下,感觉较GUILayout更好
在这里插入图片描述

EditorGUILayout.Vector3Field() 输入向量
EditorGUILayout.ColorField() 输入颜色
EditorGUILayout.CurveField() 输入曲线
        private Vector3 editorLabelVct3;
        private Color editorLabelColor;
        private AnimationCurve editorLabelCurve = new AnimationCurve(new Keyframe[]
        {
   
            new Keyframe(0,0),
            new Keyframe(1,1),
        });
        
        private void OnGUI()
        {
                       
            editorLabelVct3 = EditorGUILayout.Vector3Field("输入Vector3",editorLabelVct3);
            // editorLabelVct2 = EditorGUILayout.Vector3Field("输入Vector3",editorLabelVct2);
            // editorLabelVct4 = EditorGUILayout.Vector3Field("输入Vector3",editorLabelVct4);
            
            editorLabelColor = EditorGUILayout.ColorField("输入颜色",editorLabelColor);
            editorLabelCurve = EditorGUILayout.CurveField("输入Curve曲线
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值