Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    81
    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:
    967
    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!