Search Unity

How can I make a any UI Element open a ContextMenu when right clicked

Discussion in 'UI Toolkit' started by msfredb7, Aug 23, 2019.

  1. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    162
    Hello!

    I've been trying to add a simple context menu when the user right-clicks on a certain element.

    I have it set up like that:
    Code (CSharp):
    1. myUIElement.RegisterCallback((ContextualMenuPopulateEvent evt) =>
    2. {
    3.     evt.menu.AppendAction("ActionA", (x)=>{});  
    4.     evt.menu.AppendAction("ActionB", (x)=>{});
    5. });
    The event never gets fired. It only works if I add a child UI element that already supports the right-click feature like a text field. Like so:
    Code (CSharp):
    1. myUIElement.RegisterCallback(...);
    2. myUIElement.Add(new TextField());
    What am I missing?

    (For the sake of example, let's say the element I want to right click is a TextElement)
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    You need to make your `myUIElement` focusable (myUIElement.focusable = true), or at the very least, set the `myUIElement.pickingMode = PickingMode.Position;`.
     
  3. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    162
    @uDamian It doesn't seem to work. I have this
    Code (CSharp):
    1. var element = root.Q<Toggle>(name: "online");
    2. element.focusable = true;
    3. element.pickingMode = PickingMode.Position;
    4. element.RegisterCallback((ContextualMenuPopulateEvent evt) =>
    5. {
    6.     UnityEngine.Debug.Log("right click");
    7. });
    8.  
     
  4. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Ah sorry, just add the
    ContextualMenuManpulator
    to your element, like this:
    Code (CSharp):
    1. element.AddManipulator(new ContextualMenuManipulator((ContextualMenuPopulateEvent evt) =>
    2. {
    3.     UnityEngine.Debug.Log("right click");
    4. }));
    You could add it like this
    element.new ContextualMenuManipulator(null);
    and then register for
    ContextualMenuPopulateEvent
    later if you wish as well.
     
    Xelnath, wang37921 and cecarlsen like this.
  5. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    162
    @uDamian That worked perfectly, thanks for the help!
     
  6. neoneper

    neoneper

    Joined:
    Nov 14, 2013
    Posts:
    48
    Hello. I'm arriving late, but I think I can enjoy the subject.
    I'm trying to add more menus to my graphview's context menu. I have no idea.
    I was try the normal way, but dnt work:
    upload_2020-9-14_20-15-33.png

    upload_2020-9-14_20-14-2.png
     
  7. Yecats

    Yecats

    Joined:
    Jul 13, 2014
    Posts:
    69
    @neoneper I am trying to do the same thing. Did you solve this?

    Edit:

    I was able to solve the question that @neoneper asked - which is to add an item to the GraphView context menu.

    Here's how:
    • On your class that inherits from GraphView, override the BuildContextualMenu method.
    • You can add items to the menu by Appending / Inserting (see code below).
    • Make sure that you check for the target to be GraphView or Node - otherwise you'll get a null reference.
    Code (CSharp):
    1.  
    2. public class BehaviorTreeGraphView : GraphView
    3. {      
    4.     public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
    5.     {
    6.             base.BuildContextualMenu(evt);
    7.  
    8.             if (evt.target is GraphView || evt.target is Node)
    9.             {
    10.                 evt.menu.AppendAction("Testing", (e) => { Debug.Log("Test"); });
    11.             }
    12.      }
    13. }
    14.  
     
    Last edited: Oct 4, 2020
    Keepabee, jjbish, thirty30 and 2 others like this.
  8. neoneper

    neoneper

    Joined:
    Nov 14, 2013
    Posts:
    48
    TY Brow (^.~)
     
    thirty30 and SimonDufour like this.
  9. DaVinci_13

    DaVinci_13

    Joined:
    May 11, 2022
    Posts:
    1
    @uDamian I am trying to do this in Treeview. The ContextMenu worked, but there is a problem:
    • a new treeviewitem will not be selected when right clicked. And the last selected item is still be selected.

    How can I do to right-click select which I want?
    (ps. IMGUI treeview can work it.)
     
    Last edited: Jul 13, 2023
  10. vlastelin

    vlastelin

    Joined:
    Mar 17, 2013
    Posts:
    3
    Necro!

    Hello. I'm trying to create a TreeView and add a ContextMenu to the entire view, as well as expand it with an additional item when right-clicking on a specific element, but I'm having trouble. Can anyone help?