Unity Editor Extension (MenuItem)

MenuItem

The MenuItem property is used to add menu items to the main menu and the inspector context menu.

The MenuItem property can convert any static function into a menu command. Only static functions can use the MenuItem property.

To create a hotkey, you can use the following special characters:% (ctrl on Windows, cmd on macOS)#   (shift),&   (alt). If no special modification key combination is required, the key can be given after the underline. For ex amp le, to create a menu with the hotkey shift-alt-g, use "MyMenu / do something #&g". To create a with hotkeys   g   For a menu without pressing the modify key, use "MyMenu/Do Something _g".

Some special keyboard keys can be supported as hotkeys. For example, "#LEFT" can be mapped to shift LEFT. The keys supporting this function are: LEFT, RIGHT, UP, DOWN, F1.. F12, HOME, END, PGUP and PGDN.

Hotkey text must be preceded by a space character ("MyMenu/Do_g" cannot be interpreted as a hotkey, while "MyMenu/Do_g" can be interpreted as a hotkey).

Add a menu item to the GameObject / 'menu to ensure that the   GameObjectUtility.SetParentAndAlign To ensure that the new GameObject is properly re parented when a context click event occurs (see the following example). Your function should also call   Undo.RegisterCreatedObjectUndo , so that the create operation can be undone and the   Selection.activeObject   Set to the newly created object. Also note that in order to propagate the menu items in "GameObject /" to the hierarchy view Create drop-down menu and hierarchy view context menu, it must be grouped with other game object creation menu items. This can be achieved by setting its priority to 10 (see the following example). Please note that for MenuItem that does not have a clear priority setting in "GameObject/Create Other" and supports legacy items, the received priority is 10 instead of the default 1000. We recommend using a more descriptive category name than "Create Other" and explicitly setting the priority to 10.

 

using UnityEditor;
using UnityEngine;
public class MenuTest : MonoBehaviour
{
    private static bool isCheck = false;

    // Create Do Something in the top menu bar and click call static method
    [MenuItem("MyMenu/Do Something")]
    static void DoSomething()
    {
        //Open website
        Application.OpenURL("www.baidu.com");
        //Open Directory
        EditorUtility.RevealInFinder(Application.persistentDataPath);
    }

    //Checkable menu
    [MenuItem("MyMenu/SetCheck")]
    static void DoSetCheck()
    {
        isCheck = !isCheck;
        Menu.SetChecked("MyMenu/SetCheck", isCheck);
    }

    // The menu will fail when the method returns false
    [MenuItem("MyMenu/Log Selected Transform Name", true)]
    static bool ValidateLogSelectedTransformName()
    {
        return Selection.activeTransform != null;
    }

    // Shortcut key
    [MenuItem("MyMenu/Do Something with a Shortcut Key %g")]
    static void DoSomethingWithAShortcutKey()
    {
        EditorApplication.ExecuteMenuItem("MyMenu/Do Something");
    }

    // Add menu to Rigidbody
    [MenuItem("CONTEXT/Rigidbody/Double Mass")]
    static void DoubleMass(MenuCommand command)
    {
        Rigidbody body = (Rigidbody)command.context;
        body.mass = body.mass * 2;
        Debug.Log("Doubled Rigidbody's Mass to " + body.mass + " from Context Menu.");
    }

    // Add to GameObject menu
    [MenuItem("GameObject/MyCategory/Custom Game Object", false, 10)]
    static void CreateCustomGameObject(MenuCommand menuCommand)
    {
        GameObject go = new GameObject("Custom Game Object");
        // Set the clicked object as the parent object, and null is the parent object in the scene
        GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
        // Registration can undo creation
        Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
        Selection.activeObject = go;
    }
}

 

 

 

Keywords: C# Unity

Added by artweb on Sat, 06 Nov 2021 20:02:28 +0200