TreeList is a simple generic tree data type with Unity Editor support. It's based on a List<> internally so its name.
- Fast serialization/deserialization (only data and depth of the tree node)
- Fast iteration, children enumeration (because of the List)
- Not so fast modification (because of the List)
- Prefab workflow support (Apply/Revert)
- Copy/Paste/Undo support
- UIToolkit and IMGUI editors (mostly equivalent in functionality)
- Dark/light skin compatible
Install a package from a Git URL.
Please take a look at Unity Manual for instructions and use https://github.com/Silentor/TreeList.git?path=/Packages/com.Silentor.TreeList
URL in Package Manager to install package.
Or add "com.silentor.treelist": "https://github.com/Silentor/TreeList.git?path=/Packages/com.Silentor.TreeList"
line to your Packages/manifest.json
file
public class Sample : MonoBehaviour
{
public TreeList<String> StringTree;
private void Awake( )
{
StringTree = new TreeList<String>();
//Add root node
var rootNode = StringTree.Add( "I am Root", null );
//Add children nodes to root node
rootNode.AddChildren( "child1", "child2", "child3", "child4" );
//Enumerate children of root node
var childs = rootNode.GetChildren( ).ToArray();
//Add child to 'child3' node
var grandChild = childs[2].AddChild( "grand child" );
//Move 'child1' to the grandchild node as a child
StringTree.Move( childs[0], grandChild );
//Remove node from tree
StringTree.Remove( childs[1] );
//Print tree structure and values
Debug.Log( StringTree.ToHierarchyString() );
/*
Value = 'I am Root' (level = 0, )
Value = 'child3' (level = 1, parent 'I am Root')
Value = 'grand child' (level = 2, parent 'child3')
Value = 'child1' (level = 3, parent 'grand child')
Value = 'child4' (level = 1, parent 'I am Root')
*/
}
}
Methods GetChildren()
and GetChildrenBFS()
return IEnumerable so it allocates a little. If you want zero-allocation get-children logic for tight loops please use GetChildrenNonAlloc()
and GetChildrenBFSNonAlloc()
methods. They receive List<> parameter for result list and do not allocate if list capacity is enough.
Add/remove nodes (UIToolkit, dark theme)
Search values in a tree (IMGUI, dark theme)
Drag and drop nodes to move around or copy ( Ctrl+drag ) (IMGUI, light theme). There is a small issue with Ctrl+dragging in UIToolkit implementation, drag icon does not change from "move" to "copy" while copy operation is working.
Work with prefabs (apply, revert) and copy/paste of entire tree property, tree node value and properties of complex node levels (UIToolkit, light theme). Sorry for the themed context menu for node values, I didn't find a way to hook up the native context menu without ruining native menu for separate node values.
TreeList inspector tries hard to support custom property drawers for values (dynamic height includes), property attributes like Header etc. Even for IMGUI inspector (dark theme).
I have tested editor performance for a random tree with 1000 nodes and ~15 depth and editor responsiveness was quite good.
Feel free to post your question in Discussions
This library is under the MIT License