Do ContextualMenus only work during runtime?

I’ve been unable to get context menus to show up. Here’s my code:

using UnityEngine;
using UnityEngine.UIElements;

public class Test : MonoBehaviour
{
    private UIDocument _uiDocument;
    private VisualElement _redSquare;

    private void OnEnable()
    {
        _uiDocument = GetComponent<UIDocument>();
        _redSquare = _uiDocument.rootVisualElement.Q<VisualElement>(name: "test");
        _redSquare.focusable = true;
        _redSquare.pickingMode = PickingMode.Position;
        _redSquare.AddManipulator(new ContextualMenuManipulator(HandleContextMenuEvent));
    }

    private void HandleContextMenuEvent(ContextualMenuPopulateEvent contextMenuEvent)
    {
        contextMenuEvent.menu.AppendAction($"TESTING", HandleTestingClick, DropdownMenuAction.AlwaysEnabled);
    }

    private void HandleTestingClick(DropdownMenuAction dropdownMenuAction)
    {
        Debug.Log("TEST");
    }
}

Show up when? On a monobehaviour whilst outside of playmode?

Ah, so sorry! I meant do they work for runtime UI during playmode or only as an editor UI. For anyone else who’s wondering: they only work for editor UI
.

I wrote a custom contextual menu implementation using UI Toolkit that supports runtime usage. Please feel free to check it out.

1 Like

thx, for this work =)