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

Feature Request Further developments on TreeView

Discussion in 'UI Toolkit' started by ChGuidi, Jun 21, 2022.

  1. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    105
    Hi,

    I used the TreeView for the first time. It's working fine, but I'm missing some functionality. What are the further developments that are still on the roadmap?

    A few suggestions:
    * Being able to add childs to items after construction
    * It's quite cumbersome to set the ids to the right value. Isn't this something that should be done by the tree internally?

    Thanks,
    Chloë
     
  2. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    Hi,

    You can use `AddItem` to insert items after creation. Make sure to set the parameter rebuildTree to false when doing multiple additions, and rebuild only once they're done.

    Having control over the ids should help identify the items once the tree is created, but I understand that you might not always need that kind of control. We could introduce the possibility to create a tree view without ids and generate them internally. I'll check with the team.

    Thanks!
     
  3. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    105
    Hi @unity_griendeau

    Thanks for the reply. I don't see an AddItem on TreeViewItemData. I saw that these functions are all internal, so I cannot access them. Where can I find this AddItem?

    Code (CSharp):
    1.         internal void AddChild(TreeViewItemData<T> child)
    2.         {
    3.             m_Children.Add(child);
    4.         }
    5.  
    6.         internal void AddChildren(IList<TreeViewItemData<T>> children)
    7.         {
    8.             foreach (TreeViewItemData<T> child in children)
    9.             {
    10.                 AddChild(child);
    11.             }
    12.         }
    13.  
    14.         internal void InsertChild(TreeViewItemData<T> child, int index)
    15.         {
    16.             if (index < 0 || index >= m_Children.Count)
    17.             {
    18.                 m_Children.Add(child);
    19.             }
    20.             else
    21.             {
    22.                 m_Children.Insert(index, child);
    23.             }
    24.         }
    25.  
    26.         internal void RemoveChild(int childId)
    27.         {
    28.             if (m_Children == null)
    29.             {
    30.                 return;
    31.             }
    32.  
    33.             for (int i = 0; i < m_Children.Count; i++)
    34.             {
    35.                 if (childId == m_Children[i].id)
    36.                 {
    37.                     m_Children.RemoveAt(i);
    38.                     break;
    39.                 }
    40.             }
    41.         }
    42.  
    43.         internal int GetChildIndex(int itemId)
    44.         {
    45.             int num = 0;
    46.             foreach (TreeViewItemData<T> child in m_Children)
    47.             {
    48.                 if (child.id == itemId)
    49.                 {
    50.                     return num;
    51.                 }
    52.  
    53.                 num++;
    54.             }
    55.  
    56.             return -1;
    57.         }
    58.  
    59.         internal void ReplaceChild(TreeViewItemData<T> newChild)
    60.         {
    61.             if (!hasChildren)
    62.             {
    63.                 return;
    64.             }
    65.  
    66.             int num = 0;
    67.             foreach (TreeViewItemData<T> child in m_Children)
    68.             {
    69.                 if (child.id == newChild.id)
    70.                 {
    71.                     m_Children.RemoveAt(num);
    72.                     m_Children.Insert(num, newChild);
    73.                     break;
    74.                 }
    75.  
    76.                 num++;
    77.             }
    78.         }
     
  4. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    Sorry, it is on the TreeView directly.
    TreeView.AddItem<T>(TreeViewItemData<T> item, int parentId = -1, int childIndex = -1, bool rebuildTree = true)
     
  5. ChGuidi

    ChGuidi

    Joined:
    Dec 28, 2021
    Posts:
    105
    Ok that's good to know! I see that the documentation was also updated, thanks!
     
  6. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    How do you get tabbing to move the focus into an open treeview folder?