欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > C#探索之路基础夯实篇(6):C#在Unity中的自定义特性

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

2025/11/8 1:13:50 来源:https://blog.csdn.net/qq_43801020/article/details/143222653  浏览:    关键词:C#探索之路基础夯实篇(6):C#在Unity中的自定义特性

文章目录

  • C#探索之路基础夯实篇(6):C#中的自定义特性
    • 1、概念辨析
    • 2、应用范围
    • 3、代码案例说明
    • 4、适用范围

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

1、概念辨析

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

2、应用范围

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

3、代码案例说明

  • 数据验证与约束示例:

    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);}}
    }
    
  • 编辑器扩展示例:

    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!");}
    }
    
  • 自定义序列化与显示示例:

    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;
    }
    
  • 自定义特性范围检查

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();}
}
#endifpublic 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面板或菜单项中。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com