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面板或菜单项中。
相关推荐
数据皮皮侠AI36 分钟前
中国城市可再生能源数据集(2005-2021)|顶刊 Sci Data 11 种能源面板
大数据·人工智能·笔记·能源·1024程序员节
Eiceblue1 小时前
使用 C# 将 Excel 转换为 Markdown 表格(含批量转换示例)
开发语言·c#·excel
天人合一peng2 小时前
unity 生成标记根据背景色变色为明显的颜色
unity·游戏引擎
魔士于安2 小时前
Unity 超市总动员 超市收银台 超市货架 超市购物手推车 超市常见商品
游戏·unity·游戏引擎·贴图·模型
CandyU22 小时前
Unity —— 数据持久化
unity·游戏引擎
zh路西法2 小时前
【Unity实现Oneshot胶卷显形】游戏窗口化与Win32API的使用
游戏·unity·游戏引擎
不会编程的懒洋洋4 小时前
WPF XAML+布局+控件
xml·开发语言·c#·视觉检测·wpf·机器视觉·视图
唐青枫4 小时前
别再层层传参了!C#.NET AsyncLocal 异步上下文透传实战
c#·.net
明如正午5 小时前
【C#】托管调试助手 “PInvokeStackImbalance“:的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。
c#
Eiceblue5 小时前
C# 如何实现 Word 转 Excel ?分享两种实用方法
c#·word·excel