内容取自官方API文档特性说明部分,用于开发参考,辅助开发提升开发效率;特性(Attribute)作用于脚本中的类、变量或方法的上,用[ ]包裹,用于表明其特殊行为,如 [HideInInspector],使变量在 Inspector 面板隐藏;
部分特性还未使用过,且网上资料很少,所以欠缺解释,后期补足
本文原地址:Unity手册—Attribute汇总说明
引用版本:Unity 官方手册 ver. 2020.1 UnityEngine API文档 UnityEditor API文档 .NET特性文档
常用特性
此处列出常用特性,功能说明在下方汇总
菜单 Menu
- AddComponentMenu
- MenuItem
- CreateAssetMenuAttribute
监视器 Inspector
- HeaderAttribute
- HideInInspector
- InspectorNameAttribute
- SpaceAttribute
- TextAreaAttribute
- TooltipAttribute
- ContextMenu
- ContextMenuItemAttribute
对象/变量 Class/Property
- ColorUsageAttribute
- DisallowMultipleComponent
- MinAttribute
- RangeAttribute
- RequireComponent
- SerializeField
- Serializable
- NonSerialized
其他
- PostProcessBuildAttribute
- PostProcessSceneAttribute
- InitializeOnLoadAttribute
- InitializeOnLoadMethodAttribute
- UnityAPICompatibilityVersionAttribute
官方文档特性汇总
UnityEngine Attributes
- AddComponentMenu
参数:menuName 菜单名称
作用于类上,将脚本置于 UnityEditor 顶部菜单栏 Component 菜单中的任意选项,而不局限于 Component->Scripts 选项下
将脚本置于 UnityEditor 顶部菜单栏 Component 菜单中的任意选项,而不局限于 Component->Scripts 选项下
[AddComponentMenu("WarlGComponent/WarlGAttribute")]
public class WarlGAttributeSample : MonoBehaviour {
}
![06b68143aa742ae4f4fc5107f2d648f2.png](https://img-blog.csdnimg.cn/img_convert/06b68143aa742ae4f4fc5107f2d648f2.png)
![9e038d95739395d12429d3f0e35f8961.png](https://img-blog.csdnimg.cn/img_convert/9e038d95739395d12429d3f0e35f8961.png)
- AssemblyIsEditorAssembly
作用于程序集,具有此属性的程序集中的任何类将被视为编辑器类
- BeforeRenderOrderAttribute
参数: order 调用顺序
作用于方法上,被作用的方法会被注册为 Application.onBeforeRender 的事件回调,以给定顺序从低到高顺序调用
- ColorUsageAttribute
参数:showAlpha 是否显示 Alpha 配置项、HDR 是否高动态范围
作用于 Color 类型变量上,使其可在取色面板配置显示 Alpha 和采用 HDR 标准
[ColorUsage(true,true)]
public Color colorpicker;
![804991fade71aa604a4d1887664aff3a.png](https://img-blog.csdnimg.cn/img_convert/804991fade71aa604a4d1887664aff3a.png)
- ContextMenu
参数:itemName 菜单项名称
作用于非静态方法上,在脚本 Inspector 的 context menu 上添加额外操作(Inspector 上脚本右击菜单)
[ContextMenu("WarlG Context Menu")]
void CustomContext()
{
Debug.Log("WarlGContext");
}
![0f90e1501cb445d808c2894b6a3dc920.png](https://img-blog.csdnimg.cn/img_convert/0f90e1501cb445d808c2894b6a3dc920.png)
- ContextMenuItemAttribute
参数:name 菜单名、function 方法名
作用于变量上,给作用的变量在Inspector上添加右键菜单方法
[ContextMenuItem("ResetString", "ResetSampleString")]
public st