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 Trouble with reorderable TreeView

Discussion in 'UI Toolkit' started by DoctorShinobi, Dec 5, 2022.

  1. DoctorShinobi

    DoctorShinobi

    Joined:
    Oct 5, 2012
    Posts:
    219
    Using Unity 2022.2, I'm trying to use a TreeView with the "reorderable" property set to true.
    My issue is detecting when the user reorders items and applying those changes to the source list. I try listening to the ItemIndexChanged event like this:
    Code (CSharp):
    1. _treeView.itemIndexChanged += (itemIdBeingMoved, newParentID) =>
    2. {
    3. ...
    4. }
    5.  
    The problem is there are no parameters that indicate the old and new order of the item that was moved. treeView.itemsSource doesn't seem to help either when trying to figure out what changes were made.

    I tried looking through the docs, but couldn't find a demonstration for a TreeView that handles reordering. What am I missing?
     
  2. GeoBT

    GeoBT

    Joined:
    Jan 14, 2014
    Posts:
    2
    I ran into this same issue a week ago and tbh this callback just feels incomplete.
    When an element is reordered, we really need to know the id of the moved element, the new parent, the child index, and the old parent id in order to be able to efficiently repro the move in our external data.

    In order to get the child index I had to:

    Code (CSharp):
    1. treeView.viewController.RebuildTree();
    2. int childIndex = treeView.viewController.GetChildIndexForId(moveId);
    The callback is called internally in DefaultTreeViewController.Move() and they have the child index at that point but just aren't exposing it to us. Hope it'll be updated soon.
     
    DoctorShinobi likes this.
  3. alexandred_unity

    alexandred_unity

    Unity Technologies

    Joined:
    Dec 12, 2018
    Posts:
    43
    Hi,
    Did you try _treeView.viewController.itemIndexChanged?
     
  4. DoctorShinobi

    DoctorShinobi

    Joined:
    Oct 5, 2012
    Posts:
    219
    I did. It seems to work exactly like _treeView.itemIndexChanged, which doesn't help me apply the changes to the source list. I can get the new sibling index using GeoBT's solution, but there's still no way to get the previous parent ID.