GenericMenu.AddDisabledItem

Unity · Версия 2020.3 LTS · Scripting · GenericMenu

GenericMenu.AddDisabledItem

Объявление

public void AddDisabledItem(GUIContent content);

Параметры

content GUIContent для отображения в качестве отключенного пункта меню.

Описание

Добавить отключенный элемент в меню.

В приведенном ниже примере показано контекстное меню с отключенным элементом меню, который можно включать и выключать.

Смотрите так же: GenericMenu.AddItem, GenericMenu.AddSeparator.

// В этом примере показано, как создать контекстное меню внутри пользовательского 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();
            }
        }
    }
}

Объявление

public void AddDisabledItem(GUIContent content, bool on);

Параметры

content The GUIContent to display as a disabled menu item.
on Specifies whether to show that the item is currently activated (i.e. a tick next to the item in the menu).

Описание

Add a disabled item to the menu.

Обновлено 17 ноября 2026 г.