TreeView

asset store | unity forum | script reference

Introduction

This tutorial shows, in a simple way, how to create a basic TreeView for inspector and editor.

Quick Guide

First of all, in order to use this class, you should know where it is located in the wHiteRabbiT framework, i.e. its namespace :

using wHiteRabbiT.Unity.UI;

FYI, there are some other classes in the package such as AboutEW, feel free to explore them, some helpers which could be useful.

Before showing a TreeView, you have to instance the class and fill it with node elements, described with their relative path in the tree. For example :

GUITreeView treeView0 = new GUITreeView("root");

treeView0.AddTreeElement("test0");

treeView0.AddTreeElement("test1");

treeView0.AddTreeElement("test0/test00");

treeView0.AddTreeElement("test0/test01/test010");

treeView0.AddTreeElement("test1/test10/test100");

After doing that, you can now just call the drawing method in an OnGUI() block.

public void DrawGUILayout(float Height)

Draws the TreeView.

Height : The height of the composant in pixels.

If you want to show and active the checkboxes in the TreeView, you must enable the correct property :

treeView0.UseCheckbox = true;

There is a sample class, named GUITreeViewFolder, which is inherited from GUITreeView in order to manage folders. In the example package, this class is used to be a new view of the asset folder.

treeView = new GUITreeViewFolder("", AssetDatabase.GetAllAssetPaths());

 

// Replace the actual useless root element by the "Assets" one

treeView = treeView.children[0] as GUITreeViewFolder;

 

// Expand "Assets" folder

treeView.element.Expanded = true;

 

treeView.DragDropDependencies = true;

The last line turns on the check dependencies functionality for dropped assets in the TreeView. This is an internal behavior of GUITreeViewFolder for drag and drop events.

When you want to create your own behavior for an inherited GUITreeView class, you can (and you should) override three methods :

public override void DragAndDropCallBack (GUITreeView treeView)

{

    GUITreeViewFolder tvf = treeView as GUITreeViewFolder;

     

    if (tvf == null)

        return;

     

    foreach(string path in DragAndDrop.paths)

        if (tvf.CheckIfExists(path, true) && DragDropDependencies)

            foreach(string dpath in AssetDatabase.GetDependencies(new string[] {path}))

                tvf.CheckIfExists(dpath, true);

}

 

public override bool ShowElementExpanded

{

    get

    {

        return IsDirectory;

    }

}

 

protected override GUITreeView CreateNode(string fullName, string Name)

{

    return new GUITreeViewFolder(Name) {                        

        element = new CheckedExpandedElement {

            fullName = fullName,

            Name = Name

        },

        parent = this

    };

}