Version: 2023.2
public static void DisplayEditorMenu (UIElements.DropdownMenu menu, Rect rect);
public static void DisplayEditorMenu (UIElements.DropdownMenu menu, UIElements.EventBase triggerEvent);

参数

menu Menu to show.
rect Position and size of the parent control.
triggerEvent Event summoning context menu.

描述

Displays dropdown menu as a contextual menu.

using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
public class ContextMenuWindow : EditorWindow
{
    [MenuItem("My/Window")]
    static void ShowWindow() => GetWindow<ContextMenuWindow>();
    private void CreateGUI()
    {
        Button myContextMenuButton = null;
        myContextMenuButton = new Button(() =>
        {
            var menu = new DropdownMenu();
            menu.AppendAction("My Action 1", null, DropdownMenuAction.AlwaysEnabled);
            menu.AppendAction("My Action 2", null, DropdownMenuAction.AlwaysEnabled);
            menu.AppendAction("My Action 3", null, DropdownMenuAction.AlwaysEnabled);
            EditorMenuExtensions.DisplayEditorMenu(menu, myContextMenuButton.worldBound);
        });
        myContextMenuButton.text = "My Contex Menu Button";
    }
}