MenuItem支持自定义创建快捷键
MenuItem 菜单项可以使用一下指定字符创建热键:
%(Windows上为ctrl, OS X上为cmd),
#(shift),
&(alt), _ (无修改键)。
例如创建一个菜单热键为shift-alt-g使用GameObject/Do Something #&g。创建一个菜单热键g并没有修改键(组合键),使用GameObject/Do Something _g。热键文本必须在前面加一个空格字符(GameObject/Do_g不会被解释为热键,而是GameObject/Do _g这样,注意_g前面有空格)。
// C# example:
using UnityEditor;
using UnityEngine;
class MenuTest : MonoBehaviour {
// Add menu named "Do Something" to the main menu
//添加菜单名为Do Something 到主菜单
[MenuItem ("GameObject/Do Something")]
static void DoSomething () {
Debug.Log ("Perform operation");
}
// Validate the menu item.
//验证菜单项。
// The item will be disabled if no transform is selected.
//如果没有变换被选择,该项将是禁用的。
[MenuItem ("GameObject/Do Something", true)]
static bool ValidateDoSomething () {
return Selection.activeTransform != null;
}
// Add menu named "Do Something" to the main menu
//添加菜单名为Do Something 到主菜单
// and give it a shortcut (ctrl-o on Windows, cmd-o on OS X).
//并且指定快捷键(windows为ctrl-o,OS X为cmd-o)
[MenuItem ("GameObject/Do Something %o")]
static void DoSomething () {
Debug.Log ("Perform operation");
}
// Add context menu named "Do Something" to rigid body's context menu
//添加上下文菜单名为Do Something 到刚体的上下文菜单
[MenuItem ("CONTEXT/Rigidbody/Do Something")]
static void DoSomething (MenuCommand command) {
Rigidbody body = (Rigidbody)command.context;
body.mass = 5;
}
}