Version: 6000.3
语言: 中文
在绑定属性更改时接收回调
使用 ListView 绑定到列表

当任何绑定属性发生变化时接收回调

版本: 2021.3+

这些示例演示了如何在绑定序列化对象的任何属性发生更改时接收回调。

示例概述

此示例创建自定义检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
请参阅术语表
有两个字段。如果字段的值超出特定范围,它会警告用户。

在 Unity 编辑器中显示文本警告的自定义检查器。
在 Unity 编辑器中显示文本警告的自定义检查器。

您可以在此 GitHub 存储库中找到该示例的已完成文件。

先决条件

本指南适用于熟悉 Unity 编辑器、UI 工具包和 C# 脚本的开发人员。在开始之前,请熟悉以下内容:

创建武器对象

创建 C# 脚本来定义类Weapon作为MonoBehaviour具有两个属性:m_BaseDamagem_HardModeModifier.

  1. 使用任何模板在 Unity 中创建项目。

  2. 在您的项目窗口一个窗口,显示您的内容Assets文件夹(项目选项卡)更多信息
    术语表中查看
    ,创建一个名为callback-any-SerializedProperty-changes以存储您的文件。

  3. 创建名为Weapon.cs并将其内容替换为以下内容:

    using UnityEngine;
    
    namespace UIToolkitExamples
    {
        public class Weapon : MonoBehaviour
        {
            public const float maxDamage = 9999f;
    
            [SerializeField]
            float m_BaseDamage;
    
            [SerializeField]
            float m_HardModeModifier;
    
            public float GetDamage(bool hardMode)
            {
                return hardMode ? m_BaseDamage * m_HardModeModifier : m_BaseDamage;
            }
        }
    }
    

创建绑定以接收回调

创建一个 C# 脚本,用于定义自定义 InspectorWeapon并使用TrackSerializedObjectValue()检查m_BaseDamagem_HardModeModifier性能。

  1. callback-any-SerializedProperty-changes 文件夹中,创建一个名为Editor.

  2. Editor 文件夹中,创建一个名为WeaponCustomEditor.cs并将其内容替换为以下内容:

    using UnityEngine;
    using UnityEditor;
    using UnityEngine.UIElements;
    using UnityEditor.UIElements;
    
    namespace UIToolkitExamples
    {
        [CustomEditor(typeof(Weapon))]
        public class WeaponCustomEditor : Editor
        {
            // This is text used for the warning labels.
            const string k_NegativeWarningText =
                "This weapon has a negative final damage on at least 1 difficulty level.";
            static readonly string k_DamageCapWarningText =
                "This weapon has an excessive final damage that is capped to " + Weapon.maxDamage +
                " on at least 1 difficulty level.";
    
            // These are labels to warn users about negative damage and excessive damage.
            Label m_NegativeWarning, m_DamageCapWarning;
    
            public override VisualElement CreateInspectorGUI()
            {
                VisualElement root = new();
    
                // Create FloatFields for serialized properties.
                var baseDamageField = new FloatField("Base Damage") { bindingPath = "m_BaseDamage" };
                var modifierField = new FloatField("Hard Mode Modifier") { bindingPath = "m_HardModeModifier" };
                root.Add(baseDamageField);
                root.Add(modifierField);
    
                // Create warning labels and style them so they stand out.
                var warnings = new VisualElement();
                m_NegativeWarning = new(k_NegativeWarningText);
                m_DamageCapWarning = new(k_DamageCapWarningText);
                warnings.style.color = Color.red;
                warnings.style.unityFontStyleAndWeight = FontStyle.Bold;
                warnings.Add(m_NegativeWarning);
                warnings.Add(m_DamageCapWarning);
                root.Add(warnings);
    
                // Determine whether to show the warnings at the start.
                CheckForWarnings(serializedObject);
    
                // Whenever any serialized property on this serialized object changes its value, call CheckForWarnings.
                root.TrackSerializedObjectValue(serializedObject, CheckForWarnings);
    
                return root;
            }
    
            // Check the current values of the serialized properties to either display or hide the warnings.
            void CheckForWarnings(SerializedObject serializedObject)
            {
                // For each possible damage values of the weapon, determine whether it's negative and whether it's above the
                // maximum damage value.
                var weapon = serializedObject.targetObject as Weapon;
                var damages = new float[] { weapon.GetDamage(true), weapon.GetDamage(false) };
                var foundNegativeDamage = false;
                var foundCappedDamage = false;
                foreach (var damage in damages)
                {
                    foundNegativeDamage = foundNegativeDamage || damage < 0;
                    foundCappedDamage = foundCappedDamage || damage > Weapon.maxDamage;
                }
    
                // Display or hide warnings depending on the values of the damages.
                m_NegativeWarning.style.display = foundNegativeDamage ? DisplayStyle.Flex : DisplayStyle.None;
                m_DamageCapWarning.style.display = foundCappedDamage ? DisplayStyle.Flex : DisplayStyle.None;
            }
        }
    }
    

测试绑定

  1. 创建一个空的游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
    请参阅术语表
    场景场景包含游戏的环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,你放置你的环境、障碍物和装饰品,基本上是将你的游戏设计和构建成碎片。更多信息
    请参阅术语表
    .

  2. 选择游戏对象。

  3. 在检查器中添加 Weapon 组件。

  4. Weapon 组件中,更改字段中的值:

    • 如果基础伤害基础伤害乘以困难模式修饰值为负数,则会出现一条警告消息。
    • 如果基础伤害基础伤害乘以困难模式修正大于 9999,则会出现不同的警告消息。

其他资源

在绑定属性更改时接收回调
使用 ListView 绑定到列表