1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Are you an artist or level designer going to Unite Europe? Join our roundtables there to discuss artist features.
    Dismiss Notice
  6. Unity 5.6 is now released.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases 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:
    40
    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.