GenericMenu 通用菜单

The GenericMenu lets you create a custom context and dropdown menus.

GenericMenu用来创建自定义上下文菜单和下拉菜单。

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"引用。

// This example shows how to create a context menu inside a custom EditorWindow.
//这个例子显示怎样在自定义的编辑器窗口创建一个上下文菜单
class MyWindow extends EditorWindow {

	@MenuItem("TestContextMenu/Open Window")
	static function Init () {
		var window = GetWindow (MyWindow);
		window.position = Rect (50, 50, 250, 60);
		window.Show ();
	}

	function Callback (obj:Object) {
		Debug.Log ("Selected: " + obj);
	}

	function OnGUI() {
		var evt : Event = Event.current;
		var contextRect : Rect = new Rect (10, 10, 100, 100);

		if (evt.type == EventType.ContextClick)
		{
			var mousePos : Vector2 = evt.mousePosition;
			if (contextRect.Contains (mousePos))
			{
				// Now create the menu, add items and show it
				//现在创建菜单,添加项目并显示
				var menu : GenericMenu = new GenericMenu ();

				menu.AddItem (new GUIContent ("MenuItem1"), false, Callback, "item 1");
				menu.AddItem (new GUIContent ("MenuItem2"), false, Callback, "item 2");
				menu.AddSeparator ("");
				menu.AddItem (new GUIContent ("SubMenu/MenuItem3"), false, Callback, "item 3");

				menu.ShowAsContext ();

				evt.Use();
			}
		}
	}
}

Functions函数

Delegates委托

Page last updated: 2011-9-26