Unity中常用的Attribute

本文详细介绍了Unity3D中常见的Attribute,包括HelpURLAttribute、RangeAttribute、RequireComponentAttribute、TooltipAttribute、HideInInspectorAttribute等,涵盖了从组件交互到Inspector面板显示的各种特性,帮助开发者更好地理解和利用Unity的元数据功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:
在使用Unity3D的过程中,会经常用到许多的特性Attribute,我基本上把官方文档里列出的都记录下来,不定期更新

一、HelpURLAttribute
从字面意思理解,是查看帮助时,跳转到指定的页面。

如下图:

对应着蓝色小书的图标,点击以后会跳转到配置的URL。

二、RangeAttribute:限定int或float的取值范围。

Attribute used to make a float or int variable in a script be restricted to a specific range.
When this attribute is used, the float or int will be shown as a slider in the Inspector instead of the default number field.

当在int或float上应用RangeAttribute特性时,在Inspector面板中,显示的将是一个slider滑动条,而不是默认的数值字段。

[Range(0.1f,0.9f)]
float ratio;

三、RequireComponentAttribute

自动添加所要依赖的组件,如将一个Script做为一个GameObject的组件,而这个Script需要访问Rigidbody组件,
通过应用该属性,可以自动的添加Rigidbody组件到当前的GameObject中,避免设置错误。

*前提是:如果当前的Script已经添加到了GameObject,这时候你再应用RequireComponent特性是无效的,
你必须删除 再重新添加,才会检测。

using UnityEngine;

// PlayerScript requires the GameObject to have a Rigidbody component
[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{
    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        rb.AddForce(Vector3.up);
    }
}

四、TooltipAttribute
在Inspector面板中,为一个字段Field指定一个提示。

image.png

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    [Tooltip("Health value between 0 and 100.")]
    public int health = 0;
}

五、HideInInspectorAttribute

让一个可被序列化的字段,不要显示在Inspector面板中,防止修改。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    [HideInInspector]
    public int p = 5;
}

六、ExecuteInE

### 创建和使用自定义属性 在 Unity 中,通过继承 `PropertyAttribute` 类并应用 `[AttributeUsage]` 特性来创建自定义属性。这允许开发者指定新特性可应用于哪些程序元素。 对于字段级别的自定义属性,代码如下所示: ```csharp using System; using UnityEngine; [AttributeUsage(AttributeTargets.Field)] public class SceneName : PropertyAttribute { } ``` 此段代码声明了一个名为 `SceneName` 的简单自定义属性[^2]。然而,在这个阶段,它仅作为标记存在,并不会影响 Inspector 行为或提供额外功能。 为了使自定义属性生效,还需要编写相应的编辑器脚本来处理带有特定属性的字段。通常涉及实现 `CustomPropertyDrawer` 或者直接操作 `SerializedObject` 和 `SerializedProperty` API 来控制显示逻辑。 下面是一个简单的例子展示如何绘制带有所述 `SceneName` 属性的字符串字段: ```csharp using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(SceneName))] public class SceneNameDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // 只针对 string 类型有效 if (property.propertyType != SerializedPropertyType.String) { EditorGUI.LabelField(position, label.text, "Only works on strings!"); return; } // 获取当前场景名称列表 var scenes = EditorBuildSettings.scenes; string[] options = new string[scenes.Length]; for(int i=0;i<scenes.Length;++i){ options[i]=System.IO.Path.GetFileNameWithoutExtension(scenes[i].path); } // 绘制下拉框控件 int selectedIndex = Mathf.Max(0,System.Array.IndexOf(options, property.stringValue)); selectedIndex = EditorGUI.Popup(position,label.text,options,selectedIndex); // 更新目标对象上的值 property.stringValue = options[selectedIndex]; } } ``` 这段代码实现了对具有 `SceneName` 自定义特性的字段进行特殊渲染的功能——即从项目设置中的构建配置读取可用场景名,并呈现成一个方便的选择界面给用户交互。 当把以上两个组件结合起来时,便可以在 Unity 编辑器内看到更加友好直观的操作体验;同时保持了 C# 语法糖带来的灵活性与简洁度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值