Unity adds custom menu button

If you want to add a custom menu button in the Unity editor, you can use Unity's MenuSystem API. Here's a simple example:

First you need to reference using UnityEditor;

using UnityEngine;
using UnityEditor;

two namespaces

Then add [MenuItem("Original menu name/custom name")] before the method

[MenuItem("Original menu name/custom name")]

Then just write the function to be executed

The function corresponding to this menu can be executed without running the game. It is mainly used for testing in the compiler and has no effect after release.

Case:

It will add a command named "My Custom Command" under the "Window" menu. When this command is clicked, a message will be printed and your custom operation will be performed:

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");
    }
}

Guess you like

Origin blog.csdn.net/leoysq/article/details/133139271