Unity编译器拓展学习总结(三):编译器拓展布局UI系统——IMGUI

Unity编译器拓展学习总结(三):编译器拓展基本布局UI系统——IMGUI


前情参考:
Unity编译器拓展学习总结(一):常用工具API
Unity编译器拓展学习总结(二):各个平台菜单栏拓展


前言

上一篇我们介绍Unity的各个位置的菜单栏拓展,主要就是加上特性,这节我们将介绍布局的UI系统——IMGUI,这个是Unity的原生GUI,以前常用的布局。


一、GUILayout

可以用于运行时的GUI,也可以用于编译器的GUI,不需要传入Rect矩形布局即可,默认占一行。

1、GUILayout控件

public class GUILayoutTest : MonoBehaviour
{
    private string textAreaValue;
    private string textFieldValue;
    private string passWordFieldValue=string.Empty;
    private bool toggleValue;
    private float horSlider;
    private float verSlider;
    private int selectGrid;
    private int selectedToobar;
    private float horScrollbar;
    private float verScrollbar;
    private void OnGUI()
    {
        #region GUILayout控件
        GUILayout.Label("Hello");//文本控件
        textAreaValue = GUILayout.TextArea(textAreaValue);//文本输入框
        textFieldValue = GUILayout.TextField(textFieldValue);//单行文本输入框
        passWordFieldValue = GUILayout.PasswordField(passWordFieldValue, '*');//密码输入框
        //按钮
        if (GUILayout.Button("确认"))
        {
            //按钮逻辑
            Debug.Log("确认");
        }
        //可以持续按下的按钮
        if (GUILayout.RepeatButton("重复确认"))
        {
            //重复按钮逻辑
            Debug.Log("重复确认");
        }
        toggleValue = GUILayout.Toggle(toggleValue, "开关");//开关
        horSlider = GUILayout.HorizontalSlider(horSlider, 0, 100);//横向滑动条
        verSlider = GUILayout.VerticalSlider(verSlider, 0, 100);//纵向滑动条
        GUILayout.Box("B");//标签
        selectGrid = GUILayout.SelectionGrid(selectGrid, new string[] { "1", "2", "3" }, 1);//格子选择布局
        selectedToobar = GUILayout.Toolbar(selectedToobar, new string[] { "1", "2", "3" });//分页框,横向的
        horScrollbar = GUILayout.HorizontalScrollbar(horScrollbar, 20, 0, 100);//横向滚动条
        verScrollbar = GUILayout.VerticalScrollbar(verScrollbar, 20, 0, 100);//横向滚动条
        #endregion
    }
}

运行效果如下:
GUILayout控件

2、GUILayout布局

public class GUILayoutTest : MonoBehaviour
{
    private void OnGUI()
    {
        #region GUILayout布局

        //当只传入一个参数时,第一个参数是布局的样式名,传入GUIStyle会是该布局名
        GUILayout.BeginHorizontal("Hello",GUI.skin.box);//横向布局,可以把一系列的控件或布局放在中间
        GUILayout.Space(120);//空格布局
        //放一系列的控件或布局,将会都放在一行
        GUILayout.Label("HelloLine");
        GUILayout.Label(new Texture2D(10, 10));
        GUILayout.EndHorizontal();//以End的相应布局结束

        GUILayout.BeginVertical("box");//纵向布局
        //放一系列的控件或布局,将会都放在一列
        GUILayout.Label("HelloLine");
        GUILayout.FlexibleSpace();//灵活布局,会自动调整距离
        GUILayout.Label(new Texture2D(10, 10));
        GUILayout.EndVertical();

        GUILayout.BeginVertical("box");
        scrollPos =GUILayout.BeginScrollView(scrollPos,true, true,GUI.skin.horizontalScrollbar,GUI.skin.verticalScrollbar);//滚动视窗布局
        GUILayout.Label("ScrollView");
        GUILayout.Label("ScrollView");
        GUILayout.Label("ScrollView");
        GUILayout.Label("ScrollView");
        GUILayout.Label("ScrollView");
        GUILayout.Label("ScrollView");
        GUILayout.Label("ScrollView");
        GUILayout.Label("ScrollView");
        GUILayout.EndScrollView();
        GUILayout.EndVertical();

        GUILayout.BeginArea(new Rect(300, 0, 200, 200),"区域块",GUI.skin.box);//绘制一个区域块,超出部分将不会显示
        GUILayout.Label("Area1");
        GUILayout.Label("Area2");
        GUILayout.Label("Area3");
        GUILayout.Label("Area4");
        GUILayout.Label("Area1");
        GUILayout.Label("Area2");
        GUILayout.Label("Area3");
        GUILayout.Label("Area4");
        GUILayout.Label("Area1");
        GUILayout.Label("Area2");
        GUILayout.Label("Area3");
        GUILayout.Label("Area4");
        GUILayout.EndArea();

        #region 实现窗口布局
        if (!isClick)
        {
            if(GUILayout.Button("打开窗口"))
            isClick = true;
        }
        if (isClick)
        {
            //窗口布局
            screenRect = GUILayout.Window(0, screenRect, id =>
            {
                GUILayout.Label("My Window");
                GUILayout.Label("My Window2");
                if (GUILayout.Button("关闭"))
                {
                    isClick = false;
                }
             
                //用于拖动窗口
                GUI.DragWindow();//注意需要放在最后,不然控件会失效
                //GUI.DragWindow(new Rect(0, 0, 500, 200));可以设置的窗口内部可拖动区域,是相对于左上角的位置坐标

            }, "我的窗口");
            //模拟弹窗的窗口,需要关闭本窗口才能操作其他窗口
            screenRect2 = GUI.ModalWindow(1, screenRect2, id =>
            {
                GUILayout.Label("My Window");
                GUILayout.Label("My Window2");
                if (GUILayout.Button("关闭"))
                {
                    isClick = false;
                }
                //用于拖动窗口
                GUI.DragWindow();//注意需要放在最后,不然控件会失效
                //GUI.DragWindow(new Rect(0, 0, 500, 200));可以设置的窗口内部可拖动区域,是相对于左上角的位置坐标

            }, "我的窗口2");
            //GUI.BringWindowToBack(0);将窗口显示在其他窗口下面
            //GUI.BringWindowToFront(0); 将窗口显示在其他窗口上面
            //GUI.FocusWindow(0);//根据窗口Id聚焦在窗口
            //GUI.UnfocusWindow();//取消聚焦窗口
        }
        #endregion
        #endregion
    }
    bool isClick = false;
    Vector2 scrollPos= Vector2.zero;
    Rect screenRect= new Rect(500,200,400,300);
    Rect screenRect2 = new Rect(500, 200, 400, 300);

运行效果如下:
GUILayout布局

3、GUILayout样式操作

GUI的样式一般是GUIStyle,还有GUILayoutOption,每个控件,布局组件几乎都可以设置,一般GUIStyle在前,GUILayoutOption[]在后,这些样式都在控件的参数列表最后,GUIStyle可以不加进去。

 #region GUILayout样式

        //1、GUILayoutOption操作
        GUILayoutOption layoutOption1 = GUILayout.Width(300);//宽度
        GUILayoutOption layoutOption2= GUILayout.Height(300);//高度
        GUILayoutOption layoutOption3= GUILayout.MinWidth(300);//最小宽度
        GUILayoutOption layoutOption4= GUILayout.MaxWidth(300);//最大宽度
        GUILayoutOption layoutOption5= GUILayout.MinHeight(300);//最小高度
        GUILayoutOption layoutOption6= GUILayout.MaxWidth(300);//最大高度
        GUILayoutOption layoutOption7= GUILayout.ExpandHeight(true);//扩展高度,可以扩展到区域的最高
        GUILayoutOption layoutOption8= GUILayout.ExpandWidth(true);//扩展宽度,可以扩展到区域的最宽

        //2、GUISkin,GUI的皮肤,可以使用GUI.skin获取编译器的默认皮肤设置,也可以自己设计一个皮肤在外赋值,即右击Assets面板获取
        GUISkin skin = new GUISkin();
        //2、Style样式,可以使用默认,也可以自己设置
        GUIStyle style = new GUIStyle(GUI.skin.label);
        skin.box = new GUIStyle(GUI.skin.box);//设置为默认的box的风格
        style.alignment= TextAnchor.MiddleCenter;//文本对齐方式
        style.normal.textColor = Color.white;//文本的颜色
        style.fontSize = 18;//文本显示大小
        style.fontStyle= FontStyle.Bold;//文本样式,如加粗、斜体等
        //style.font = new Font();//文本字体
        style.stretchHeight= true;//是否自动扩展高度
        style.stretchWidth= true;//是否自动扩展宽度
        style.border = new RectOffset(2, 2, 2, 2);//所有背景图片的边界
        style.clipping = TextClipping.Clip;//当超出范围时的裁剪方式
        GUILayout.Label("HelloLine",style);
        #endregion

下面是加入自定义的GUISkin操作
自创GUISkin

二、GUI

GUI的操作也是类似于GUILayout,不过GUI有一些新的API可以使用,这一点区别于GUILayout。

using UnityEngine;

public class GUITest : MonoBehaviour
{
    private void OnGUI()
    {
        #region GUI控件
        //用GUI的API的控件于GUILayout不同,需要在前面加入Rect矩形数据结构,
        //指定左上角位置坐标与大小,后面几乎与GUILayout参数列表一致
        GUI.Label(new Rect(0, 0, 200, 200), "Hello");//文本控件
        #endregion

        #region GUI布局

        //开始一个组,会在固定位置固定大小绘制一个群组的UI元素
        GUI.BeginGroup(new Rect(Screen.width / 2 - 400, Screen.height / 2 - 300, 800, 600));
        //注意,此时该矩形数据的位置为相对该组的位置
        if(GUI.Button(new Rect(0, 0, 100, 100), "点击"))
        {
            Debug.Log("点击");
        }
        //GUI.Box(new Rect(0, 0, 800, 600), "This box is now centered! - here you would put your main menu");
        GUI.EndGroup();
        #endregion

        #region GUI属性字段、其他方法
        //Debug.Log(GUI.depth);//可以用于设置GUI的渲染排序层级

        #region 设置控件背景颜色
        GUI.contentColor = Color.yellow;//全局文字颜色
        beforeColor = GUI.backgroundColor;//可以用来设置GUI背景颜色,也可以用于渲染控件颜色
        GUI.backgroundColor = Color.blue;
        if (GUI.Button(new Rect(0, 200, 100, 50), "颜色按钮"))
        {
            Debug.Log("点击");
        }
        GUI.backgroundColor = beforeColor;
        #endregion

        //GUI.color;//文字和背景颜色

        GUI.enabled=false;//是否启用GUI
        if (GUI.Button(new Rect(0, 250, 100, 50), "启用"))
        {
            Debug.Log("启用");
        }
        GUI.enabled = true;

        //模拟登录
        GUI.SetNextControlName("user");//设置下一个控件的名称。将使用给定名称注册下一个控件。
        login = GUI.TextField(new Rect(60, 10, 130, 20), login);
        //点击tab键且聚焦在user控件上
        if (Event.current.Equals(Event.KeyboardEvent("tab")) &&
            GUI.GetNameOfFocusedControl() == "user")
            Debug.Log("Login");
        //Debug.Log(GUI.GetNameOfFocusedControl());//获取当前聚焦的控件名
        GUI.FocusControl("user");//聚焦在指定控件上
        //GUI.skin;//编译器的皮肤设置,可以获取到各种控件,布局默认设置

        //使用自定义的GUIContent参数设置提示信息
        GUI.Button(new Rect(60, 40, 100, 20), new GUIContent("Click me", "This is the tooltip"));
        //GUI.tooltip获取当前提示信息,当鼠标进入或者按键进入时才有值
        GUI.Label(new Rect(60, 60, 100, 40), GUI.tooltip);

        #region 图形绘制(很消耗DrawCall)
        绘制图形
        //GUI.DrawTexture(new Rect(60, 100, 100, 100), Resources.Load<Sprite>("open").texture,ScaleMode.StretchToFill,
        //    true,0.5f,Color.cyan,50,60);

        //Texture2D tex = Resources.Load<Sprite>("process").texture;
        指定要显示在的屏幕区域
        //Rect destRect = new Rect(200, 200, 100, 100);
        指定要显示的图片内部区域
        //Rect sourceRect = new Rect(0, 0, tex.width, tex.height/2);
        //DrawTextureWithTexCoords(destRect,sourceRect,tex);
        #endregion

        #endregion

        #region GUIContent
        //GUIContent就是GUI的一些内容设置,可以设置文本信息,贴图信息、提示信息,除了输入框控件,一般控件的参数列表有
        GUIContent mGUIContent = new GUIContent("内容", new Texture2D(200, 200, TextureFormat.ARGB4444, true), "提示信息")
        {
            text= "内容",
            image= new Texture2D(200, 200, TextureFormat.ARGB4444, true),
            tooltip="提示信息"
        };
        #endregion
    }
    Color beforeColor;
    string login;
    Rect screenRect = new Rect(500, 200, 400, 300);

    /// <summary>
    /// 绘制图片部分区域
    /// </summary>
    /// <param name="destRect">指定要显示在的屏幕区域</param>
    /// <param name="sourceRect">指定要显示的图片内部区域</param>
    /// <param name="tex"></param>
    void DrawTextureWithTexCoords(Rect destRect, Rect sourceRect, Texture tex)
    {
        int tw, th;
        tw = tex.width;
        th = tex.height;

        //------------调整放缩比例------------------
        sourceRect.x = sourceRect.x / tw;//x位置比例
        //屏幕坐标系原点: 左上角
        //图片坐标系原点: 左下角
        //图片的Y轴与屏幕的Y转方向相反,这里需要颠倒一下(都以左上角为坐标原点)
        sourceRect.y = 1.0f - (sourceRect.y + sourceRect.height) / th;//y位置比例
        sourceRect.width = sourceRect.width / tw;//宽比例
        sourceRect.height = sourceRect.height / th;//高比例
        //------------------------------------------

        GUI.DrawTextureWithTexCoords(destRect, tex, sourceRect, true);
    }
}

效果如下:
GUI相关介绍

三、EditorGUILayout

EditorGUILayout只能用于编译器模式下,只能放在Editor的文件夹内。作用类似于GUILayout。

1、EditorGUILayout控件

EditorGUILayout控件于GUILayout控件相比增加了不少功能。

using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEditor.EditorTools;
using UnityEngine;

public class EditorGUILayoutTest : EditorWindow
{
    [MenuItem("Test/打开EditorGUILayoutTest窗口")]
    static void OpenMyWindow()
    {
        var myWindow = GetWindow<EditorGUILayoutTest>();
        myWindow.Show();
    }
    string TAGValue;
    string inputAreaValue;
    string inputFieldValue;

    Bounds mBounds;
    Color mColor;
    AnimationCurve mCurve = new AnimationCurve();
    Gradient mGradient=new Gradient();
    Object mObj;

    double delayedDouble;
    double doubleValue;

    public MyFlags flags;//Flags
    public MyEnum enums;
    string m_itemString = string.Empty;
    int intPopValue;

    bool mInspectorFold;
    int intSliderValue;//int的Slider值
    float sliderValue;
    float KnobValue;

    float minSValue = 50;
    float maxSValue = 100;

    bool toggleValue;
    bool toggleLeftValue;
    private void OnGUI()
    {
        #region EditorGUILayout控件

        EditorGUILayout.LabelField("这是一个标签");//标签文本
        EditorGUILayout.SelectableLabel("这是一个可选择标签");

        inputAreaValue = EditorGUILayout.TextArea(inputAreaValue);//多行输入框
        inputFieldValue = EditorGUILayout.TextArea(inputFieldValue);//单行输入框

        EditorGUILayout.PrefixLabel("我的边界前面部分");//放在之前一些控件前的标签文本
        //1、Unity数据结构输入框
        mBounds = EditorGUILayout.BoundsField("我的边界", mBounds);//Bounds显示
        //EditorGUILayout.BoundsIntField//BoundsInt显示
        mColor = EditorGUILayout.ColorField("我的颜色", mColor);//颜色显示
        mCurve = EditorGUILayout.CurveField("我的曲线", mCurve);//曲线显示
        //EditorGUILayout.Vector2Field//Vector2显示
        //EditorGUILayout.Vector3Field//Vector3显示
        mGradient = EditorGUILayout.GradientField("颜色渐变",mGradient);
        //EditorGUILayout.LayerField//层级
        //EditorGUILayout.MaskField//Mask遮罩
        //EditorGUILayout.RectField //矩形框
        //EditorGUILayout.RectIntField
        //标签栏
        TAGValue = EditorGUILayout.TagField(TAGValue);
        //1.1、Unity的Object数据选择框
        SerializedObject serializedObject = new SerializedObject(Selection.activeObject.GetComponent<SpriteRenderer>());
        //EditorGUILayout.ObjectField(serializedObject.FindProperty("m_Sprite"), typeof(Sprite));
       
        mObj =EditorGUILayout.ObjectField(Selection.activeObject,Selection.activeObject.GetType(),true);
        //1.2、根据属性制作一个字段
        EditorGUILayout.PropertyField(serializedObject.FindProperty("m_DrawMode"));
        //2、数值型输入框
        delayedDouble = EditorGUILayout.DelayedDoubleField("浮点显示", delayedDouble);//浮点数字显示
        //EditorGUILayout.DelayedFloatField
        //EditorGUILayout.DelayedIntField
        //EditorGUILayout.IntField
        doubleValue = EditorGUILayout.DoubleField("浮点显示2", doubleValue);//浮点显示

        //EditorGUILayout.FloatField
        //EditorGUILayout.LongField

        //3、下拉框
        //3.1、下拉框按钮实现下拉框选择
        if (EditorGUILayout.DropdownButton(new GUIContent(m_itemString, "这是一个下拉按钮"), FocusType.Keyboard))
        {
            var alls = new string[4] { "A", "B", "C", "D" };//下拉框内容
            GenericMenu _menu = new GenericMenu();
            foreach (var item in alls)
            {
                if (string.IsNullOrEmpty(item))
                {
                    continue;
                }
                //添加菜单
                _menu.AddItem(new GUIContent(item), m_itemString.Equals(item), OnValueSelected, item);
            }
            _menu.ShowAsContext();//显示菜单
        }
        //3.2 EnumFlagsField实现下拉框选择(用于选择枚举标志,可以多选)
        flags = (MyFlags)EditorGUILayout.EnumFlagsField(flags);
        //3.3 EnumPopup实现下拉框选择
        enums = (MyEnum)EditorGUILayout.EnumPopup(enums);
        //3.4 IntPopup,可支持非枚举值显示
        //intPopValue = EditorGUILayout.IntPopup(intPopValue, new string[] { "A", "B", "C", "D" }, new int[] { 0, 1, 2, 3 });
        //3.5 Popup实现
        intPopValue = EditorGUILayout.Popup(intPopValue, new string[] { "A", "B", "C", "D" });

        //Toolbar显示编译器工具,场景中的
        //PlatformTool hcl = ScriptableObject.CreateInstance<PlatformTool>();
        //EditorGUILayout.EditorToolbar(hcl);//参数有EditorTool,可以自己设置Tool内容。
        //EditorGUILayout.ToolContextToolbar()
        //EditorGUILayout.EditorToolbarForTarget(new GUIContent("Tools", "这是一个编辑工具"),Selection.activeObject.GetComponent<Transform>()); //指定具体目标的Toolbar显示编译器工具
        //EditorGUILayout.ToolContextToolbarForTarget(new GUIContent("HUANG", "这是一个工具栏"), ScriptableObject.CreateInstance<WobbleContext>());

        //4、HelpBox帮助框
        EditorGUILayout.HelpBox("这是一个帮助框", MessageType.Info);


        //5、产生一个类似于Inspect面板的组件最上面的信息
        mInspectorFold=EditorGUILayout.InspectorTitlebar(mInspectorFold, Selection.activeObject);

        //6、Slider
        sliderValue = EditorGUILayout.Slider(sliderValue, 0, 100);
        intSliderValue = EditorGUILayout.IntSlider(intSliderValue, 0, 100);
        //6.2、最大最小值滑动条
        EditorGUILayout.MinMaxSlider(ref minSValue, ref maxSValue, 0, 150);
        
        //产生一个圆形填充块
        KnobValue = EditorGUILayout.Knob(new Vector2(50, 50), KnobValue, 0, 100, "个", Color.gray, Color.yellow, true);

        //链接按钮,可以用于打开链接
        if (EditorGUILayout.LinkButton("链接按钮"))
        {

        }

        toggleValue = EditorGUILayout.Toggle("开关",toggleValue);//开关
        toggleLeftValue = EditorGUILayout.ToggleLeft("左开关", toggleLeftValue);//开关在左边的开关
       
        #endregion
    }
    void OnValueSelected(object value)
    {
        m_itemString = value.ToString();
    }

    [System.Flags]
    public enum MyFlags
    {
        A = 1 << 0,
        B = 1 << 2,
        Z = 1 << 31,

        AB = A | B,
        AZ = A | Z,
    }
    public enum MyEnum
    {
        A,
        B,
        C,
        D
    }
}

效果如下:
EditorGUILayout控件

2、EditorGUILayout布局

  private void OnGUI()
  {
        #region EditorGUILayout布局
        //横向布局
        EditorGUILayout.BeginHorizontal("box");
        EditorGUILayout.LabelField("这是横向布局");
        EditorGUILayout.LabelField("这是横向布局");
        EditorGUILayout.EndHorizontal();
        //纵向布局
        EditorGUILayout.BeginVertical("box");
        EditorGUILayout.LabelField("这是纵向布局");
        EditorGUILayout.LabelField("这是纵向布局");
        EditorGUILayout.EndVertical();
        //滚动视窗
        scrollView=EditorGUILayout.BeginScrollView(scrollView);
        EditorGUILayout.LabelField("这是滚动视窗");
        EditorGUILayout.LabelField("这是滚动视窗");
        EditorGUILayout.LabelField("这是滚动视窗");
        EditorGUILayout.LabelField("这是滚动视窗");
        EditorGUILayout.LabelField("这是滚动视窗");
        EditorGUILayout.LabelField("这是滚动视窗");

        //目标打包平台布局
        buildTargetGroup=EditorGUILayout.BeginBuildTargetSelectionGrouping();
        if (buildTargetGroup == BuildTargetGroup.Standalone)
        {
            Debug.Log("打包Window");
        }
        EditorGUILayout.EndBuildTargetSelectionGrouping();
        EditorGUILayout.EndScrollView();

        fadeValue = EditorGUILayout.Slider(fadeValue, 0.1f, 1f);//向上移动的的像素范围
        fadeShow=EditorGUILayout.BeginFadeGroup(fadeValue);//可以进行渐入渐出的效果
        EditorGUILayout.BeginVertical();
        fold = EditorGUILayout.Foldout(fold, "折叠标签");//折叠标签,可以制作树形结构
        if (fold)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("树形结构子节点");
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndFadeGroup();

        EditorGUILayout.Separator();//空6个像素

        foldoutheader = EditorGUILayout.BeginFoldoutHeaderGroup(foldoutheader, "这是一个折叠组");
        if (foldoutheader)
        {
            EditorGUILayout.LabelField("折叠组成员1");
            EditorGUILayout.LabelField("折叠组成员2");
        }
        EditorGUILayout.EndFoldoutHeaderGroup();

        toggleGroupValue=EditorGUILayout.BeginToggleGroup("这是一个开关组", toggleGroupValue);
        if (toggleGroupValue)
        {
            EditorGUILayout.LabelField("开关组成员1");
            EditorGUILayout.LabelField("开关组成员2");
        }
        EditorGUILayout.EndToggleGroup();

        EditorGUILayout.Space(10);//空格
        EditorGUILayout.Separator();//空6个像素

        EditorGUILayout.LabelField("空格后的结果");
        //Debug.Log(EditorGUILayout.GetControlRect(true,50, EditorStyles.boldLabel));
}
     bool fold;
    float fadeValue=1;
    bool fadeShow;
    bool foldoutheader;
    bool toggleGroupValue;
    Vector2 scrollView = Vector2.zero;
    BuildTargetGroup buildTargetGroup;
        #endregion

效果如下:
EditorGUILayout布局

3、EditorGUILayout样式

        #region EditorGUILayout样式

        //可以直接通过EditorStyles获取属性找到想要的样式
        Font font = EditorStyles.boldFont; //加粗字体
        GUIStyle style = EditorStyles.boldLabel;//加粗样式文本
       //EditorStyles.label;//标签样式

        #endregion

四、EditorGUI

只能用于编译器模式下,只能放在Editor文件夹内,与EditorGUILayout用法相似,有一些新的API可以使用。

using UnityEditor;
using UnityEngine;

public class EditorGUITest : EditorWindow
{
    [MenuItem("Test/打开EditorGUITest窗口")]
    static void OpenMyWindow()
    {
        var myWindow = GetWindow<EditorGUITest>();
        myWindow.Show();
    }

    private void OnGUI()
    {
        #region  EditorGUI控件
        //EditorGUI的控件与EditorGUILayout基本相似,也只是多了一个Rect矩形数据结构
        EditorGUI.LabelField(new Rect(0, 0, 100, 10), "这是一个标签");//标签文本
        //下面将只介绍EditorGUI所特有的控件。
        //类似于前置标签控件
        EditorGUI.HandlePrefixLabel(new Rect(10, 10, 135, 25), new Rect(0, 50, 135, 25), new GUIContent("Hello",
           "tip"), 0);
        EditorGUI.DropShadowLabel(new Rect(10, 50, 135, 25), "这是一个下拉框遮罩标签");//有阴影的标签

        //多重布局分配
        EditorGUI.MultiFloatField(new Rect(0, 100, 130, 50), new GUIContent("Test"), new GUIContent[]
        {
            new GUIContent("A"),
            new GUIContent("B")
        }, new float[] { 0.2f, 0.5f });
        //EditorGUI.MultiIntField;
        //EditorGUI.MultiIntField;
        //EditorGUI.MultiPropertyField;
        progressSliderValue = EditorGUILayout.Slider(progressSliderValue, 0, 1);
        EditorGUI.ProgressBar(new Rect(0, 150, 130, 50), progressSliderValue, "进度条");//一个进度条,值是0到1
        #endregion

        #region EditorGUI布局
        //内容的更改检测,可以用于及时的更新
        EditorGUI.BeginChangeCheck();
        EditorGUI.EndChangeCheck();

        EditorGUI.BeginDisabledGroup(progressSliderValue <= 0.5f);
        //下拉型按钮
        if(EditorGUI.DropdownButton(new Rect(0,200,50,50),new GUIContent("可用显示按钮"), FocusType.Keyboard))
        {
            Debug.Log("显示");
        }
        EditorGUI.EndDisabledGroup();


        SerializedObject serializedObj =new SerializedObject(Selection.activeTransform);
        SerializedProperty property = serializedObj.FindProperty("m_LocalPosition");
        propertyWapper =EditorGUI.BeginProperty(new Rect(0,250,150,25), propertyWapper, property);//开始一段属性
        EditorGUI.BeginChangeCheck();

        newValue=EditorGUI.Vector3Field(new Rect(0, 250, 150, 25), propertyWapper, newValue);
        if (EditorGUI.EndChangeCheck())
        {
            property.vector3Value = newValue;
        }
        EditorGUI.EndProperty();
        #endregion

        #region 其他
        //EditorGUI.DrawPreviewTexture;//绘制前置贴图
        EditorGUI.DrawRect(new Rect(0, 300, 150, 25), Color.blue);//绘制矩形颜色
        //EditorGUI.DrawTextureAlpha;//绘制有透明度的贴图
        //EditorGUI.DrawTextureTransparent();//绘制透明贴图

        //EditorGUI.FocusTextInControl("MYTEST");//聚焦在MYTEST命名的控件上
        //EditorGUI.GetPropertyHeight(property);//获取属性字段的高
        EditorGUI.indentLevel = 2;//缩进值
        Debug.Log(EditorGUI.IndentedRect(new Rect(0, 0, 100, 100)));//获得缩进的矩形框
        Debug.Log(EditorGUI.CanCacheInspectorGUI(property));//检测属性是否能被缓存
        #endregion
    }
    float progressSliderValue;
    Vector3 newValue;
    GUIContent propertyWapper;
}

效果如下:
EditorGUI

五、GUILayoutUtility/GUIUtility/EditorGUILayoutUtility/EditorGUIUtility/EditorUtility

是GUI的一些辅助工具类,详细学习参考以下链接:
Unity Editor 编辑器拓展 08——各种辅助类 EditorGUIUtility、EditorUtility、GUIUtility、GUILayoutUtility

总结

本文详细介绍了IMGUI系统所涉及到的所有内容,通过这章的学习,可以基于此进而对Unity的各个面板进行拓展。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值