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…
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?
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.