前言:
在使用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;
}
六、ExecuteInEditMode