包含此页的版本:
不含此页的版本:
版本: 2022.3+
虽然有一个默认的检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
请参阅术语表对于您的 MonoBehaviours 和 ScriptableObjects,您可能需要为类编写一个自定义 Inspector。自定义检查器可以帮助您执行以下作:
此示例为 MonoBehaviour 类创建自定义 Inspector。它同时使用 C#脚本一段代码,允许您创建自己的组件、触发游戏事件、随时间修改组件属性以及以您喜欢的任何方式响应用户输入。更多信息
请参阅术语表和 UI Builder 创建 UI。自定义检查器还具有自定义属性抽屉一种 Unity 功能,允许你通过使用脚本上的属性或控制特定 Serializable 类的外观来自定义检查器窗口中某些控件的外观 更多信息
请参阅术语表.
自定义检查器显示Car类,包括品牌、制造年份、颜色和轮胎列表。Inspector 使用 PropertyField 控件来显示Car类,以及一个自定义属性抽屉,用于显示Tire类。
您可以在此 GitHub 存储库中找到此示例创建的已完成文件。
本指南适用于熟悉 Unity 编辑器、UI 工具包和 C# 脚本的开发人员。在开始之前,请熟悉以下内容:
要创建自定义检查器,请首先定义一个继承自MonoBehaviour.自定义类表示简单的car具有多个属性。
使用任何模板在 Unity 中创建项目。
在您的项目窗口一个窗口,显示您的内容Assets文件夹(项目选项卡)更多信息
在术语表中查看,创建一个名为create-a-custom-inspector以存储您的所有文件。
创建名为Car.cs内容如下:
using UnityEngine;
public class Car : MonoBehaviour
{
public string m_Make = "Toyota";
public int m_YearBuilt = 1980;
public Color m_Color = Color.black;
}
创建一个新的游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
请参阅术语表在场景场景包含游戏的环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,你放置你的环境、障碍物和装饰品,基本上是将你的游戏设计和构建成碎片。更多信息
请参阅术语表并附加Carscript 组件添加到它。
要为任何序列化对象创建自定义检查器,您需要创建一个派生自 Editor 基类的类,并向其添加 CustomEditor 属性。此属性可让 Unity 知道此自定义检查器表示哪个类。
注意:自定义 Inspector 脚本必须位于Editor文件夹或仅编辑器程序集定义。这是因为UnityEditor命名空间对于创建自定义检查器至关重要,但在这些区域之外无法访问。如果尝试创建独立构建而不遵守此规定,则构建过程将失败。
创建一个名为Editor在create-a-custom-inspector文件夹。
创建名为Car_Inspector.cs在Editor文件夹,其中包含以下内容:
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
[CustomEditor(typeof(Car))]
public class Car_Inspector : Editor
{
}
选择具有Car附加到它的组件。将显示默认的 Inspector。
要替换默认的 Inspector,请在Car_Inspector类,添加以下代码以覆盖 CreateInspectorGUI() 并返回一个新的视觉元素实例化或派生自 C# 的可视化树的节点VisualElement类。您可以设置外观样式、定义行为并将其作为 UI 的一部分显示在屏幕上。更多信息
请参阅术语表包含 UI:
public override VisualElement CreateInspectorGUI()
{
// Create a new VisualElement to be the root of our Inspector UI.
VisualElement myInspector = new VisualElement();
// Add a simple label.
myInspector.Add(new Label("This is a custom Inspector"));
// Return the finished Inspector UI.
return myInspector;
}
选择具有Car附加到它的组件。检查器现在显示标签This is a custom Inspector而不是默认的检查器。
你可以使用 UI Builder 创建自定义 Inspector UI,并使用 C# 脚本从 UXML 文件加载和实例化 UI。
要打开 UI 生成器,请选择窗口> UI 工具包> UI 生成器。
选择 文件(File) > 新建(New) 以创建新的可视化树资产。
要在 UI Builder 中启用仅编辑器控件,请选择<unsaved file>*.uxml,然后选中“编辑器扩展创作”复选框。
将标签控件从“库”拖动到“层次结构”。这会将标签控件添加到可视化树。
在标签控件的“检查器”面板中,更新标签文本。
选择“文件”>“保存”,并将可视化树另存为Car_Inspector_UXML.uxml到Assets/create-a-custom-inspector文件夹。
要使用你在自定义检查器中创建的UXML文件,请将该文件分配给自定义检查器,然后在CreateInspectorGUI()函数并将其添加到可视化树中。为此,您可以使用 CloneTree 方法。您可以传递任何VisualElement作为参数充当已创建元素的父元素。
在Car_Inspector.cs,为VisualTreeAsset并在脚本中分配Car_Inspector_UXML.uxml到它。
public VisualTreeAsset m_InspectorUXML;
更新CreateInspectorGUI()方法加载 UXML 文件并将其克隆到可视化树中。完成的Car_Inspector.cs文件如下所示:
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
[CustomEditor(typeof(Car))]
public class Car_Inspector : Editor
{
public VisualTreeAsset m_InspectorUXML;
public override VisualElement CreateInspectorGUI()
{
// Create a new VisualElement to be the root of our Inspector UI.
VisualElement myInspector = new VisualElement();
// Add a simple label.
myInspector.Add(new Label("This is a custom Inspector"));
// Load the UXML file and clone its tree into the inspector.
if (m_InspectorUXML != null)
{
VisualElement uxmlContent = m_InspectorUXML.CloneTree();
myInspector.Add(uxmlContent);
}
// Return the finished Inspector UI.
return myInspector;
}
}
在 的 Inspector 窗口中Car_Inspector.cs,将Car_Inspector_UXML.uxml文件添加到 Inspector UXML 字段。
选择具有Car附加到它的组件。检查器Car组件现在显示两个标签:一个通过脚本,一个通过UI Builder/UXML。
此自定义检查器显示Car类。更新 UI 控件时,实例中的值Car班级变化。为此,请将 UI 控件添加到可视化树,并将它们绑定到类的各个属性。
若要将控件绑定到序列化属性,请将该属性分配给binding-path控件的字段。创建自定义检查器时,绑定是自动的。CreateInspectorGUI()在返回可视化树后执行隐式绑定。有关详细信息,请参阅 SerializedObject 数据绑定。
双击Car_Inspector_UXML.uxml在 UI Builder 中打开它。
添加一个TextField 控件TextField 控件向用户显示一段非交互式文本,例如标题、其他 GUI 控件的标签或说明。更多信息
请参阅术语表.
在 TextField 的 Inspector 面板中,将标签文本设置为Make of the car.
将绑定路径设置为m_Make.
添加样式类unity-base-field__aligned到“样式类列表”,以便文本字段与“检查器”窗口中的其他字段对齐。有关详细信息,请参阅 BaseField。
在 UI Builder 中,选择“文件”>“保存”。
选择具有Car附加到它的组件。检查器car组件现在显示Make of the cartext 字段。文本字段绑定到m_Make属性的Car类。
要显示Car类,为每个字段添加一个控件。控件必须与属性类型匹配。例如,您必须绑定int设置为“整数”字段或“整数滑块”。
您可以使用泛型 PropertyField 控件,而不是根据属性类型添加特定控件。此控件适用于大多数类型的序列化属性,并为此属性类型生成默认的检查器 UI。
的优点PropertyField是 Inspector UI 在您更改脚本中的变量类型时自动调整。但是,无法在 UI 生成器中预览控件,因为在可视化树绑定到序列化对象之前,控件类型是未知的。
双击Car_Inspector_UXML.uxml在 UI Builder 中打开它。
为m_YearBuilt属性Car类,并设置绑定路径和标签文本。
添加样式类unity-base-field__aligned到样式类列表。
为m_Color属性Car类,并设置绑定路径和标签文本。
添加样式类unity-base-field__aligned到样式类列表。
在 UI Builder 中,选择“文件”>“保存”。
选择具有Car组件。检查器car组件现在显示Year Built和Paint Color属性字段。
自定义属性抽取器是自定义可序列化类的自定义检查器 UI。如果该可序列化类是另一个序列化对象的一部分,则自定义 UI 会在检查器中显示该属性。在 UI Toolkit 中,PropertyField控件显示字段的自定义属性抽取盒(如果存在)。
在create-a-custom-inspector文件夹中,创建一个名为Tire.cs内容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Tire
{
public float m_AirPressure = 21.5f;
public int m_ProfileDepth = 4;
}
在Car.cs,添加列表Tire到Car类。完成的Car.cs文件如下所示:
using UnityEngine;
public class Car : MonoBehaviour
{
public string m_Make = "Toyota";
public int m_YearBuilt = 1980;
public Color m_Color = Color.black;
// This car has four tires.
public Tire[] m_Tires = new Tire[4];
}
PropertyField 控件适用于所有标准属性类型,还支持自定义可序列化类和数组。要显示汽车轮胎的属性,请添加另一个PropertyField在Car_Inspector_UXML.uxml并将其绑定到m_Tires.完成的Car_Inspector_UXML.uxml文件如下所示:
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="True">
<ui:TextField picking-mode="Ignore" label="Make of the car" value="filler text" binding-path="m_Make" class="unity-base-field__aligned" />
<uie:PropertyField binding-path="m_YearBuilt" label="Year Built" class="unity-base-field__aligned" />
<uie:PropertyField binding-path="m_Color" label="Paint Color" class="unity-base-field__aligned" />
<uie:PropertyField label="Tires" binding-path="m_Tires" class="unity-base-field__aligned" />
</ui:UXML>
选择带有Car元件。检查器car组件现在显示Tiresproperty 字段。
您可以创建自定义属性抽屉来自定义单个的外观Tire元素。而不是派生自Editor基类,自定义属性抽屉派生自PropertyDrawer类。
你可以使用C#脚本或UXML为属性创建UI。此示例使用 C# 脚本创建自定义 UI。若要为自定义属性创建 UI,请重写 CreatePropertyGUI 方法。
在Editor文件夹中,创建一个名为Tire_PropertyDrawer.cs内容如下:
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
[CustomPropertyDrawer(typeof(Tire))]
public class Tire_PropertyDrawer : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
// Create drawer UI using C#.
var popup = new UnityEngine.UIElements.PopupWindow();
popup.text = "Tire Details";
popup.Add(new PropertyField(property.FindPropertyRelative("m_AirPressure"), "Air Pressure (psi)"));
popup.Add(new PropertyField(property.FindPropertyRelative("m_ProfileDepth"), "Profile Depth (mm)"));
// Return the finished UI.
return popup;
}
}
选择具有Car组件。检查器car组件现在显示Tires属性字段,其中包含自定义属性抽屉。