contextualMenuManager or Context Menu

Hey all,

I’ve been trying to figure out how to get a runtime context menu going for a while and I haven’t had any luck.

I’ve found various references to the ‘contextualMenuManager’, which seems to have been how this was done in some prior versions:

Along with some old posts by Unity devs here:

… talking about adding a manipulator

Translated to current Unity (2021.2) I see that the VisualElement class has a panel object, which in turn has a contextualMenuManager on it.

However, at least in the Unity Editor, this property is always null, and there doesn’t seem to be a way for me to change that. I’ve tried several things, including explicitly setting PickingMode, fumbling with panel settings, but no go.

Does anyone have any advice? Maybe I’m going down the completely wrong path with this, and should build my own context menu framework?

I’d appreciate any help. Thanks!

Were you able to figure anything out for this? I have not been able to get the ContextualMenuManager or the AddManipulator methods to work either.

Here’s how you can add new context menu options to your element using the ContextualMenuManipulator:

element.AddManipulator(new ContextualMenuManipulator((evt) =>
{
    evt.menu.AppendAction("Log Something", action => Debug.Log("Something"));
}));
1 Like

Hi there,
when I do the above I still get throw out of DoDisplayMenu in UnityEngine.UIElements.ContextualMenuManipulator, because it won’t find contextualMenuManager on RuntimePanel. Any idea of what could be wrong?

private void DoDisplayMenu(EventBase evt)
    {
      if (this.target.elementPanel?.contextualMenuManager == null)
        return;
      this.target.elementPanel.contextualMenuManager.DisplayMenu(evt, (IEventHandler) this.target);
      evt.StopPropagation();
      evt.PreventDefault();
    }
1 Like

Found this thread: MultiColumnListView and Context Menu It states that currently, context menu is not available everywhere in runtime.

1 Like

Thank you for the updates everyone! And I apologize for not responding back sooner.

Were you able to figure anything out for this? I have not been able to get the ContextualMenuManager or the AddManipulator methods to work either.

I was never able to get it working with the built-in systems but I eventually rolled my own very simple and limited context menu from an absolutely-positioned container element.

Reading that other thread it sounds like this context menu stuff was for the Unity Editor but not Runtime. Hopefully in the future we get built-in support for both.