Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    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:
    42
    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:
    851
    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:
    2
    Same as my friend before me, I was stuck because of this for some time.

    This needs to be added to the docs!