Version: 5.5 (switch to 5.6b)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

GenericMenu

class in UnityEditor

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

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

The example below opens an Editor window with a green area. Context-clicking the green area shows a context menu, which triggers a callback to the script when an item is selected.

.

#pragma strict
// This example shows how to create a context menu inside a custom EditorWindow.
// context-click the green area to show the menu
public class GenericMenuExample extends EditorWindow {
	@MenuItem("Example/Open Window")
	static function Init() {
		var window: EditorWindow = GetWindow.<GenericMenuExample>();
		window.position = new Rect(50, 50, 250, 60);
		window.Show();
	}
	function Callback(obj: Object) {
		Debug.Log("Selected: " + obj);
	}
	function OnGUI() {
		var currentEvent: Event = Event.current;
		var contextRect: Rect = new Rect(10, 10, 100, 100);
		EditorGUI.DrawRect(contextRect, Color.green);
		if (currentEvent.type == EventType.ContextClick) {
			var mousePos: Vector2 = currentEvent.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();
				currentEvent.Use();
			}
		}
	}
}
using UnityEngine;
using UnityEditor;
using System.Collections;

// This example shows how to create a context menu inside a custom EditorWindow. // context-click the green area to show the menu

public class GenericMenuExample : EditorWindow {

[MenuItem("Example/Open Window")] static void Init() { EditorWindow window = GetWindow<GenericMenuExample>(); window.position = new Rect(50, 50, 250, 60); window.Show(); }

void Callback(object obj) { Debug.Log("Selected: " + obj); }

void OnGUI() { Event currentEvent = Event.current; Rect contextRect = new Rect(10, 10, 100, 100); EditorGUI.DrawRect(contextRect, Color.green);

if (currentEvent.type == EventType.ContextClick) { Vector2 mousePos = currentEvent.mousePosition; if (contextRect.Contains(mousePos)) { // Now create the menu, add items and show it GenericMenu menu = 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(); currentEvent.Use(); } } } }

Public Functions

AddDisabledItemAdd a disabled item to the menu.
AddItemAdd an item to the menu.
AddSeparatorAdd a seperator item to the menu.
DropDownShow the menu at the given screen rect.
GetItemCountGet number of items in the menu.
ShowAsContextShow the menu under the mouse when right-clicked.

Delegates

MenuFunctionCallback function, called when a menu item is selected.
MenuFunction2Callback function with user data, called when a menu item is selected.