VisualElement: How to add UnityEngine.UIElements.DropdownMenu to another VisualElement

I can’t figure out how to use the UnityEngine.UIElements.DropdownMenu with the VisualElement system. With other classes that inherit from UIElements.VisualElement, I can simple .Add() them but I can’t with a UIElements.DropdownMenu.

Instead I’ve been using UnityEditor.UIElements.ToolbarMenu because it inherits from VisualElement, has a .menu which is a DropdownMenu, and Im making a custom editor so it being in UnityEditor isn’t an issue. However, when using it, I get really weird error triangles for the backing texture…

alt text

Am I missing something? How do I properly use DropdownMenu? Is there a way to fix ToolbarMenu if that’s what I’m supposed to use?

A little late but…

Here’s an example of a simple right-click menu:

using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

public class MyEditorWindow : EditorWindow
{
    [MenuItem("Hello/Hello")]
    static void MenuEntry()
    {
        GetWindow<MyEditorWindow>();
    }

    void OnEnable()
    {
        var label = new Label("Hello!");
        label.AddManipulator(new ContextualMenuManipulator(BuildContextualMenu));
        rootVisualElement.Add(label);
    }

    void BuildContextualMenu(ContextualMenuPopulateEvent evt)
    {
        evt.menu.AppendAction("SomeAction", OnMenuAction, DropdownMenuAction.AlwaysEnabled);
    }

    void OnMenuAction(DropdownMenuAction action)
    {
        Debug.Log(action.name);
    }
}

The above code uses the ContextualMenuManipulator which is not a physical element, just a manipulator that goes on top of a element and provides the menu functionality.

By the way, if you just want a dropdown/popup control, have a look at the Choice Fields in the Window > UI > UIElements Samples window.