MenuItem 菜单项

Inherits from System.Attribute

The MenuItem attribute allows you to add menu items to the main menu and inspector context menus.

MenuItem属性允许你添加菜单项到主菜单和检视面板上下文菜单。

Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.
注意:这是一个编辑器类,如果想使用它你需要把它放到工程目录下的Assets/Editor文件夹下。编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,你需要在脚本前面加上 "using UnityEditor"引用。

The MenuItem attribute turns any static function into a menu command. Only static functions can use the MenuItem attribute.

MenuItem属性把任意静态函数变成为一个菜单命令。仅静态函数能使用这个MenuItem属性。

To create a hotkey you can use the following special characters: % (ctrl on Windows, cmd on OS X), # (shift), & (alt), _ (no key modifiers). For example to create a menu with hotkey shift-alt-g use "GameObject/Do Something #&g". To create a menu with hotkey g and no key modifiers pressed use "GameObject/Do Something _g". A hotkey text must be preceded with a space character ("GameObject/Do_g" won't be interpreted as hotkey, while "GameObject/Do _g" will).

可以使用一下指定字符创建热键:% (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;
	}
}
// JavaScript example:
// Add menu named "Do Something" to the main menu
//添加菜单名为Do Something 到主菜单
@MenuItem ("GameObject/Do Something")
static function Something () {
	Debug.Log ("Perform operation");
}

// Validate the menu item.
//验证菜单项。
// The item will be disabled if no transform is selected.
//如果没有变换被选择,该项将是禁用的。
@MenuItem ("GameObject/Do Something", true)
static function 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 function SomethingAgain () {
	Debug.Log ("Perform operation");
}

// Add context menu named "Do Something" to rigid body's context menu
//添加上下文菜单名为Do Something 到刚体的上下文菜单
@MenuItem ("CONTEXT/Rigidbody/Something")
static function Something (command : MenuCommand) {
	var body : Rigidbody = command.context;
	body.mass = 5;
}

Constructors构造器

最后修改:2011年6月27日 Monday 14:46

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。