Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

TreeView ContextClicked firing at the same time as ContextClickedItem

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

  1. AstralRadish


    Jul 26, 2016
    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 *(, 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;
    5.         private ScriptableObject[] GetSelectedObjects(IList<int> selectedIds) {
    6.             return selectedIds.Select(id => m_dataModel.GetRowByID(id).targetObject as ScriptableObject).ToArray();
    7.         }
    9.         protected override void ContextClicked() {
    10.             base.ContextClicked();
    11.             if (OnContextClicked != null) {
    12.                 OnContextClicked();
    13.             }
    14.         }
    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):
    3.         void init() {
    4.             m_ModelsListView.OnContextClickedItem += modelContextClickedItem;
    5.             m_ModelsListView.OnContextClicked += modelContextClicked;
    6.         }
    8.         void modelContextClickedItem(ScriptableObject[] selection) {
    9.             Debug.Log("contextItem");
    10.         }
    12.         void modelContextClicked() {
    13.             Debug.Log("context");
    14.         }
    When clicking outside a row, I get "context" printed. When I click on a row, I get "contextItem" and "context" printed.
  2. Mads-Nyholm


    Unity Technologies

    Aug 19, 2013

    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.