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面板或菜单项中。
相关推荐
电商api接口开发10 小时前
ASP.NET MVC 入门指南
c#·asp.net·mvc
我不是程序猿儿11 小时前
[C#]反射的实战应用,实际数据模拟
开发语言·c#
爱编程的鱼12 小时前
C# 结构(Struct)
开发语言·人工智能·算法·c#
是阿根12 小时前
unity使用iTextSharp生成PDF文件
unity·c#·游戏引擎
ABAP 成13 小时前
.NET Framework 4.0可用EXCEL导入至DataTable
c#
WineMonk16 小时前
C#多线程访问资源
c#
Bardb16 小时前
04-stm32的标准外设库
stm32·c#
风,停下16 小时前
C#基于Sunnyui框架和MVC模式实现用户登录管理
设计模式·c#·mvc
钢铁男儿16 小时前
C# 实战_RichTextBox选中某一行条目高亮,离开恢复
开发语言·c#
千叶真尹19 小时前
【无标题】
c#·linq