Version: 2021.3

MenuItem

class in UnityEditor

切换到手册

描述

MenuItem 属性用于向主菜单和检视面板上下文菜单添加菜单项。

该 MenuItem 属性能够将任何静态函数转变为菜单命令。仅静态函数可使用 MenuItem 属性。

To create a hotkey you can use the following special characters: % (ctrl on Windows and Linux, cmd on macOS), ^ (ctrl on Windows, Linux, and macOS), # (shift), & (alt). If no special modifier key combinations are required the key can be given after an underscore. For example to create a menu with hotkey shift-alt-g use "MyMenu/Do Something #&g". To create a menu with hotkey g and no key modifiers pressed use "MyMenu/Do Something _g".

Some special keyboard keys are supported as hotkeys, for example "#LEFT" would map to shift-left. The keys supported like this are: LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN, INS, DEL, TAB, SPACE.

热键文本前必须有一个空格字符(“MyMenu/Do_g”不能被解释为热键,而“MyMenu/Do _g”则可以被解释为热键)。

将菜单项添加到“GameObject/”菜单,以在创建自定义游戏对象时,确保 调用 GameObjectUtility.SetParentAndAlign,从而确保在发生上下文单击事件时, 对新的游戏对象进行正确地重定父级(请参阅以下示例)。您的函数也应该调用 Undo.RegisterCreatedObjectUndo,以使创建操作可撤销并将 Selection.activeObject 设置到新创建的对象上。另请注意,为了将“GameObject/”中的菜单项 传播到层级视图 Create 下拉菜单和层级视图上下文菜单,它必须与 其他游戏对象创建菜单项归为一组。这可以通过将其优先级 设为 10 来实现(请参阅以下示例)。请注意,对于“GameObject/Create Other” 中没有明确优先级设置且支持旧版项目的 MenuItem 来说,接收到的优先级为 10 而非默认的 1000, 我们建议使用比“Create Other”更具描述性的类别名称,并将优先级 显式设置为 10。

using UnityEditor;
using UnityEngine;
public class MenuTest : MonoBehaviour
{
    // Add a menu item named "Do Something" to MyMenu in the menu bar.
    [MenuItem("MyMenu/Do Something")]
    static void DoSomething()
    {
        Debug.Log("Doing Something...");
    }

// Validated menu item. // Add a menu item named "Log Selected Transform Name" to MyMenu in the menu bar. // We use a second function to validate the menu item // so it will only be enabled if we have a transform selected. [MenuItem("MyMenu/Log Selected Transform Name")] static void LogSelectedTransformName() { Debug.Log("Selected Transform is on " + Selection.activeTransform.gameObject.name + "."); }

// Validate the menu item defined by the function above. // The menu item will be disabled if this function returns false. [MenuItem("MyMenu/Log Selected Transform Name", true)] static bool ValidateLogSelectedTransformName() { // Return false if no transform is selected. return Selection.activeTransform != null; }

// Add a menu item named "Do Something with a Shortcut Key" to MyMenu in the menu bar // and give it a shortcut (ctrl-g on Windows, cmd-g on macOS). [MenuItem("MyMenu/Do Something with a Shortcut Key %g")] static void DoSomethingWithAShortcutKey() { Debug.Log("Doing something with a Shortcut Key..."); }

// Add a menu item called "Double Mass" to a Rigidbody's context menu. [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 a menu item to create custom GameObjects. // Priority 1 ensures it is grouped with the other menu items of the same kind // and propagated to the hierarchy dropdown and hierarchy context menus. [MenuItem("GameObject/MyCategory/Custom Game Object", false, 10)] static void CreateCustomGameObject(MenuCommand menuCommand) { // Create a custom game object GameObject go = new GameObject("Custom Game Object"); // Ensure it gets reparented if this was a context click (otherwise does nothing) GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject); // Register the creation in the undo system Undo.RegisterCreatedObjectUndo(go, "Create " + go.name); Selection.activeObject = go; } }

变量

editorModesSpecifies the Editor modes that the item is displayed for. If you don't specify any Editor modes, the item is only displayed for the default mode.

构造函数

MenuItem创建一个菜单项并在选中此菜单项后调用静态函数。