Legacy Documentation: Version 5.2
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

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

Switch to Manual

Description

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

// 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(); } } } }

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.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961