Version: Unity 6.0 (6000.0)
语言 : 中文
使用 IMGUI 创建自定义Editor窗口
使用 IMGUI 创建自定义编辑器

将属性绘制器与 IMGUI 结合使用可自定义 Inspector

注意:强烈建议用 UI 工具包扩展 Unity Editor,这样能提供比 IMGUI 更现代、更灵活和可扩展的解决方案。

借助属性绘制器,可通过使用脚本的特性或控制特定 Serializable 类的外观来自定义 __Inspector 窗口__中某些控件的外观。

属性绘制器有两种用途:

  • 自定义 Serializable 类的每个实例的 GUI。
  • 通过自定义的__属性特性__来自定义脚本成员的 GUI。

自定义 Serializable 类的 GUI

如果有自定义的 Serializable 类,可以使用__属性绘制器__来控制该类在 Inspector 中的外观。请参考以下脚本示例中的 Serializable 类 Ingredient(注意:这些不是编辑器脚本。属性特性类应放在常规脚本文件中):

C#(示例)

using System;
using UnityEngine;

enum IngredientUnit { Spoon, Cup, Bowl, Piece }

// Custom serializable class
[Serializable]
public class Ingredient
{
    public string name;
    public int amount = 1;
    public IngredientUnit unit;
}

public class Recipe : MonoBehaviour
{
    public Ingredient potionResult;
    public Ingredient[] potionIngredients;
}

可以使用自定义属性绘制器来更改 Inspector 中 Ingredient 类的每个外观。比较不带有和带有自定义属性绘制器的 Inspector 中 Ingredient 属性的外观:

不带有(左)和带有(右)自定义属性绘制器的 Inspector 中的类。
不带有(左)和带有(右)自定义属性绘制器的 Inspector 中的类。

可以使用 CustomPropertyDrawer 属性将属性绘制器附加到 Serializable 类,然后传到属性绘制器所针对的 Serializable 类的类型。

C#(示例)

using UnityEditor;
using UnityEngine;

// IngredientDrawer
[CustomPropertyDrawer(typeof(Ingredient))]
public class IngredientDrawer : PropertyDrawer
{
    // Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty(position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        var amountRect = new Rect(position.x, position.y, 30, position.height);
        var unitRect = new Rect(position.x + 35, position.y, 50, position.height);
        var nameRect = new Rect(position.x + 90, position.y, position.width - 90, position.height);

        // Draw fields - pass GUIContent.none to each so they are drawn without labels
        EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("amount"), GUIContent.none);
        EditorGUI.PropertyField(unitRect, property.FindPropertyRelative("unit"), GUIContent.none);
        EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty();
    }
}

使用属性特性来自定义脚本成员的 GUI

属性绘制器__的另一用途是更改脚本中具有自定义__属性特性__的成员的外观。假设要将脚本中的浮点数或整数限制在特定范围内,并在 Inspector__ 中将其显示为滑动条。那么,可以使用内置的 PropertyAttribute__(称为 RangeAttribute__)来执行此操作:

C#(示例)

// Show this float in the Inspector as a slider between 0 and 10
[Range(0f, 10f)]
float myFloat = 0f;

还可以创建自己的 PropertyAttribute。我们以 RangeAttribute 的代码为例。该属性必须扩展 PropertyAttribute 类。如果需要,属性可以使用参数并将它们存储为公共成员变量。

C#(示例)

using UnityEngine;

public class MyRangeAttribute : PropertyAttribute 
{
        readonly float min;
        readonly float max;
        
        void MyRangeAttribute(float min, float max)
        {
            this.min = min;
            this.max = max;
        }
}

拥有该特性之后,就需要创建一个__属性绘制器__来绘制具有该特性的属性。该绘制器必须扩展 PropertyDrawer 类,且必须具有 CustomPropertyDrawer 特性来说明绘制器所针对的特性。

属性绘制器类应放在编辑器脚本中,该脚本位于称为 Editor 的文件夹内。

C#(示例)

using UnityEditor;
using UnityEngine;

// Tell the MyRangeDrawer that it is a drawer for properties with the MyRangeAttribute.
[CustomPropertyDrawer(typeof(MyRangeAttribute))]
public class RangeDrawer : PropertyDrawer
{
    // Draw the property inside the given rect
    void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // First get the attribute since it contains the range for the slider
        MyRangeAttribute range = (MyRangeAttribute)attribute;

        // Now draw the property as a Slider or an IntSlider based on whether it's a float or integer.
        if (property.propertyType == SerializedPropertyType.Float)
            EditorGUI.Slider(position, property, range.min, range.max, label);
        else if (property.propertyType == SerializedPropertyType.Integer)
            EditorGUI.IntSlider(position, property, (int) range.min, (int) range.max, label);
        else
            EditorGUI.LabelField(position, label.text, "Use MyRange with float or int.");
    }
}

请注意,出于性能原因,EditorGUILayout 函数不能用于属性绘制器。

默认对象引用

如果定义公共对象字段或使用 SerializeField 标记的私有字段,则可以为这些字段设置默认引用。在项目窗口中选择脚本资源时,Inspector 窗口中会显示默认引用字段。

注意:建议将每个属性绘制器都保存在自己的文件中,并使用匹配的名称。这样可确保有效分配默认的对象引用,因其只能分配给单个属性绘制器。当同一文件中存在多种类型时,所指定的类型要么与文件名匹配,要么是该文件中定义的第一个类型。

使用 IMGUI 创建自定义Editor窗口
使用 IMGUI 创建自定义编辑器