Version: 6000.3
语言: 中文
使用 IMGUI 创建自定义编辑器窗口
使用 IMGUI 创建自定义编辑器

将属性抽屉与 IMGUI 一起使用来自定义检查器

注意:强烈建议使用 UI 工具包来扩展 Unity 编辑器,因为它提供了比 IMGUI 更现代、更灵活、更可扩展的解决方案。

属性抽屉可用于通过使用脚本一段代码,允许您创建自己的组件、触发游戏事件、随时间修改组件属性以及以您喜欢的任何方式响应用户输入。更多信息
请参阅术语表
,或者通过控制特定的Serializable类应该看起来。

属性抽屉有两个用途:

  • 自定义 Serializable 类的每个实例的 GUI。
  • 使用自定义属性属性自定义脚本成员的 GUI。

自定义 Serializable 类的 GUI

如果你有一个自定义的 Serializable 类,你可以使用 Property Drawer 来控制它在检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
请参阅术语表
.考虑以下脚本示例中的 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 类的每个外观。比较不使用和使用自定义属性抽屉的检查器中成分属性的外观:

不带(左)和(右)自定义属性抽屉的检查器中的类。
不带(左)和(右)自定义属性抽屉的检查器中的类。

可以使用 CustomPropertyDrawer 属性将 Property Drawer 附加到 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

属性抽取器的另一个用途是更改脚本中具有自定义属性属性的成员的外观。假设您要将脚本中的浮点数或整数限制在特定范围内,并在检查器中将它们显示为滑块。使用名为 RangeAttribute 的内置 PropertyAttribute,您可以做到这一点:

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 函数不能与属性抽屉一起使用。

默认对象引用

如果将 public 定义为对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
请参阅术语表
字段或标有 SerializeField 的私有字段,您可以为这些字段设置默认引用。当您在项目窗口一个窗口,显示您的内容Assets文件夹(项目选项卡)更多信息
术语表中查看
.

注意:建议将每个 PropertyDrawer 维护在自己的文件中,并具有匹配的名称。这确保了默认对象引用的有效分配,因为它们只能分配给单个 PropertyDrawer。当同一文件中存在多个类型时,分配的类型将与文件名匹配,或者将是文件中定义的第一个类型。

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