Editor工具开发基础一:顶部菜单栏拓展

一.创建脚本路径

不管是那种工具 都需要在工程里创建一个Editor文件夹 来存放工具.cs文件

二.特性

MenuItem 特性 修饰静态方法 

三个构造函数

public MenuItem(string itemName);
public MenuItem(string itemName, bool isValidateFunction);
public MenuItem(string itemName, bool isValidateFunction, int priority);

参数意义:

1.itemName 工具路径
2.isValidateFunction 是否是验证函数 为true是验证函数
3.priority 权重 同一级目录下权重越高 越在下边

三.示例

1.public MenuItem(string itemName);

eg:

public class EditorTools {
    [MenuItem("EditorTool/Tool")]
    public static void Test() {
        Debug.Log("test");
    }
}

显示:

 点击打印:

 2.public MenuItem(string itemName, bool isValidateFunction);

eg:

public class EditorTools {
    [MenuItem("EditorTool/Tool", false)]
    public static void Test() {
        Debug.Log("test");
    }
}

显示:

 参数修改为 True

eg:

public class EditorTools {
    [MenuItem("EditorTool/Tool", true)]
    public static void Test() {
        Debug.Log("test");
    }
}

显示:

 我们发现此时顶部菜单栏已经没有EditorTool了

因为参数为true时这个函数就是验证函数了,不再显示在外部工具栏

为True时的用法如下

eg:

public class EditorTools {
    [MenuItem("EditorTool/Tool", false)]
    public static void Test() {
        Debug.Log("test");
    }

    [MenuItem("EditorTool/Tool", true)]
    public static bool IsValidate() {
        return false;
    }
}

显示:

此时Test函数被验证函数IsValidate验证为无法执行

3.public MenuItem(string itemName, bool isValidateFunction, int priority);

修改权重:
eg:

public class EditorTools {
    [MenuItem("EditorTool/Tool2", false, 100)]
    public static void Test2() {
        Debug.Log("test");
    }
    [MenuItem("EditorTool/Tool3", false, 101)]
    public static void Test3() {
        Debug.Log("test");
    }
    [MenuItem("EditorTool/Tool1", false, 90)]
    public static void Test1() {
        Debug.Log("test");
    }
}

显示:

猜你喜欢

转载自blog.csdn.net/SmillCool/article/details/129183374
今日推荐