unity中一些常用的Attribute(特性)

来源网址:https://www.cnblogs.com/nsky/p/5275106.html
根据原博主自己分类总结了一下 感谢原博主
作用在类上方的
1.[RequireComponent(typeof(Rigidbody))]
RequireComponent约束的组件。是不能删除的。除非先删除当前脚本

2.[DisallowMultipleComponent]
DisallowMultipleComponent:不能在一个对象上重复添加该脚本。

3.[AddComponentMenu(“脚本/prot.cs”), SelectionBase]
AddComponentMenu:在Component菜单中添加一项
4.[Serializable]
序列化类、结构、枚举、委托

作用在方法上方

  1. [ContextMenu(“方法名”)]
    ContextMenu:单击组件右键菜单
    作用:在Inspector中右键点击这个脚本在菜单中点击方法名即可运行方法
    eg:[ContextMenu(“callBack”)]
    void CallBack()
    {
    Debug.Log(“back”);
    }

作用在字段上方
1.[SerializeField]
序列化字段 :private类型的字段 可以在Inspector显示

2.[Range(0, 5)]
该字段值的范围 :让字段(数值类型)通过滑动条来确定值
可以:适用于多个变量、数组
eg:[SerializeField, Range(1, 5)]
int cont1, sum1;
[SerializeField, Range(1, 5)]
int[] array;

3.[Tooltip(“提示”)]
Tooltip:鼠标放到字段上显示提示

4.[Space(60)]
Space:设置字段和字段之间的间距 (距上个字段的间距)

5.[Header(“提示”)]
Header:字段的头部 可以用作显示
eg: [SerializeField, Header(“用户地址”)]
string address;

6.[Multiline(5)] 5代表行数
Multiline:多行文本框
eg:[SerializeField, Multiline(5)]
string message;

7.[TextArea(1, 7)] 1:最小行,7:最大行
TextArea:多行文本框(没有行限制)超过了最大值行。就会显示滑动条
eg: [SerializeField, TextArea(1, 7)]
string tips;
8. [HideInInspector]
使字段在Inspector不再显示(即使将Inspector的模式设置为Debug模式也看不见)
eg: [HideInInspector]
public int a=10;

猜你喜欢

转载自blog.csdn.net/lyb19940526/article/details/85837289
今日推荐