Unity添加自定义菜单按钮

如果你想在Unity编辑器中添加自定义菜单按钮,你可以使用Unity的MenuSystem API。这是一个简单的示例:

首先需要引用using UnityEditor;

using UnityEngine;
using UnityEditor;

两个命名空间

然后在方法前添加    [MenuItem("原菜单名/自定义名")]

[MenuItem("原菜单名/自定义名")]

紧接着写要执行的函数即可

这个菜单对应的函数不需要运行游戏即可执行,主要是在编译器中测试使用,发布后没有作用

案例:

它将在"Window"菜单下添加一个名为"My Custom Command"的命令,当点击此命令时,会打印一条消息并执行你的自定义操作:

using UnityEngine;
using UnityEditor;

public class CustomMenu : EditorWindow
{
    [MenuItem("Window/My Custom Command")]
    public static void ShowWindow()
    {
        GetWindow(typeof(CustomMenu)).Show();
    }

    [MenuItem("Edit/My Custom Command")]
    public static void Edit()
    {
        // 执行你的自定义编辑操作
        Debug.Log("Editing");
    }
}

猜你喜欢

转载自blog.csdn.net/leoysq/article/details/133139271