C#探索之路基础夯实篇(6):C#在Unity中的自定义特性

文章目录

C#探索之路基础夯实篇(6):C#中的自定义特性

1、概念辨析

自定义特性(Attribute)是一种可以附加到类、字段、方法等各种元素上的标记,用于在编译时或运行时提供额外的信息或功能。它们是 C# 语言中的一种元编程特性,允许开发者通过声明性的方式为代码添加元数据。

2、应用范围

  • 数据验证与约束: 可以用于验证和约束数据的有效性,如范围检查、非空检查等。
  • 编辑器扩展: 可以用于增强Unity编辑器的功能,如自定义面板显示、自定义菜单项等。
  • 自定义序列化与显示: 可以用于自定义字段的序列化与显示方式,如指定字段在Inspector面板中的显示名称等。

3、代码案例说明

  • 数据验证与约束示例:

    csharp 复制代码
    using UnityEngine;
    
    // 自定义特性用于标记非空字段
    public class NotEmptyAttribute : PropertyAttribute
    {
        // 用于标记字段是否允许为空
    }
    
    public class Player : MonoBehaviour
    {
        [NotEmpty] // 使用自定义特性
        public string playerName;
    
        void Start()
        {
            // 验证字段是否为空
            if (string.IsNullOrEmpty(playerName))
            {
                Debug.LogError("Player name cannot be empty!");
            }
            else
            {
                Debug.Log("Player name: " + playerName);
            }
        }
    }
  • 编辑器扩展示例:

    csharp 复制代码
    using UnityEngine;
    using UnityEditor;
    
    // 自定义特性用于在编辑器中显示调试按钮
    public class DebugButtonAttribute : PropertyAttribute
    {
        public string methodName;
    
        public DebugButtonAttribute(string methodName)
        {
            this.methodName = methodName;
        }
    }
    
    [CustomEditor(typeof(TestScript))]
    public class TestScriptEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
    
            TestScript testScript = (TestScript)target;
    
            // 根据特性在 Inspector 面板中创建按钮
            if (GUILayout.Button("Debug Log"))
            {
                testScript.DebugLog();
            }
        }
    }
    
    public class TestScript : MonoBehaviour
    {
        [DebugButton("DebugLog")] // 使用自定义特性
        private Button debugButton;
        
        private void DebugLog()
        {
            Debug.Log("Debug Log button clicked!");
        }
    }
  • 自定义序列化与显示示例:

    csharp 复制代码
    using UnityEngine;
    
    // 自定义特性用于指定字段在Inspector面板中的显示名称
    public class DisplayNameAttribute : PropertyAttribute
    {
        public string displayName;
    
        public DisplayNameAttribute(string displayName)
        {
            this.displayName = displayName;
        }
    }
    
    public class PlayerData : MonoBehaviour
    {
        [DisplayName("Player Name")] // 使用自定义特性
        public string playerName;
    
        [DisplayName("Player Age")] // 使用自定义特性
        public int playerAge;
    }
  • 自定义特性范围检查

c# 复制代码
using UnityEngine;

// 自定义特性用于标记范围检查
public class RangeCheckAttribute : PropertyAttribute
{
    public int min;
    public int max;

    public RangeCheckAttribute(int min, int max)
    {
        this.min = min;
        this.max = max;
    }
}

#if UNITY_EDITOR
using UnityEditor;

// 自定义编辑器用于绘制带有范围检查特性的字段
[CustomPropertyDrawer(typeof(RangeCheckAttribute))]
public class RangeCheckDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        RangeCheckAttribute range = attribute as RangeCheckAttribute;

        EditorGUI.BeginProperty(position, label, property);

        // 绘制滑块
        property.intValue = EditorGUI.IntSlider(position, label, property.intValue, range.min, range.max);

        EditorGUI.EndProperty();
    }
}
#endif

public class Player : MonoBehaviour
{
    [RangeCheck(0, 100)] // 使用自定义特性
    public int health = 50;

    void Start()
    {
        // 输出玩家健康值
        Debug.Log("Player health: " + health);
    }
}

在这个示例中,我们使用了自定义特性 RangeCheckAttribute 来标记范围检查,并且编写了一个名为 RangeCheckDrawer 的自定义编辑器脚本,用于在 Unity 编辑器中绘制带有范围检查特性的字段。这样,在 Unity 编辑器中查看 Player 组件时,health 字段旁边会出现一个滑块,可以直接调整健康值,并确保其在指定的范围内。

4、适用范围

何时使用自定义特性比较合适?

  • 当需要为代码添加额外的元数据,以提供额外的信息或功能时,如编辑器扩展、数据验证、自定义序列化与显示等场景。
  • 当希望通过声明性的方式为代码添加元数据,以增强代码的可读性和可维护性时。
  • 当需要在特定的上下文中使用自定义元数据时,如Unity编辑器中的Inspector面板或菜单项中。
相关推荐
rockey6271 小时前
基于AScript的Lua脚本语言发布啦
c#·.net·lua·script·动态脚本
半亩码田3 小时前
【.NET新特性·第11篇】C# 14 字段属性:告别手写 backing field
java·c#·.net
曹牧4 小时前
C#:问号
前端·数据库·c#
少控科技4 小时前
农场设备管理代码(2)
开发语言·c#
玖玥拾5 小时前
Unity3D RPG 入门项目(四)背包拖拽、物品悬浮提示、项目开发思维
3d·unity·游戏引擎
唐青枫6 小时前
别再把命令行参数写成一团:C#.NET System.CommandLine 实战详解
c#·.net
hez20106 小时前
让 .NET 11 的泛型虚方法变得更快!
c#·.net·.net core·clr·compiler
宝桥南山7 小时前
Microsoft Agent Framework(.NET) - 尝试一下从MCP Server上获取Agent Skills
ai·微软·c#·aigc·.net·.netcore
少控科技9 小时前
农场设备管理代码(1)
开发语言·c#
xcLeigh9 小时前
Unity基础:Start与Update方法——Unity脚本生命周期初探
java·unity·游戏引擎·教程