Unity 折叠树 EditorGUI.Foldout

发表于2017-11-04
评论0 1.1k浏览

下面给大家介绍下折叠树EditorGUI.Foldout,在使用折叠树EditorGUI.Foldout时,还需要两个类:树节点类和界面实现类:


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 > 0)   
  43.         {  
  44.             for (int i = 0; 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 > 1)  
  64.         {  
  65.             string name = segment[0];  
  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 = 0; i < allChildrenPath.Count; i )   
  80.             {  
  81.                 string childPath = allChildrenPath [i].Remove (0, fullPath.Length);  
  82.                 string[] childPathSegment = childPath.Split('/');  
  83.                 if (childPathSegment.Length > 1) {  
  84.                     string childDirPath = childPathSegment [0];  
  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 = 0;  
  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.   
  42.     private void GetAssets()  
  43.     {  
  44.         list.Clear ();  
  45.         list.Add ("生物/动物");  
  46.         list.Add ("生物/动物/宠物/猫");  
  47.         list.Add ("生物/动物/宠物/狗");  
  48. //      list.Add ("生物/动物/野生/老虎");  
  49. //      list.Add ("生物/动物/野生/狮子");  
  50.   
  51.         list.Add ("生物/植物");  
  52.         list.Add ("生物/植物/蔬菜/白菜");  
  53.         list.Add ("生物/植物/蔬菜/萝卜");  
  54. //      list.Add ("生物/植物/水果/苹果");  
  55. //      list.Add ("生物/植物/水果/橘子");  
  56.       
  57.         Debug.Log ("获取数据完成");  
  58.     }  
  59.   
  60.     private void CreateTree()  
  61.     {  
  62.         root = TreeNode.Get ().GenerateFileTree (list);  
  63.         Debug.Log ("生成文件树完成");  
  64. //      ShowFileTree (root, 0);  
  65. //      Debug.Log ("显示文件树完成");  
  66.     }  
  67.   
  68.   
  69.     private void ShowFileTree(TreeNode node, int level)  
  70.     {  
  71.         string prefix = "";  
  72.         for (int i = 0; i < level; i )  
  73.         {  
  74.             prefix  = "~";  
  75.         }  
  76.         Debug.Log (prefix   node.name);  
  77.         if (node == null || node.children == null)   
  78.         {  
  79.             return;  
  80.         }  
  81.         for (int i = 0; i < node.children.Count; i )   
  82.         {  
  83.             ShowFileTree (node.children[i], level 1);  
  84.         }  
  85.     }  
  86.   
  87.   
  88.     private void DrawFileTree(TreeNode node, int level)  
  89.     {  
  90.         if (node == null)   
  91.         {  
  92.             return;  
  93.         }  
  94.         GUIStyle style = new GUIStyle();  
  95.         style.normal.background = null;  
  96.         style.normal.textColor = Color.white;  
  97.         if (node == currentNode)   
  98.         {  
  99.             style.normal.textColor = Color.red;  
  100.         }  
  101.   
  102.         Rect rect = new Rect(5 20*level, 5 20*treeIndex, node.name.Length*25, 20);  
  103.         treeIndex ;  
  104.   
  105.         if (node.nodeType == TreeNode.TreeNodeType.Switch) {  
  106.             node.isOpen = EditorGUI.Foldout (rect, node.isOpen, node.name, true);  
  107.         }  
  108.         else  
  109.         {  
  110.             if (GUI.Button (rect, node.name, style))   
  111.             {  
  112.                 Debug.Log (node.name);  
  113.                 currentNode = node;  
  114.             }  
  115.         }  
  116.       
  117.         if (node==null || !node.isOpen || node.children == null)   
  118.         {  
  119.             return;  
  120.         }  
  121.         for (int i = 0; i < node.children.Count; i )   
  122.         {  
  123.             DrawFileTree (node.children[i], level 1);  
  124.         }  
  125.     }  
  126.   
  127.     void OnGUI()  
  128.     {  
  129.         treeIndex = 0;  
  130.         DrawFileTree (root, 0);  
  131.     }  
  132. }  
效果图:


如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论