Legacy Documentation: Version 5.4
LanguageEnglish
  • C#
  • JS

Script language

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

GenericMenu.AddItem

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

Sumbission failed

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

Close

Cancel

public function AddItem(content: GUIContent, on: bool, func: GenericMenu.MenuFunction): void;
public void AddItem(GUIContent content, bool on, GenericMenu.MenuFunction func);

Parameters

content The GUIContent to add as a menu item.
on Whether to show the item is currently activated (i.e. a tick next to the item in the menu).
func The function to call when the menu item is selected.

Description

Add an item to the menu.


public function AddItem(content: GUIContent, on: bool, func: GenericMenu.MenuFunction2, userData: object): void;
public void AddItem(GUIContent content, bool on, GenericMenu.MenuFunction2 func, object userData);

Parameters

content The GUIContent to add as a menu item.
on Whether to show the item is currently activated (i.e. a tick next to the item in the menu).
func The function to call when the menu item is selected.
userData The data to pass to the function called when the item is selected.

Description

Add an item to the menu.

#pragma strict
// This example shows how to create a context menu inside a custom EditorWindow.
public class MyWindow extends EditorWindow {
	@MenuItem("TestContextMenu/Open Window")
	public static function Init() {
		var window: EditorWindow = GetWindow(MyWindow);
		window.position = new Rect(50, 50, 250, 60);
		window.Show();
	}
	public function Callback(obj: Object) {
		Debug.Log("Selected: " + obj);
	}
	public 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.AddItem(new GUIContent("SubMenu/MenuItem4"), false, Callback, "item 4");
				menu.AddItem(new GUIContent("SubMenu/MenuItem5"), false, Callback, "item 5");
				menu.AddSeparator("SubMenu/");
				menu.AddItem(new GUIContent("SubMenu/MenuItem6"), false, Callback, "item 6");
				menu.ShowAsContext();
				evt.Use();
			}
		}
	}
}
// This example shows how to create a context menu inside a custom EditorWindow.
using UnityEngine;
using UnityEditor;

public class MyWindow : EditorWindow { [MenuItem("TestContextMenu/Open Window")] public static void Init () { EditorWindow window = GetWindow (typeof(MyWindow)); window.position = new Rect (50, 50, 250, 60); window.Show (); } public void Callback (object obj) { Debug.Log ("Selected: " + obj); }

public void OnGUI() { Event evt = Event.current; Rect contextRect = new Rect (10, 10, 100, 100); if (evt.type == EventType.ContextClick) { Vector2 mousePos = evt.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.AddItem (new GUIContent ("SubMenu/MenuItem4"), false, Callback, "item 4"); menu.AddItem (new GUIContent ("SubMenu/MenuItem5"), false, Callback, "item 5"); menu.AddSeparator ("SubMenu/"); menu.AddItem (new GUIContent ("SubMenu/MenuItem6"), false, Callback, "item 6"); menu.ShowAsContext ();

evt.Use(); } } } }
no example available in JavaScript
// This example shows how to create a context menu inside a custom EditorWindow.
using UnityEngine;
using UnityEditor;

public class MyWindow : EditorWindow { [MenuItem("TestContextMenu/Open Window")] public static void Init () { var window = GetWindow (typeof(MyWindow)); window.position = new Rect (50, 50, 250, 60); window.Show (); } bool item2enabled = false; public void Toggle () { item2enabled = !item2enabled; Debug.Log("item2enabled: "+item2enabled); } public void Item2Callback () { Debug.Log("Item 2 Selected"); }

public void OnGUI() { Event evt = Event.current; Rect contextRect = new Rect (10, 10, 100, 100); if (evt.type == EventType.ContextClick) { Vector2 mousePos = evt.mousePosition; if (contextRect.Contains (mousePos)) { // Now create the menu, add items and show it GenericMenu menu = new GenericMenu (); menu.AddItem (new GUIContent ("Toggle item 2"), item2enabled, Toggle); if (item2enabled) { menu.AddItem (new GUIContent ("Item 2"), false, Item2Callback); } else { menu.AddDisabledItem (new GUIContent ("Item 2")); } menu.ShowAsContext ();

evt.Use(); } } } }