MenuItem 属性用于向主菜单和检视面板上下文菜单添加菜单项。
该 MenuItem 属性能够将任何静态函数转变为菜单命令。仅静态函数可使用 MenuItem 属性。
要创建热键,您可以使用以下特殊字符:%(在 Windows 上为 ctrl,在 macOS 上为 cmd)、# (shift)、& (alt)。如果不需要特殊的修改键组合,该键可以在下划线后给出。例如,要创建一个带有热键 shift-alt-g 的菜单,可以使用“MyMenu/Do Something #&g”
。要创建带有热键 g 而不按下修改键的菜单,则使用“MyMenu/Do Something _g”
。
一些特殊的键盘键可支持作为热键,例如,“#LEFT”可映射到 shift-left。支持这一功能的键为:LEFT、RIGHT、UP、DOWN、F1 .. F12、HOME、END、PGUP 和 PGDN。
热键文本前必须有一个空格字符(“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; } }
MenuItem | 创建一个菜单项并在选中此菜单项后调用静态函数。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.