#
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.
<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 on items. |
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. |
Refresh and a search keep the selection and the expanded items. When the search ends, the tree shows the items that the user expanded before it.
- A search or a
Refresh that removes a selected item raises SelectionChanged.
- Items are compared by reference. An item in several places shows in each.
Select, SetExpanded and ParentOf use its last place.
- In the UI Builder, a tree without roots shows two sample folders in a dashed outline. The sample does not show in Play mode or in a build, and the UI Builder does not save it to the UXML file.
- An item that is its own ancestor (a cycle) is left out below itself.
- The tree reads all items on each change. For tens of thousands of items, give it a smaller root set.
#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, arrows and selection. |
tb-data-tree__label |
The text of a row (ellipsis). |