TreeView GUI
A Drag and Drop Tree View Element
|
This tutorial shows, in a simple way, how to create a basic TreeView for inspector and editor.
First of all, in order to use this class, you should know where it is located in the wHiteRabbiT framework, i.e. its namespace :
1 | 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 usefull.
Before showing a TreeView, you have to instance the class and fill it with node elements, describe with their relative path in tree. For example :
1 2 3 4 5 6 | 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 a OnGUI() block.
1 | public void DrawGUILayout( float Height) |
Draws the TreeView.
Height : The height of the composant in pixel.
If you wan to show and active the checkboxes in the TreeView, you must enable the correct property :
1 | 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.
1 2 3 4 5 6 7 8 9 | 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 turn on the check dependencies functionnality for dropped asset in the TreeView. This is a internal behavior of GUITreeViewFolder for drag and dropped event.
When you want to create your own behavior for an inherited GUITreeView class, you can (and you should) override three methods :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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 }; } |