Unity 编辑器篇|(六)编辑器拓展EditorGUI类 (全面总结 | 建议收藏)

1. 前言

  • EditorGUI类 专门用于编辑器界面的GUI类,提供了Unity内置类或结构体的控件函数,比如Color类、Vector3类等定义的控件。
  • 为编辑器在Inspector和EditorWindow中扩展提供了很大的方便。

2. 参数

静态函数 描述
BeginChangeCheck 启动一个新代码块来检查 GUI 更改。
BeginDisabledGroup 创建一组可禁用的控件。
BeginFoldoutHeaderGroup 创建一个左侧带有折叠箭头的标签。
BeginProperty 创建一个属性封装器,可用于使常规 GUI 控件与 SerializedProperty 配合使用。
BoundsField 创建用于输入 Bounds 的 Center 和 Extents 字段。
BoundsIntField 创建用于输入 BoundsInt 的 Position 和 Size 字段。
ColorField 创建一个用于选择 Color 的字段。
CurveField 生成一个用于编辑 AnimationCurve 的字段。
DelayedDoubleField 创建一个用于输入双精度浮点数的延迟文本字段。
DelayedFloatField 创建一个用于输入浮点数的延迟文本字段。
DelayedIntField 创建一个用于输入整数的延迟文本字段。
DelayedTextField 创建一个延迟文本字段。
DoubleField 生成一个用于输入双精度浮点数的文本字段。
DrawPreviewTexture 在矩形内绘制纹理。
DrawRect 在当前编辑器窗口中的指定位置以指定大小绘制一个着色的矩形。
DrawTextureAlpha 在矩形内绘制纹理的 Alpha 通道。
DropdownButton 创建一个能够对鼠标按下做出反应的按钮,用于显示您自己的下拉菜单内容。
DropShadowLabel 绘制带有投影的标签。
EndChangeCheck 结束代码块并检查是否有任何 GUI 更改。
EndDisabledGroup 结束由 BeginDisabledGroup 开始的禁用组。
EndFoldoutHeaderGroup 关闭以 BeginFoldoutHeaderGroup 开头的组。
EndProperty 结束由 BeginProperty 开始的属性封装器。
EnumFlagsField 在单击枚举类型的每个值时显示带有选项的菜单。一个名称为“Nothing”的值0选项和一个名称为“Everything”的值0(即所有位设置)选项总是显示在菜单的顶部。值0和0的名称可以通过在enum类型中定义这些值来重写。
EnumPopup 创建一个枚举弹出选择字段。
FloatField 创建一个用于输入浮点数的文本字段。
FocusTextInControl 将键盘焦点移动到指定的文本字段,并开始编辑内容。
Foldout 创建一个左侧带有折叠箭头的标签。
GetPropertyHeight 获取 PropertyField 控件所需的高度。
GradientField 创建一个用于编辑 Gradient 的字段。
HandlePrefixLabel 为一些控件创建一个标签。
HelpBox 创建一个带有发送给用户的消息的帮助框。
InspectorTitlebar 创建一个类似于 Inspector 窗口的标题栏。
IntField 创建一个用于输入整数的文本字段。
IntPopup 创建一个整数弹出选择字段。
IntSlider 创建一个滑动条,用户可以进行拖动以在最小值和最大值之间更改整数值。
LabelField 创建一个标签字段。(用于显示只读信息。)
LargeSplitButtonWithDropdownList 创建一个大按钮,其中包含常规按钮部分和用于打开下拉菜单的箭头。
LayerField 创建一个层选择字段。
LinkButton 制作可点击的链接标签。
LongField 创建一个用于输入长整数的文本字段。
MaskField 创建一个适用于掩码的字段。
MinMaxSlider 创建一个特殊滑动条,用户可利用该滑动条指定最小值和最大值之间的一个范围。
MultiFloatField 创建一个带有文本字段的多控件,用于在同一行中输入多个浮点值。
MultiIntField 创建一个带有文本字段的多控件,用于在同一行中输入多个整数。
MultiPropertyField 创建一个在同一行中包含多个属性字段的多控件。
ObjectField 创建一个对象字段。您可以通过拖放对象或使用对象选择器选择对象来分配对象。
PasswordField 创建一个可让用户输入密码的文本字段。
Popup 创建一个通用弹出选择字段。
PrefixLabel 创建一个显示在特定控件前的标签。
ProgressBar 创建一个进度条。
PropertyField 使用此方法在编辑器中针对 SerializedProperty 创建一个字段。
RectField 创建用于输入 Rect 的 X、Y、W 和 H 字段。
RectIntField 创建用于输入 RectInt 的 X、Y、W 和 H 字段。
SelectableLabel 创建一个可选择标签字段。(用于显示可复制粘贴的只读信息。)
Slider 创建一个滑动条,用户可以进行拖动以在最小值和最大值之间更改值。
TagField 创建一个标签选择字段。
TextArea 创建一个文本区域。
TextField 创建一个文本字段。
Toggle 创建一个开关。
ToggleLeft 创建一个开关字段,其中开关位于左侧,标签紧随其右。
Vector2Field 创建用于输入 Vector2 的 X 和 Y 字段。
Vector2IntField 创建用于输入 Vector2Int 的 X 和 Y 整数字段。
Vector3Field 创建用于输入 Vector3 的 X、Y 和 Z 字段。
Vector3IntField 创建用于输入 Vector3Int 的 X、Y 和 Z 整数字段。
Vector4Field 创建用于输入 Vector4 的 X、Y、Z 和 W 字段。

3. 功能

3.1 折叠菜单: Foldout

  • Foldout:创建一个左侧带有折叠箭头的标签。bool 用户选择的折叠状态。如果为 true,则应渲染子对象。这适用于创建树或文件夹之类的结构,其中子对象仅在父项展开时显示。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    public bool showPosition = true;
    public string status = "Select a GameObject";
    [MenuItem("Examples/Foldout Usage")]
    static void Init()
    {
    
    
        UnityEditor.EditorWindow window = GetWindow(typeof(EditorGUIExample));
        window.position = new Rect(0, 0, 150, 60);
        window.Show();
    }

    void OnGUI()
    {
    
    
        showPosition = EditorGUI.Foldout(new Rect(3, 3, position.width - 6, 15), showPosition, status);
        if (showPosition)
            if (Selection.activeTransform)
            {
    
    
                Selection.activeTransform.position = EditorGUI.Vector3Field(new Rect(3, 25, position.width - 6, 40), "Position", Selection.activeTransform.position);
                status = Selection.activeTransform.name;
            }

        if (!Selection.activeTransform)
        {
    
    
            status = "Select a GameObject";
            showPosition = false;
        }
    }

    void OnInspectorUpdate()
    {
    
    
        Repaint();
    }
}

1

3.2 检查 GUI 更改: BeginChangeCheck 、EndChangeCheck 监听值改变

  • BeginChangeCheck :启动一个新代码块来检查 GUI 更改。
  • EndChangeCheck :结束代码块并检查是否有任何 GUI 更改。
  • 结合使用来创建一个代码块,检查是否只有该代码块中包含的控件的 GUI 状态发生了更改。
  • 此方法不同于 GUI.changed,后者对于 GUI 状态的“任何”更改都返回 true。BeginChangeCheck() 将检查限制为一组特定的控件。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    float sliderValue = 0;
    string labelText = "-";

    [MenuItem("Window/Example Window")]
    static void Init()
    {
    
    
        var example = (EditorGUIExample)EditorWindow.GetWindow(typeof(EditorGUIExample));
        example.Show();
    }

    void OnGUI()
    {
    
    
        EditorGUILayout.LabelField("New value", labelText);

        EditorGUI.BeginChangeCheck();

        sliderValue =  EditorGUI.Slider(new Rect(0, 0, 100, 30), sliderValue, 0, 1);

        if (EditorGUI.EndChangeCheck())
        {
    
    
            labelText = sliderValue.ToString();
        }
    }
}

2

3.3 可禁用控件:BeginDisabledGroup 、EndDisabledGroup 是否禁用组中的控件

  • BeginDisabledGroup :创建一组可禁用的控件。
  • EndDisabledGroup :结束由 BeginDisabledGroup 开始的禁用组。
  • 如果 Disabled 设置为 true,则将禁用组内的控件。 如果为 false,则不会更改 Enabled/Disabled 状态。
    注意:与使用 EditorGUI.BeginDisabledGroup()/EditorGUI.EndDisabledGroup() 相比,DisabledScope 通常更可取,因为它提供了一个更加安全的作用域机制
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    [MenuItem("Tool/TestOnGUI")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }


    bool _foldoutValue;
    bool _isDisable;
    string _textField;
    string _passwordFile;

    public void OnGUI()
    {
    
    
        _foldoutValue = EditorGUI.Foldout(new Rect(0, 0, 20, 20), _foldoutValue, "折叠");
        if (_foldoutValue)
        {
    
    
            _isDisable = EditorGUI.Toggle(new Rect(0, 20, 20, 20), "Disable", _isDisable);
            EditorGUI.BeginDisabledGroup(_isDisable);
            _textField = EditorGUI.TextField(new Rect(0, 40, 200, 20), "账号:", _textField);
            _passwordFile = EditorGUI.PasswordField(new Rect(0, 60, 200, 20), "密码:", _passwordFile);
            EditorGUI.EndDisabledGroup();
        }
    }  
}

2

在这里插入图片描述

3.4 下拉菜单:DropdownButton

  • DropdownButton :创建一个能够对鼠标按下做出反应的按钮,用于显示您自己的下拉菜单内容。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    [MenuItem("Tool/TestOnGUI")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }


    bool _foldoutValue;

    public void OnGUI()
    {
    
    
        _foldoutValue = EditorGUI.Foldout(new Rect(0, 0, 20, 20), _foldoutValue, "折叠");
        if (_foldoutValue)
        {
    
    
            if (EditorGUI.DropdownButton(new Rect(0, 20, 200, 40), new GUIContent("下拉菜单"), FocusType.Keyboard))
            {
    
    
                GenericMenu genericMenu = new GenericMenu();
                genericMenu.AddItem(new GUIContent("One"), false, () => {
    
     Debug.Log("回调函数"); });
                genericMenu.AddSeparator(string.Empty);//分割线
                genericMenu.AddItem(new GUIContent("Two"), false, () => {
    
     });
                genericMenu.AddItem(new GUIContent("There"), false, () => {
    
     });
                genericMenu.DropDown(new Rect(0, 20, 20, 20));
            }
        }
    }  
}

3

3.5 超链接:LinkButton

  • LinkButton :超链接按钮。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    [MenuItem("Tool/TestOnGUI")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }


    public void OnGUI()
    {
    
    
        if (EditorGUI.LinkButton(new Rect(0, 0, 100, 30), "超链接"))
        {
    
    
            Application.OpenURL("www.baidu.com");
        }
    }  
}

5

3.6 颜色字段:ColorField

  • ColorField :创建一个用于选择 Color 的字段。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    [MenuItem("Tool/TestOnGUI")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }

    Color _colorFiled;
    public void OnGUI()
    {
    
    
        _colorFiled = EditorGUI.ColorField(new Rect(0, 0, 200, 20), _colorFiled);
    }  
}

6

3.7 边界盒字段:BoundsField 、BoundsIntField

  • BoundsField :创建用于输入 Bounds 的 Center 和 Extents 字段。
  • BoundsIntField :创建用于输入 BoundsInt 的 Position 和 Size 字段。
using Codice.Client.BaseCommands;
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    [MenuItem("Tool/TestOnGUI")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }

    Bounds _boundsFiledValue;
    BoundsInt _boundsIntFiledValue;
    public void OnGUI()
    {
    
    
        _boundsFiledValue = EditorGUI.BoundsField(new Rect(0, 0, 200, 20), _boundsFiledValue);
        _boundsIntFiledValue = EditorGUI.BoundsIntField(new Rect(0, 50, 200, 20), _boundsIntFiledValue);
    } 
}

7

3.8 动画曲线面板:CurveField

  • CurveField :生成一个用于编辑 AnimationCurve 的字段。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    [MenuItem("Tool/TestOnGUI")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }

    private AnimationCurve _animationCurveValue = new AnimationCurve();
    public void OnGUI()
    {
    
    
        _animationCurveValue = EditorGUI.CurveField(new Rect(0, 0, 200, 20), _animationCurveValue);
    }
}

8

3.9 渐变:GradientField

  • GradientField :创建一个用于编辑 Gradient 的字段。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    Gradient gradient = new Gradient();

    [MenuItem("Examples/Gradient Field demo")]
    static void Init()
    {
    
    
        EditorWindow window = GetWindow(typeof(EditorGUIExample));
        window.position = new Rect(0, 0, 400, 199);
        window.Show();
    }

    void OnGUI()
    {
    
    
        gradient = EditorGUI.GradientField(
            new Rect(3, 3, position.width - 6, 50), "Gradient", gradient);
    }
}

9

3.10 对象选择器:ObjectField

  • ObjectField :创建一个对象字段。您可以通过拖放对象或使用对象选择器选择对象来分配对象。当对象引用作为资源的一部分进行存储时,请确保 allowSceneObjects 参数为 false,因为资源无法存储对场景中对象的引用。如果 ObjectField 是脚本组件的自定义编辑器的一部分,请使用 EditorUtility.IsPersistent() 检查该组件是位于资源还是场景对象中。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    [MenuItem("Tool/ObjectField")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }

    private Sprite _objectFieldValue;
    public void OnGUI()
    {
    
    
        _objectFieldValue = (Sprite)EditorGUI.ObjectField(new Rect(0, 0, 50, 50), _objectFieldValue, typeof(Sprite), true);
    }
}

10

3.11 双精度浮点数字段:DoubleField、DelayedDoubleField

  • DoubleField :生成一个用于输入双精度浮点数的文本字段。
  • DelayedDoubleField :创建一个用于输入双精度浮点数的延迟文本字段。与DoubleField 类似,但在用户按 Enter 键或将焦点从双精度浮点数字段移开之前,不会返回新值。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    

    double value1 = 0F;
    double value2 = 0F;

    [MenuItem("Tool/DoubleField")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }
    public void OnGUI()
    {
    
    
        value1 = EditorGUI.DoubleField(new Rect(0, 0, 100, 30), value1);
        value2 = EditorGUI.DelayedDoubleField(new Rect(0, 50, 200, 30), value2);
    }
}

10

3.12 多维字段:RectField、RectIntField、Vector2Field、Vector2IntField、Vector3Field、Vector3IntField、Vector4Field

  • RectField :创建用于输入 Rect 的 X、Y、W 和 H 字段。
  • RectIntField :创建用于输入 RectInt 的 X、Y、W 和 H 字段。
  • Vector2Field :创建用于输入 Vector2 的 X 和 Y 字段。
  • Vector2IntField :创建用于输入 Vector2Int 的 X 和 Y 整数字段。
  • Vector3Field :创建用于输入 Vector3 的 X、Y 和 Z 字段。
  • Vector3IntField :创建用于输入 Vector3Int 的 X、Y 和 Z 整数字段。
  • Vector4Field :创建用于输入 Vector4 的 X、Y、Z 和 W 字段。

RectField:

using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    

    Camera[] cameras;

    [MenuItem("Tool/DoubleField")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }
    void OnGUI()
    {
    
    
        if (GUI.Button(new Rect(3, 3, position.width - 6, 20), "Update list"))
            cameras = FindObjectsOfType<Camera>();

        if (cameras.Length > 0)
        {
    
    
            for (var i = 0; i < cameras.Length; i++)
            {
    
    
                cameras[i].rect = EditorGUI.RectField(
                    new Rect(3, 25 + 45 * i, position.width - 6, 25),
                    cameras[i].name,
                    cameras[i].rect);
            }
        }
    }
}

1

using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    Vector2 v2;
    Vector2Int v2Int;
    Vector3 v3;
    Vector3Int v3Int;
    Vector4 v4Int;

    [MenuItem("Tool/DoubleField")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }
    void OnGUI()
    {
    
    
        v2 = EditorGUI.Vector2Field(new Rect(0, 0, 100, 30), "Vector2Field", v2);
        v2Int = EditorGUI.Vector2IntField(new Rect(0, 40, 100, 30), "Vector2IntField", v2Int);

        v3 = EditorGUI.Vector2Field(new Rect(0, 80, 100, 30), "Vector3Field", v3);
        v3Int = EditorGUI.Vector3IntField(new Rect(0, 120, 150, 30), "Vector3IntField", v3Int);

        v4Int = EditorGUI.Vector4Field(new Rect(0, 160, 150, 30), "Vector4Field", v4Int);
    }
}

6

3.13 切换框:Toggle、ToggleLeft

  • Toggle :创建一个开关。
  • ToggleLeft :创建一个开关字段,其中开关位于左侧,标签紧随其右。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    bool showClose = true;

    [MenuItem("Tool/Toggle")]
    private static void OpenGUIExample()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }
    void OnGUI()
    {
    
    
        showClose = EditorGUI.Toggle(new Rect(0, 5, position.width, 20),
             "Show Close Button",
             showClose);

        showClose = EditorGUI.ToggleLeft(new Rect(0, 25, position.width, 20),
           "Show Close Button",
           showClose);
    }
}

5

3.14 选择标签:TagField、LayerField

  • TagField :创建一个标签选择字段。
  • LayerField :创建一个层选择字段。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    string selectedTag = "";
    int selectedLayer = 0;

    [MenuItem("Examples/Field")]
    static void Init()
    {
    
    
        EditorWindow window = GetWindow<EditorGUIExample>();
        window.position = new Rect(0, 0, 350, 70);
        window.Show();
    }

    void OnGUI()
    {
    
    
        selectedTag = EditorGUI.TagField(
            new Rect(3, 3, position.width / 2 - 6, 20),
            "New Tag:",
            selectedTag);
        selectedLayer = EditorGUI.LayerField(
            new Rect(position.width / 2 + 3, 3, position.width / 2 - 6, 20),
            "New Layer:",
            selectedLayer);
    }

    void OnInspectorUpdate()
    {
    
    
        Repaint();
    }
}

10

3.15 滑动条:Slider、MinMaxSlider

  • Slider :创建一个滑动条,用户可以进行拖动以在最小值和最大值之间更改值。
  • MinMaxSlider :创建一个特殊滑动条,用户可利用该滑动条指定最小值和最大值之间的一个范围。
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;
using UnityEngine.UIElements;

public class EditorGUIExample : EditorWindow
{
    
    
    float scale = 1.0f;

    float minVal = -10;
    float minLimit = -20;
    float maxVal = 10;
    float maxLimit = 20;

    [MenuItem("Examples/Slider")]
    static void Init()
    {
    
    
        EditorWindow window = GetWindow<EditorGUIExample>();
        window.position = new Rect(0, 0, 350, 70);
        window.Show();
    }

    void OnGUI()
    {
    
    
        scale = EditorGUI.Slider(new Rect(5, 30, 150, 20), scale, 1, 100);
       EditorGUI.MinMaxSlider(
           new Rect(0, 0, position.width, 20),
           new GUIContent("Random range:"),
           ref minVal, ref maxVal,
           minLimit, maxLimit);
    }
}

12

3.16 进度条:ProgressBar

  • ProgressBar :创建一个进度条,值的范围为从 0 到 1,其中 0 表示进度条填充了 0%,而 1 表示进度条 100% 完全填充。
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;
using UnityEngine.UIElements;

public class EditorGUIExample : EditorWindow
{
    
    
    float armor = 20;
    float damage = 80;

    [MenuItem("Examples/ProgressBar")]
    static void Init()
    {
    
    
        EditorWindow window = GetWindow<EditorGUIExample>();
        window.position = new Rect(0, 0, 350, 70);
        window.Show();
    }

    void OnGUI()
    {
    
    
        armor = EditorGUI.IntSlider(new Rect(3, 3, position.width - 6, 15), "Armor", Mathf.RoundToInt(armor), 0, 100);
        damage = EditorGUI.IntSlider(new Rect(3, 25, position.width - 6, 15), "Damage", Mathf.RoundToInt(damage), 0, 100);

        EditorGUI.ProgressBar(new Rect(3, 45, position.width - 6, 20), armor / 100, "Armor");
        EditorGUI.ProgressBar(new Rect(3, 70, position.width - 6, 20), damage / 100, "Damage");
    }
}

13

3.17 帮助框:HelpBox

  • HelpBox :创建一个带有发送给用户的消息的帮助框。
  EditorGUI.HelpBox(new Rect(0, 0, 10, 10), "message", MessageType.Info);

3.18 Inspector标题栏:InspectorTitlebar

  • InspectorTitlebar :创建一个类似于 Inspector 窗口的标题栏。
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;
using UnityEngine.UIElements;

public class EditorGUIExample : EditorWindow
{
    
    
    bool showing = true;
    [MenuItem("Examples/InspectorTitlebar")]
    static void Init()
    {
    
    
        EditorWindow window = GetWindow<EditorGUIExample>();
        window.position = new Rect(0, 0, 350, 70);
        window.Show();
    }

    void OnGUI()
    {
    
    
        var currObj = Selection.activeTransform;
        showing = EditorGUI.InspectorTitlebar(new Rect(0, 0, position.width, 20), showing, currObj, showing);
    }
}

在这里插入图片描述

3.19 文本:FloatField、IntField、LabelField、LongField、MultiFloatField、MultiIntField、MultiPropertyField、TextArea、TextField、PasswordField

  • FloatField :创建一个用于输入浮点数的文本字段。
  • IntField :创建一个用于输入整数的文本字段。
  • LabelField :创建一个标签字段。(用于显示只读信息。)
  • LongField :创建一个用于输入长整数的文本字段。
  • MultiFloatField :创建一个带有文本字段的多控件,用于在同一行中输入多个浮点值。
  • MultiIntField :创建一个带有文本字段的多控件,用于在同一行中输入多个整数。
  • MultiPropertyField :创建一个在同一行中包含多个属性字段的多控件。
  • TextArea :创建一个文本区域。
  • TextField :创建一个文本字段。
  • PasswordField :创建一个可让用户输入密码的文本字段。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    

    float FloatField = 0;
    int IntField = 0;
    long LongField = 0;
    float[] numbers = new float[] {
    
     0, 1, 2 };
    GUIContent[] contents = new GUIContent[] {
    
    
    new GUIContent ("X"),
    new GUIContent ("Y"),
    new GUIContent ("Z")};

    string note = "Notes:";
    string PasswordField = "";
    string objNames = "";
    [MenuItem("Examples/Slider")]
    static void Init()
    {
    
    
        EditorWindow window = GetWindow<EditorGUIExample>();
        window.position = new Rect(0, 0, 350, 70);
        window.Show();
    }

    void OnGUI()
    {
    
    
        FloatField = EditorGUI.FloatField(new Rect(0, 0, 200, 20), "FloatField:", FloatField);
        IntField = EditorGUI.IntField(new Rect(0, 20, 200, 20), "IntField:", IntField);
        LongField = EditorGUI.LongField(new Rect(0, 40, 200, 20), "LongField:", LongField);
        EditorGUI.LabelField(new Rect(0, 60, position.width, 20), "LabelField", EditorApplication.timeSinceStartup.ToString());
        EditorGUI.MultiFloatField(
            new Rect(0, 100, 200, EditorGUIUtility.singleLineHeight),
            new GUIContent("位置点"), contents, numbers);
        note = EditorGUI.TextArea(new Rect(0, 150, 100, 30), note);
        objNames = EditorGUI.TextField(new Rect(0, 190, position.width - 20, 20),
            "TextField:",
            objNames);

        PasswordField = EditorGUI.PasswordField(new Rect(0, 210, 100, 20), PasswordField);
    }
}

12

3.20 通用弹出选择字段:Popup

  • Popup :创建一个通用弹出选择字段,以参数形式获取当前所选的索引,并返回用户选择的索引。
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;
using UnityEngine.UIElements;

public class EditorGUIExample : EditorWindow
{
    
    
    string[] options = {
    
     "Rigidbody", "Box Collider", "Sphere Collider" };
    int index = 0;

    [MenuItem("Examples/ProgressBar")]
    static void Init()
    {
    
    
        EditorWindow window = GetWindow<EditorGUIExample>();
        window.position = new Rect(0, 0, 350, 70);
        window.Show();
    }

    void OnGUI()
    {
    
    
        index = EditorGUI.Popup(
            new Rect(0, 0, position.width, 20),
            "Component:",
            index,
            options);
    }
}

15

3.21 单选枚举:EnumPopup

  • EnumPopup :创建一个枚举弹出选择字段。
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;
using UnityEngine.UIElements;

public class EditorGUIExample : EditorWindow
{
    
    
    OPTIONS display = OPTIONS.Position;
    public enum OPTIONS
    {
    
    
        Position = 0,
        Rotation = 1,
        Scale = 2,
    }

    [MenuItem("Examples/ProgressBar")]
    static void Init()
    {
    
    
        EditorWindow window = GetWindow<EditorGUIExample>();
        window.position = new Rect(0, 0, 350, 70);
        window.Show();
    }

    void OnGUI()
    {
    
    
        display = (OPTIONS)EditorGUI.EnumPopup(
           new Rect(3, 3, position.width - 6, 15),
           "Show:",
           display);
    }
}

16

3.22 多选枚举:EnumFlagsField

  • EnumFlagsField :单击时显示一个菜单,其中包含针对枚举类型的每个值的选项。名称为“Nothing”的值 0 选项和名称为“Everything”的值 ~0(即所有位设置)选项始终显示在菜单顶部。值 0 和 ~0 的名称可以通过在枚举类型中定义这些值来覆盖。
using UnityEditor;
using UnityEngine;

public class EditorGUIExample : EditorWindow
{
    
    
    enum ExampleFlagsEnum
    {
    
    
        None = 0, 
        A = 1 << 0,
        B = 1 << 1,
        AB = A | B, 
        C = 1 << 2,
        All = ~0, 
    }

    ExampleFlagsEnum m_Flags;

    [MenuItem("Examples/EnumFlagsField Example")]
    static void OpenWindow()
    {
    
    
        GetWindow<EditorGUIExample>().Show();
    }

    void OnGUI()
    {
    
    
        m_Flags = (ExampleFlagsEnum)EditorGUI.EnumFlagsField(new Rect(5, 5, 300, 20), m_Flags);
    }
}

2

3.23 单选/多选整型:IntPopup、MaskField

  • IntPopup :创建一个整数弹出选择字段。
  • MaskField :创建一个适用于掩码的字段。
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using static Unity.IO.LowLevel.Unsafe.AsyncReadManagerMetrics;

public class EditorGUIExample : EditorWindow
{
    
    
    int flags = 0;
    string[] options = {
    
     "CanJump", "CanShoot", "CanSwim" };


    int selectedSize = 1;
    string[] names = {
    
     "Double", "Triple", "Quadruple" };
    int[] sizes = {
    
     2, 3, 4 };

    [MenuItem("Examples/Mask Field Usage")]
    static void Init()
    {
    
    
        var window = GetWindow<EditorGUIExample>();
        window.Show();
    }

    void OnGUI()
    {
    
    
        flags = EditorGUI.MaskField(new Rect(0, 0, 300, 20), "Player Flags", flags, options);

        selectedSize = EditorGUI.IntPopup(
           new Rect(0, 30, position.width - 6, 20),
           "Size:",
           selectedSize,
           names,
           sizes);
    }
}

3

猜你喜欢

转载自blog.csdn.net/backlighting2015/article/details/135566416