使用 [xxx] 标记类和方法

版权声明:欢迎大家留言讨论共同进步,转载请注明出处 https://blog.csdn.net/qq_39108767/article/details/83447199

代码中很方便的 [xxx] 标记,比如工作中最常用的[SerializeField]等,还有扩展编辑器、自定义工具窗口,都需要用到[xxx]标记,网上可以搜到非常多且详细的介绍说明,比如:

Unity常用[xxx]用法 特性 (https://blog.csdn.net/FifthGently/article/details/78363364),

整理的非常齐全而且简单明了,需要的童鞋们可以百度谷歌啦~ 这里就只简单列举一下常用的几个标记:

//在Component菜单中自定义类
[AddComponentMenu("Test/TestComponent")]

//将脚本或组件自动添加到this.GameObject
[RequireComponent(typeof(Managers))]

//在编辑界面让功能(类)起作用,挂载脚本时即开始执行
[ExecuteInEditMode]

public class NewTestScript : MonoBehaviour
{
    //在Inspector面板序列化非public属性
    [SerializeField] int testNum01;

    //在Inspector面板隐藏public属性
    [HideInInspector] int testNum02;

    //在Inspector面板的属性,与上一行间隔指定距离
    [Space]
    public int testNum03;

    //在Inspector面板的属性标题
    [Header("XXX")]
    public int testNum04;

    //Inspector面板的属性限制取值范围,并用滑动条显示
    [Range(0, 10)]
    public int testNum05;

    //在编辑界面让功能(类)起作用
    [ExecuteInEditMode]
    void Awake()
    {
        Debug.Log(111);
    }
    void Start () 
    {
        Debug.Log(222);
	}

    //在Inspector版面右击包含该类,在菜单中会出现名为“XXX”的选项,点击选项会执行该标记的方法
    [ContextMenu("TestFunc")]
    void TestFunc()
    {
        Debug.Log(333);
    }

    //在菜单中添加选项栏,执行对应功能 (static, using UnityEditor;)
    [MenuItem("XXX/XXX")]
    static void TestMenuFunc()
    {
        Debug.Log(444);
    }

    //-----

    //自定义菜单栏选项
    //[MenuItem("MyTools/ToolName")]

    //在资源面板右键Create,创建该类对应的Asset文件
    //[CreateAssetMenu(fileName = "GameDataAsset", menuName = "Creat GameData Asset")]

    // ··· ···

}

猜你喜欢

转载自blog.csdn.net/qq_39108767/article/details/83447199
xxx