Unity3D :在任何绑定属性更改时接收回调

推荐:将NSDT场景编辑器就加入你的3D工具链
3D工具集:NSDT简石数字孪生

在任何绑定属性更改时接收回调

版本: 2021.3+

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

示例概述

此示例创建一个自定义检查员
有两个字段。如果字段的值超出特定范围,它会警告用户。

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

先决条件

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

  • TrackSerializedObjectValue()

创建武器对象

创建一个 C# 脚本,将类定义为具有两个属性的 :和 。WeaponMonoBehaviourm_BaseDamagem_HardModeModifier

  1. 使用任何模板在 Unity 中创建项目。
  2. 在你的项目窗口
    ,创建一个名为用于存储文件的文件夹。callback-any-SerializedProperty-changes

创建一个名为的 C# 脚本,并将其内容替换为以下内容: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# 脚本,该脚本定义 的自定义检查器,并使用该方法检查 和 属性中的更改。WeaponTrackSerializedObjectValue()m_BaseDamagem_HardModeModifier

  1. 回调任意序列化属性更改文件夹中,创建一个名为 的文件夹。Editor

“编辑器”文件夹中,创建一个名为的 C# 脚本,并将其内容替换为以下内容: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. 创建一个空的游戏对象
    在一个现场
    .
  2. 选择游戏对象。
  3. 在检查器中添加武器组件。

武器组件中,更改字段中的值:

  • 如果基础伤害或基础伤害乘以困难模式修饰符为负数,则会显示警告消息。
  • 如果基础伤害或基础伤害乘以困难模式修改器大于 9999,则会显示不同的警告消息。

3D建模学习工作室整理翻译,转载请注明出处!

上一篇:Unity3D :绑定属性更改时接收回调 (mvrlink.com)

下一篇:Unity3D :使用列表视图绑定到列表 (mvrlink.com)

NSDT场景编辑器 | NSDT 数字孪生 | GLTF在线编辑器 | 3D模型在线转换 | UnrealSynth虚幻合成数据生成器 | 3D模型自动纹理化工具
2023 power by nsdt©鄂ICP备2023000829号