Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

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

GenericMenu.AddDisabledItem

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

public function AddDisabledItem(content: GUIContent): void;
public void AddDisabledItem(GUIContent content);

Parameters

content The GUIContent to display as a disabled menu item.

Description

Add a disabled item to the menu.

The example below shows a context menu with a disabled menu item that can be toggled on and off.



See Also: GenericMenu.AddItem, GenericMenu.AddSeparator.

#pragma strict
// This example shows how to create a context menu inside a custom EditorWindow,
// where the first menu item toggles whether the second menu item is enabled
// or disabled.
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();
	}
	var item2enabled: boolean = false;
	public function Toggle() {
		item2enabled = !item2enabled;
		Debug.Log("item2enabled: " + item2enabled);
	}
	public function Item2Callback() {
		Debug.Log("Item 2 Selected");
	}
	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("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();
			}
		}
	}
}
// This example shows how to create a context menu inside a custom EditorWindow,
// where the first menu item toggles whether the second menu item is enabled
// or disabled.

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