UI Toolbox logoUI Toolbox
Support

DataTree

DataTree in UIToolbox shows a tree of any objects. It wraps the native TreeView, so the rows are virtual. You give it the root objects and a function that gives the children of an object. You do not build TreeViewItemData or ids.

A project tree open down to a selected Player.cs fileA project tree open down to a selected Player.cs file

<tb:DataTree name="scene" style="height: 260px;" />
var tree = root.Q<DataTree>("scene");
tree.SetRoots(scene.GetRootGameObjects(), item =>
    ((GameObject)item).transform.Cast<Transform>()
        .Select(t => t.gameObject),
    item => ((GameObject)item).name);
searchField.RegisterValueChangedCallback(evt =>
    tree.SearchText = evt.newValue);
tree.SelectionChanged += items => Debug.Log(tree.SelectedItem);
Member Description
SetRoots(roots, children, text) The root objects, the children of an object (null for a leaf), and the row text (default ToString).
Refresh() Reads the items again after code adds, removes or changes them.
SearchText Shows the items whose text contains it, in any case, with their parents, all expanded. The match is marked. Empty shows all items.
SelectedItems, SelectedItem, ClearSelection, SelectionChanged The selection as objects.
ItemsChosen A double click or Enter (the A button on a gamepad) on items. Enter or A on a row with children also opens or closes it.
RowHeight (row-height, 20) The fixed row height.
Select(item) Expands the parents, selects the item and scrolls to it. False when the item is not shown.
SetExpanded(item, expanded), IsExpanded(item), ParentOf(item) The expanded state and the parent of a shown item.
View The native TreeView, for the settings that the tree does not wrap, such as selectionType, ExpandAll or a custom makeItem and bindItem.

In the examples

Styling

Class Element
tb-data-tree The tree (grows; surface background, border). The variable --tb-data-tree-match on it is the mark color of search matches; it must let the text show through.
tb-data-tree__view The native TreeView. The workspace theme colors its rows and selection.
tb-data-tree__label The text of a row (ellipsis).

The expand arrow of a row is a corner of two borders. It points right on a closed item and turns down on an open item in --tb-animation-duration-fast on the ease-out-cubic curve. Its color is the variable --tb-data-tree-arrow-color, default var(--tb-color-text-muted). Set it on the tree, or on a class of the tree: the tree declares the variable itself, so a value on a parent does not reach the arrow.

.my-tree { --tb-data-tree-arrow-color: #cdb27a; }