|
The GenericMenu lets you create a custom context and dropdown menus.
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.
// 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();
}
}
}
}
AddItem |
Add an item to the menu |
AddDisabledItem |
Add a disabled item to the menu |
AddSeparator |
Add a seperator item to the menu |
GetItemCount |
Get number of items in the menu |
ShowAsContext |
Show the menu under the mouse |
DropDown |
Show the menu at the given screen rect |
MenuFunction |
Callback function, called when a menu item is selected |
MenuFunction2 |
Callback function with user data, called when a menu item is selected |