Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

TreeView ContextClicked firing at the same time as ContextClickedItem

Discussion in 'Extensions & OnGUI' started by AstralRadish, Jun 18, 2017.

  1. AstralRadish

    AstralRadish

    Joined:
    Jul 26, 2016
    Posts:
    1
    I'm building a custom TreeView and I have two event listeners for ContextClicked (OnContextClicked) and ContextClickedItem (OnContextClickedItem)
    ContextClicked is only supposed to get called when clicking outside any items *(https://docs.unity3d.com/ScriptReference/IMGUI.Controls.TreeView.ContextClicked.html), however, it is getting called at the same time as OnContextClickedItem when right-clicking a row.

    Related event dispatcher code (in class which extends TreeView):

    Code (CSharp):
    1.         public delegate void selectionDelegate(ScriptableObject[] selection);
    2.         public selectionDelegate OnContextClickedItem;
    3.         public Action OnContextClicked;
    4.  
    5.         private ScriptableObject[] GetSelectedObjects(IList<int> selectedIds) {
    6.             return selectedIds.Select(id => m_dataModel.GetRowByID(id).targetObject as ScriptableObject).ToArray();
    7.         }
    8.  
    9.         protected override void ContextClicked() {
    10.             base.ContextClicked();
    11.             if (OnContextClicked != null) {
    12.                 OnContextClicked();
    13.             }
    14.         }
    15.  
    16.         protected override void ContextClickedItem(int id) {
    17.             base.ContextClickedItem(id);
    18.             if (OnContextClickedItem != null) {
    19.                 OnContextClickedItem(GetSelectedObjects(GetSelection()));
    20.             }
    21.         }
    Event listener

    Code (CSharp):
    1.      
    2.  
    3.         void init() {
    4.             m_ModelsListView.OnContextClickedItem += modelContextClickedItem;
    5.             m_ModelsListView.OnContextClicked += modelContextClicked;
    6.         }
    7.  
    8.         void modelContextClickedItem(ScriptableObject[] selection) {
    9.             Debug.Log("contextItem");
    10.         }
    11.  
    12.         void modelContextClicked() {
    13.             Debug.Log("context");
    14.         }
    15.  
    When clicking outside a row, I get "context" printed. When I click on a row, I get "contextItem" and "context" printed.
     
  2. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    67
    Hi,

    If you use the context click you need to call: Event.current.Use();
    This is not clear from the current docs but will get added.
     
    AstralRadish likes this.
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    952
    Necro!

    Here's a reminder to update the docs. I just stumbled across this same issue, and needed to determine which context menu to show.
     
  4. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    4
    Same as my friend before me, I was stuck because of this for some time.

    This needs to be added to the docs!