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

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

1:树节点类(TreeNode)

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class TreeNode {
  6.  
  7. public enum TreeNodeType
  8. {
  9. Item,
  10. Switch
  11. }
  12.  
  13. public string name;
  14. public TreeNodeType nodeType = TreeNodeType.Item;
  15. public TreeNode parent;
  16. public List<TreeNode> children = null;
  17. public bool isOpen = false;
  18. public static TreeNode _instance = null;
  19.  
  20. public static TreeNode Get()
  21. {
  22. if (_instance == null)
  23. {
  24. _instance = new TreeNode ();
  25. }
  26. return _instance;
  27. }
  28.  
  29. public void InsertNode(TreeNode node)
  30. {
  31. if (this.children == null)
  32. {
  33. this.children = new List<TreeNode> ();
  34. }
  35. children.Add (node);
  36. node.parent = this;
  37. }
  38.  
  39. public void OpenAllNode(TreeNode node)
  40. {
  41. node.isOpen = true;
  42. if (node.children != null && node.children.Count > )
  43. {
  44. for (int i = ; i < node.children.Count; i++)
  45. {
  46. OpenAllNode (node.children[i]);
  47. }
  48. }
  49. }
  50.  
  51. public TreeNode GenerateFileTree(List<string> list)
  52. {
  53. TreeNode root = new TreeNode ();
  54. root = GenerateFileNode ("", "生物/", list);
  55. OpenAllNode (root);
  56. return root;
  57. }
  58.  
  59. public TreeNode GenerateFileNode(string parentFullPath,string path,List<string> list)
  60. {
  61. TreeNode node = new TreeNode ();
  62. string[] segment = path.Split ('/');
  63. if (segment.Length > )
  64. {
  65. string name = segment[];
  66. node.name = name;
  67. node.nodeType = TreeNodeType.Switch;
  68. string fullPath = parentFullPath + name+"/";
  69. List<string> allChildrenPath = list.FindAll (s=>
  70. {
  71. if (s.StartsWith(fullPath) && s!=fullPath)
  72. {
  73. return true;
  74. }
  75. return false;
  76. }
  77. );
  78. List<string> dirList = new List<string> ();
  79. for (int i = ; i < allChildrenPath.Count; i++)
  80. {
  81. string childPath = allChildrenPath [i].Remove (, fullPath.Length);
  82. string[] childPathSegment = childPath.Split('/');
  83. if (childPathSegment.Length > ) {
  84. string childDirPath = childPathSegment [];
  85. if (!dirList.Contains (childDirPath)) {
  86. dirList.Add (childDirPath);
  87. TreeNode childNode = GenerateFileNode (fullPath, childDirPath + "/", list);
  88. node.InsertNode (childNode);
  89. }
  90. }
  91. else
  92. {
  93. TreeNode childNode = GenerateFileNode (fullPath, childPath, list);
  94. node.InsertNode (childNode);
  95. }
  96. }
  97. }
  98. else
  99. {
  100. node.name = path;
  101. node.nodeType = TreeNodeType.Item;
  102. list.Remove (path);
  103. }
  104. return node;
  105. }
  106.  
  107. }

2:界面实现类(CreateTreeList)

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class CreateTreeList:EditorWindow {
  7.  
  8. private List<string> list = new List<string> ();
  9. private static TreeNode root = null;
  10. private TreeNode currentNode;
  11. private static CreateTreeList _instance = new CreateTreeList();
  12. private int treeIndex = ;
  13. private static CreateTreeList window; // 自定义窗体
  14.  
  15. [MenuItem("H3D/构建树视图")]
  16.  
  17. static void Init(){
  18. window = EditorWindow.GetWindow<CreateTreeList>(); // 创建自定义窗体
  19. window.titleContent = new GUIContent("构建树视图"); // 窗口的标题
  20. window.Show();
  21. _instance.GetAssets ();
  22. _instance.CreateTree ();
  23. // 创建树
  24. }
  25.  
  26. // void Awake()
  27. // {
  28. // Debug.Log ("Awake");
  29. // }
  30.  
  31. void Start()
  32. {
  33. Debug.Log ("Start");
  34. }
  35.  
  36. // void Update()
  37. // {
  38. // Debug.Log ("Update");
  39. // }
  40.  
  41. private void GetAssets()
  42. {
  43. list.Clear ();
  44. list.Add ("生物/动物");
  45. list.Add ("生物/动物/宠物/猫");
  46. list.Add ("生物/动物/宠物/狗");
  47. // list.Add ("生物/动物/野生/老虎");
  48. // list.Add ("生物/动物/野生/狮子");
  49.  
  50. list.Add ("生物/植物");
  51. list.Add ("生物/植物/蔬菜/白菜");
  52. list.Add ("生物/植物/蔬菜/萝卜");
  53. // list.Add ("生物/植物/水果/苹果");
  54. // list.Add ("生物/植物/水果/橘子");
  55.  
  56. Debug.Log ("获取数据完成");
  57. }
  58.  
  59. private void CreateTree()
  60. {
  61. root = TreeNode.Get ().GenerateFileTree (list);
  62. Debug.Log ("生成文件树完成");
  63. // ShowFileTree (root, 0);
  64. // Debug.Log ("显示文件树完成");
  65. }
  66.  
  67. private void ShowFileTree(TreeNode node, int level)
  68. {
  69. string prefix = "";
  70. for (int i = ; i < level; i++)
  71. {
  72. prefix += "~";
  73. }
  74. Debug.Log (prefix + node.name);
  75. if (node == null || node.children == null)
  76. {
  77. return;
  78. }
  79. for (int i = ; i < node.children.Count; i++)
  80. {
  81. ShowFileTree (node.children[i], level+);
  82. }
  83. }
  84.  
  85. private void DrawFileTree(TreeNode node, int level)
  86. {
  87. if (node == null)
  88. {
  89. return;
  90. }
  91. GUIStyle style = new GUIStyle();
  92. style.normal.background = null;
  93. style.normal.textColor = Color.white;
  94. if (node == currentNode)
  95. {
  96. style.normal.textColor = Color.red;
  97. }
  98.  
  99. Rect rect = new Rect(+*level, +*treeIndex, node.name.Length*, );
  100. treeIndex++;
  101.  
  102. if (node.nodeType == TreeNode.TreeNodeType.Switch) {
  103. node.isOpen = EditorGUI.Foldout (rect, node.isOpen, node.name, true);
  104. }
  105. else
  106. {
  107. if (GUI.Button (rect, node.name, style))
  108. {
  109. Debug.Log (node.name);
  110. currentNode = node;
  111. }
  112. }
  113.  
  114. if (node==null || !node.isOpen || node.children == null)
  115. {
  116. return;
  117. }
  118. for (int i = ; i < node.children.Count; i++)
  119. {
  120. DrawFileTree (node.children[i], level+);
  121. }
  122. }
  123.  
  124. void OnGUI()
  125. {
  126. treeIndex = ;
  127. DrawFileTree (root, );
  128. }
  129. }

效果图:

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

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. python-xss攻击和单例模式

    1.python中单例模式 class Foo: instance = None def __new__(cls, *args, **kwargs): if not Foo.instance: Foo ...

  2. DRF之三大认证

    一.用户认证Authorticatons 1.源码解析 第一步. 找入口 def dispatch(self, request, *args, **kwargs): # 1.首先我们进入的是APIVi ...

  3. ## ucore Lab0 一些杂记

    ucore Lab0 一些杂记 前一阵子开始做 MIT 6.828,做了两三个实验才发现清华的 ucore 貌似更友好一些,再加上前几个实验也与6.828 有所重叠,于是决定迁移阵地. 文章计划分两类 ...

  4. 【转】UBOOT——启动内核

    转自:https://www.cnblogs.com/biaohc/p/6403863.html 1:什么是UBOOT,为什么要有UBOOT? UBOOT的主要作用是用来启动linux内核,因为CPU ...

  5. ubuntu 安装nvidia driver

    错误的命令:sudo apt-get install nvidiar-430好多教程都是这样 sudo apt-get install nvidia-driver-430  很奇怪这个命令变成这样 h ...

  6. moc_XXXX.o:(.data.rel.ro._ZTI12CalculatorUI[_ZTI12CalculatorUI]+0x10): undefined reference to `typeinfo for QWidget' collect2: error: ld returned 1 exit status make: *** [Makefile:144: myCalculator]

    main.cpp:(.text.startup+0x22): undefined reference to `QApplication::QApplication(int&, char**, ...

  7. python笔记(2)--字符串

    一.创建字符串 使用 '' , "" , ''' 和 """ 来创建字符串 var1 = 'hello, world' var2 = "ja ...

  8. css将两个元素水平对齐,兼容IE8

    css实现元素水平对齐 css实现水平对齐,如图 有人会说css实现这种水平对齐要兼容ie8还不简单吗?使用float: left,或者display: inline-block,不就可以了吗?是的, ...

  9. 常见BUG

    1.没有配置Tomcat服务,由于 <exclusions> <exclusion> <groupId>org.springframework.boot</g ...

  10. IDEA unable to find valid certification path to requested target

    一.报错 Could not transfer artifact org.apache.maven.plugins:maven-install-plugin:pom:2.4 from/to alima ...