FolderTreeView.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEditor.IMGUI.Controls;
  7. using UnityEngine;
  8. using UnityEngine.SceneManagement;
  9. using UnityObject = UnityEngine.Object;
  10. /// <summary>
  11. /// 绘制文件数状目录
  12. /// </summary>
  13. public class FolderTreeView : TreeView
  14. {
  15. /// <summary>
  16. /// 选择Item回调
  17. /// </summary>
  18. public CallBack<FolderTreeViewItem> selectCallBack;
  19. /// <summary>
  20. /// 双击Item回调
  21. /// </summary>
  22. public CallBack<FolderTreeViewItem> dblclickItemCallBack;
  23. static Texture2D folderIcon = EditorGUIUtility.FindTexture("Folder Icon");
  24. static Texture2D fileIcon = EditorGUIUtility.FindTexture("TextAsset Icon");
  25. private bool userSearch = true;
  26. private int index;
  27. private List<string> allPath = null;
  28. private SearchField m_SearchField;
  29. public FolderTreeView(TreeViewState state)
  30. : base(state)
  31. {
  32. index = 1;
  33. searchString = "";
  34. rowHeight = 20;
  35. showBorder = true;
  36. m_SearchField = new SearchField();
  37. m_SearchField.downOrUpArrowKeyPressed += SetFocusAndEnsureSelectedItem;
  38. }
  39. /// <summary>
  40. /// 是否开启使用搜索栏
  41. /// </summary>
  42. public bool UserSearch
  43. {
  44. get
  45. {
  46. return userSearch;
  47. }
  48. set
  49. {
  50. userSearch = value;
  51. }
  52. }
  53. /// <summary>
  54. /// 返回root节点
  55. /// </summary>
  56. public FolderTreeViewItem RootItem
  57. {
  58. get { return (FolderTreeViewItem)rootItem; }
  59. }
  60. /// <summary>
  61. /// 设置数据
  62. /// </summary>
  63. /// <param name="datas"></param>
  64. public void SetData(List<string> datas)
  65. {
  66. allPath = datas;
  67. //BuildRoot();
  68. // BuildRows(rootItem);
  69. Reload();
  70. }
  71. private List<FolderTreeViewItem> GetFolderRows()
  72. {
  73. List<FolderTreeViewItem> list = new List<FolderTreeViewItem>();
  74. var rows = GetRows();
  75. if (rows != null)
  76. {
  77. foreach (var item in GetRows())
  78. {
  79. list.Add((FolderTreeViewItem)item);
  80. }
  81. }
  82. return list;
  83. }
  84. protected override TreeViewItem BuildRoot()
  85. {
  86. FolderTreeViewItem root = new FolderTreeViewItem { id = 0, depth = -1, displayName = "Root" };
  87. root.children = new List<TreeViewItem>();
  88. var rows = GetFolderRows();
  89. if (allPath == null)
  90. return root;
  91. foreach (var p in allPath)
  92. {
  93. string[] items = p.Split('/');
  94. string fullPath = "";
  95. for (int i = 0; i < items.Length; i++)
  96. {
  97. string displayName = items[i];
  98. if (string.IsNullOrEmpty(fullPath))
  99. fullPath = displayName;
  100. else
  101. fullPath += "/" + displayName;
  102. bool isDirectory = !(i == (items.Length - 1));
  103. bool isHaveItem = false;
  104. foreach (var item in rows)
  105. {
  106. if (item.fullPath ==fullPath && item.isDirectory==isDirectory)
  107. {
  108. isHaveItem = true;
  109. break;
  110. }
  111. }
  112. if ( !isHaveItem)
  113. {
  114. FolderTreeViewItem temp = new FolderTreeViewItem(index, i, displayName,fullPath,isDirectory);
  115. string dir= Path.GetDirectoryName(fullPath);
  116. FolderTreeViewItem parent = null;
  117. foreach (var item in rows)
  118. {
  119. if (item.fullPath == dir && item.isDirectory)
  120. {
  121. parent = item;
  122. break;
  123. }
  124. }
  125. if (parent != null)
  126. parent.AddChild(temp);
  127. else
  128. root.AddChild(temp);
  129. rows.Add(temp);
  130. index++;
  131. }
  132. }
  133. }
  134. List<TreeViewItem> list = new List<TreeViewItem>();
  135. foreach (var item in rows)
  136. {
  137. list.Add(item);
  138. }
  139. //SetupParentsAndChildrenFromDepths(root, list);
  140. foreach (var item in rows)
  141. {
  142. if (item.hasChildren)
  143. item.icon = folderIcon;
  144. else
  145. item.icon = fileIcon;
  146. }
  147. return root;
  148. }
  149. public override void OnGUI(Rect rect)
  150. {
  151. if (!userSearch)
  152. base.OnGUI(rect);
  153. else
  154. {
  155. Rect searchRect = new Rect(rect.x, rect.y, rect.width, EditorStyles.toolbar.fixedHeight);
  156. GUI.Box(searchRect, "", EditorStyles.toolbar);
  157. searchRect = new Rect(rect.x+ rect.width / 2, rect.y+3, rect.width / 2, EditorStyles.toolbar.fixedHeight);
  158. searchString = m_SearchField.OnToolbarGUI(searchRect, searchString);
  159. rect = new Rect(rect.x, rect.y + searchRect.height, rect.width, rect.height - searchRect.height);
  160. base.OnGUI(rect);
  161. }
  162. }
  163. protected override void RowGUI(RowGUIArgs args)
  164. {
  165. Event e = Event.current;
  166. if (e.type == EventType.MouseDown && args.rowRect.Contains(e.mousePosition) && selectCallBack != null)
  167. {
  168. selectCallBack((FolderTreeViewItem)args.item);
  169. }
  170. if (e.type == EventType.MouseDown && args.rowRect.Contains(e.mousePosition) && e.clickCount == 2 && dblclickItemCallBack != null)
  171. {
  172. dblclickItemCallBack((FolderTreeViewItem)args.item);
  173. }
  174. base.RowGUI(args);
  175. }
  176. }
  177. public class FolderTreeViewItem: TreeViewItem
  178. {
  179. private string m_fullPath = "";
  180. private bool m_isDirectory;
  181. public FolderTreeViewItem(int id, int depth, string displayName,string fullPath,bool isDirectory) : base(id, depth, displayName)
  182. {
  183. m_fullPath = fullPath;
  184. m_isDirectory = isDirectory;
  185. }
  186. public FolderTreeViewItem(int id, int depth, string displayName) : base(id, depth, displayName)
  187. {
  188. }
  189. public FolderTreeViewItem(int id, int depth) : base(id, depth)
  190. {
  191. }
  192. public FolderTreeViewItem(int id) : base(id)
  193. {
  194. }
  195. public FolderTreeViewItem() : base()
  196. {
  197. }
  198. public string fullPath
  199. {
  200. get
  201. {
  202. return m_fullPath;
  203. }
  204. set
  205. {
  206. m_fullPath = value;
  207. }
  208. }
  209. public bool isDirectory
  210. {
  211. get
  212. {
  213. return m_isDirectory;
  214. }
  215. set
  216. {
  217. m_isDirectory = value;
  218. }
  219. }
  220. }