MenuItem
属性允许您向主菜单和 Inspector 窗口上下文菜单添加菜单项。
MenuItem
属性将任何静态函数转换为菜单命令。只有静态函数才能使用 MenuItem
属性。
要创建热键,请使用以下特殊字符
如果不需要特殊的修饰键组合,则可以在下划线后给出键。例如,要创建具有热键 Shift+Alt+G 的菜单,请使用 "MyMenu/Do Something #&g"
。要创建没有需要按下的修饰键的热键 G 的菜单,请使用 "MyMenu/Do Something _g"
。
热键文本前必须有空格字符。例如,"MyMenu/Do_g"
不被解释为热键,但 "MyMenu/Do _g"
是。
某些特殊的键盘键支持作为热键。例如,“#LEFT” 将映射到 Shift-Left。这样支持的键包括:LEFT、RIGHT、UP、DOWN、F1 .. F12、HOME、END、PGUP、PGDN、INS、DEL、BACKSPACE、TAB 和 SPACE。
当向“GameObject/”菜单添加菜单项以创建自定义游戏对象时,请确保调用 GameObjectUtility.SetParentAndAlign 以确保在上下文单击的情况下新游戏对象被正确重新父级化(请参见下面的示例)。您的函数还应调用 Undo.RegisterCreatedObjectUndo 使创建可撤消,并将 Selection.activeObject 设置为新创建的对象。另请注意,为了使“GameObject/”中的菜单项传播到层次结构创建下拉菜单和层次结构上下文菜单,它必须与其他游戏对象创建菜单项分组。这可以通过将其优先级设置为 10 来实现(请参见下面的示例)。请注意,出于旧版兼容性的目的,“GameObject/Create Other”中的 MenuItem 如果没有显式设置优先级,则将接收 10 的优先级而不是默认的 1000 - 我们建议使用比“Create Other”更具描述性的类别名称并显式地将优先级设置为 10。
您可以使用 MenuItem
向项目窗口中的右键单击上下文菜单添加菜单项。项目窗口中的上下文菜单使用与 Assets 菜单相同的菜单项。当您向 Assets 菜单添加菜单项时,该菜单项也会添加到项目窗口的上下文菜单中。例如,"Assets/Do something"
将 Do something
菜单项添加到项目窗口的上下文菜单中。
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..."); }
// Add a menu item named "Log Selected Transform Name" to MyMenu in the menu bar. // We want this to be validated menu item: an item that is only enabled if specific conditions are met. // To achieve this, we use a second function below 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 "Log Selected Transform Name" menu item is disabled if this function returns false. // We tell the Editor that this is a validation function by decorating it with a MenuItem attribute // and passing true as the second parameter. // This invokes the MenuItem(string itemName, bool isValidateFunction) attribute constructor // resulting in the function being treated as the validator for "Log Selected Transform Name" menu item. [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 10 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; }
// Add a menu item called "Test" to the Scene view context menu when the // EditorToolContext "TestToolContext" is engaged. [MenuItem("CONTEXT/TestToolContext/Test")] static void TestToolContextItem() { Debug.Log("Testing Test Tool Context Menu Item"); }
// Add a menu item called "Test" to the Scene view context menu when the // EditorTool "TestTool" is engaged. [MenuItem("CONTEXT/TestTool/Test")] static void TestToolItem() { Debug.Log("Testing Test Tool Menu Item"); } }
MenuItem | 创建一个菜单项,并在选中菜单项时调用其后的静态函数。 |