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面板或菜单项中。
相关推荐
zybsjn32 分钟前
后端系统做国际化改造,生成多语言包
java·python·c#
敲代码的 蜡笔小新1 小时前
【行为型之迭代器模式】游戏开发实战——Unity高效集合遍历与场景管理的架构精髓
unity·设计模式·c#·迭代器模式
yc_12242 小时前
SqlHelper 实现类,支持多数据库,提供异步操作、自动重试、事务、存储过程、分页、缓存等功能。
数据库·c#
Kookoos2 小时前
Redis + ABP vNext 构建分布式高可用缓存架构
redis·分布式·缓存·架构·c#·.net
Zhen (Evan) Wang4 小时前
ABP-Book Store Application中文讲解 - Part 2: The Book List Page
c#
小乖兽技术6 小时前
在 .NET 8 开发的WinForms 程序中展示程序版本号的几种方式
开发语言·c#·.net
TheWindofFate6 小时前
C# 基础 try-catch代码块
c#·try-catch
TIF星空10 小时前
【使用 C# 获取 USB 设备信息及进行通信】
开发语言·经验分享·笔记·学习·microsoft·c#
csdn_aspnet12 小时前
如何在 C# 中自定义 Datagridview 标题
c#·winform·datagridview
津津有味道12 小时前
MIFARE DESFire Light 卡C#读写更改卡片密钥源码
c#·light·desfire·ev2