https://blog.csdn.net/e295166319/article/details/52370575

需要两个类:树节点类和界面实现类

1:树节点类(TreeNode)

using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class TreeNode { public enum TreeNodeType
{
Item,
Switch
} public string name;
public TreeNodeType nodeType = TreeNodeType.Item;
public TreeNode parent;
public List<TreeNode> children = null;
public bool isOpen = false;
public static TreeNode _instance = null; public static TreeNode Get()
{
if (_instance == null)
{
_instance = new TreeNode ();
}
return _instance;
} public void InsertNode(TreeNode node)
{
if (this.children == null)
{
this.children = new List<TreeNode> ();
}
children.Add (node);
node.parent = this;
} public void OpenAllNode(TreeNode node)
{
node.isOpen = true;
if (node.children != null && node.children.Count > )
{
for (int i = ; i < node.children.Count; i++)
{
OpenAllNode (node.children[i]);
}
}
} public TreeNode GenerateFileTree(List<string> list)
{
TreeNode root = new TreeNode ();
root = GenerateFileNode ("", "生物/", list);
OpenAllNode (root);
return root;
} public TreeNode GenerateFileNode(string parentFullPath,string path,List<string> list)
{
TreeNode node = new TreeNode ();
string[] segment = path.Split ('/');
if (segment.Length > )
{
string name = segment[];
node.name = name;
node.nodeType = TreeNodeType.Switch;
string fullPath = parentFullPath + name+"/";
List<string> allChildrenPath = list.FindAll (s=>
{
if (s.StartsWith(fullPath) && s!=fullPath)
{
return true;
}
return false;
}
);
List<string> dirList = new List<string> ();
for (int i = ; i < allChildrenPath.Count; i++)
{
string childPath = allChildrenPath [i].Remove (, fullPath.Length);
string[] childPathSegment = childPath.Split('/');
if (childPathSegment.Length > ) {
string childDirPath = childPathSegment [];
if (!dirList.Contains (childDirPath)) {
dirList.Add (childDirPath);
TreeNode childNode = GenerateFileNode (fullPath, childDirPath + "/", list);
node.InsertNode (childNode);
}
}
else
{
TreeNode childNode = GenerateFileNode (fullPath, childPath, list);
node.InsertNode (childNode);
}
}
}
else
{
node.name = path;
node.nodeType = TreeNodeType.Item;
list.Remove (path);
}
return node;
} }

2:界面实现类(CreateTreeList)

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic; public class CreateTreeList:EditorWindow { private List<string> list = new List<string> ();
private static TreeNode root = null;
private TreeNode currentNode;
private static CreateTreeList _instance = new CreateTreeList();
private int treeIndex = ;
private static CreateTreeList window; // 自定义窗体 [MenuItem("H3D/构建树视图")] static void Init(){
window = EditorWindow.GetWindow<CreateTreeList>(); // 创建自定义窗体
window.titleContent = new GUIContent("构建树视图"); // 窗口的标题
window.Show();
_instance.GetAssets ();
_instance.CreateTree ();
// 创建树
} // void Awake()
// {
// Debug.Log ("Awake");
// } void Start()
{
Debug.Log ("Start");
} // void Update()
// {
// Debug.Log ("Update");
// } private void GetAssets()
{
list.Clear ();
list.Add ("生物/动物");
list.Add ("生物/动物/宠物/猫");
list.Add ("生物/动物/宠物/狗");
// list.Add ("生物/动物/野生/老虎");
// list.Add ("生物/动物/野生/狮子"); list.Add ("生物/植物");
list.Add ("生物/植物/蔬菜/白菜");
list.Add ("生物/植物/蔬菜/萝卜");
// list.Add ("生物/植物/水果/苹果");
// list.Add ("生物/植物/水果/橘子"); Debug.Log ("获取数据完成");
} private void CreateTree()
{
root = TreeNode.Get ().GenerateFileTree (list);
Debug.Log ("生成文件树完成");
// ShowFileTree (root, 0);
// Debug.Log ("显示文件树完成");
} private void ShowFileTree(TreeNode node, int level)
{
string prefix = "";
for (int i = ; i < level; i++)
{
prefix += "~";
}
Debug.Log (prefix + node.name);
if (node == null || node.children == null)
{
return;
}
for (int i = ; i < node.children.Count; i++)
{
ShowFileTree (node.children[i], level+);
}
} private void DrawFileTree(TreeNode node, int level)
{
if (node == null)
{
return;
}
GUIStyle style = new GUIStyle();
style.normal.background = null;
style.normal.textColor = Color.white;
if (node == currentNode)
{
style.normal.textColor = Color.red;
} Rect rect = new Rect(+*level, +*treeIndex, node.name.Length*, );
treeIndex++; if (node.nodeType == TreeNode.TreeNodeType.Switch) {
node.isOpen = EditorGUI.Foldout (rect, node.isOpen, node.name, true);
}
else
{
if (GUI.Button (rect, node.name, style))
{
Debug.Log (node.name);
currentNode = node;
}
} if (node==null || !node.isOpen || node.children == null)
{
return;
}
for (int i = ; i < node.children.Count; i++)
{
DrawFileTree (node.children[i], level+);
}
} void OnGUI()
{
treeIndex = ;
DrawFileTree (root, );
}
}

效果图:

(点击后,被点击的节点红色显示,并在控制台输出被点击的节点的名字)

unity editor 折叠树的更多相关文章

  1. 帆软报表(finereport) 折叠树

    在进行展现数据时,希望模板的数据是可以动态折叠的,即点击数据前面的加号才展开对应下面的数据,可通过树节点按钮实现折叠树效果 实现思路: 1.这里建立一个内置数据集 添加数据 设置模板样式,添加颜色和对 ...

  2. Spine用于Timeline(NullReferenceException: Object reference not set to an instance of an object pine.Unity.Editor.AnimationReferenceAssetEditor.OnInspectorGUI ())

    报错信息:Spine.Unity.Editor.AnimationReferenceAssetEditor.OnInspectorGUI () (at Assets/Extention/Spine/E ...

  3. Unity Editor 下创建Lua和Text文件

    预览 在Project视图中,扩展右键菜单,右键 – Create - Text File 创建一个Text文件,或者Lua文件. 关键点 获取当前选择的路径,以Assets路径开头 var sele ...

  4. Unity Editor已停止工作

    在更换系统之后,可能会出现打开刚安装好的Unity,显示Unity Editor已停止工作,这时候我们考虑是系统win7的问题.可以在原系统上升级,也可以重新安装,升级.文中所涉及到的软件,可在右侧加 ...

  5. vue 仿zTree折叠树

    需求: vue实现仿zTree折叠树,此文章仅作为记录文档. 实现: <template> <div class="line-tree"> <div ...

  6. 编写 Unity Editor 插件

    Editor Style Viewer 在开发过程中,我喜欢编写一些辅助的Editor插件,方便在游戏开发过程进行调试. 下面是摘自Asset Store的一个查看Unity 默认GUI样式的小工具 ...

  7. [Editor]Unity Editor类常用方法

    Editor文档资料 Unity教程之-Unity Attribute的使用总结:http://www.unity.5helpyou.com/3550.html 利用unity3d属性来设置Inspe ...

  8. [cb] Unity Editor 添加右键菜单

    需求 为Unity的Editor窗口添加右键菜单 实现代码 // This example shows how to create a context menu inside a custom Edi ...

  9. unity Editor的使用

    1.首先定义一个需要控制数值的类,类中定义若干个变量 using UnityEngine;using System.Collections; using UnityEngine; using Syst ...

随机推荐

  1. 看CLRS 对B树的浅显理解

    定义及特点: 每个结点有n个关键字和n+1个指向子结点的指针,即有n+1个孩子结点. n个关键字按非递减的顺序存储. 最小度数t>=2,除了根结点的所有内部结点(非叶结点)的孩子数>=t且 ...

  2. C#索引器3 重载

    7.索引器  重载 public class Demo { private Hashtable name = new Hashtable(); public string this[int index ...

  3. python类和self解析

    在介绍Python的self用法之前,先来介绍下Python中的类和实例……我们知道,面向对象最重要的概念就是类(class)和实例(instance),类是抽象的模板,比如学生这个抽象的事物,可以用 ...

  4. java Thread源码分析(二)

    一.sleep的使用 public class ThreadTest { public static void main(String[] args) throws InterruptedExcept ...

  5. bzoj4011 [HNOI2015]落忆枫音 拓扑排序+DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4011 题解 首先考虑如果没有那么一条被新加进来的奇怪的边的做法. 我们只需要给每一个点挑一个父 ...

  6. Debian10+OpenMediaVault(OMV)安装

    前言:测试打造NAS平台,以下是步骤. 安装Debian10 注:请下载amd64,不要下载i836平台,因为OMV外挂插件不支持I836所以不建议用i836,如只使用官方插件可以无视 安装前-安装, ...

  7. 文本检错——中文拼写检查工具FASPell

    最近因为相关项目需要考虑中文文本检错,然后就发现了爱奇艺发布的号称SOTA的FASPell已经开源代码,所以开始着手实现. 检错思想两步:一,掩码语言模型(MLM)产生候选字符:二,CSD过滤候选字符 ...

  8. 【leetcode】523. Continuous Subarray Sum

    题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...

  9. 面试题常考&必考之--js中的难点!!!原型链,原型(__proto__),原型对象(prototype)结合例子更易懂

    1>首先,我们先将函数对象认识清楚: 补充snow的另一种写法: var snow =function(){}; 2>其次:就是原型对象 每当我们定义一个函数对象的时候,这个对象中就会包含 ...

  10. java上传大文件(局域网环境)

    文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...