Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question unity treeview expanding and collapsing manually.

Discussion in 'UI Toolkit' started by EspyGDS, Jul 12, 2023.

  1. EspyGDS

    EspyGDS

    Joined:
    Mar 11, 2020
    Posts:
    10
    So I have a treeview with multiple layers in it and it has custom styling in it. I don't want to show the arrow on the left that can expand and collapse the items but I want this functionality when I click on an item in the treeview. Now we have the selection change event for which I made a method that also does this. However selection changed does not trigger if you click on the same item twice (which makes sense). But none of the other events same to be able to do this either so how would I be able to do this?
     
  2. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    You could register a ClickEvent on your items to detect the click and do the expand/collapse from there.
    Here's some documentation on how to do it: https://docs.unity3d.com/Manual/UIE-Click-Events.html
    It could also be done just on a PointerDownEvent or PointerUpEvent.

    It could look something like this (not tested).

    Code (CSharp):
    1. treeView.makeItem = () =>
    2. {
    3.     var element = new MyElement();
    4.     element.RegisterCallback<ClickEvent>(OnItemClicked);
    5.     return element;
    6. };
    7. treeView.bindItem = (element, i) =>
    8. {
    9.     ((MyElement)element).index = i;
    10. };
    11.  
    12. void OnItemClicked(ClickEvent evt)
    13. {
    14.     var element = evt.target as MyElement;
    15.     treeView.viewController.ExpandItemByIndex(element.index, false);
    16. }
     
  3. EspyGDS

    EspyGDS

    Joined:
    Mar 11, 2020
    Posts:
    10
    Had to do a bit more work then that but thanks to this I did get to the conclusion thanks