123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnityEditor;
- using UnityEditor.IMGUI.Controls;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityObject = UnityEngine.Object;
- /// <summary>
- /// 绘制文件数状目录
- /// </summary>
- public class FolderTreeView : TreeView
- {
- /// <summary>
- /// 选择Item回调
- /// </summary>
- public CallBack<FolderTreeViewItem> selectCallBack;
- /// <summary>
- /// 双击Item回调
- /// </summary>
- public CallBack<FolderTreeViewItem> dblclickItemCallBack;
- static Texture2D folderIcon = EditorGUIUtility.FindTexture("Folder Icon");
- static Texture2D fileIcon = EditorGUIUtility.FindTexture("TextAsset Icon");
- private bool userSearch = true;
- private int index;
- private List<string> allPath = null;
- private SearchField m_SearchField;
- public FolderTreeView(TreeViewState state)
- : base(state)
- {
- index = 1;
- searchString = "";
- rowHeight = 20;
- showBorder = true;
- m_SearchField = new SearchField();
- m_SearchField.downOrUpArrowKeyPressed += SetFocusAndEnsureSelectedItem;
- }
- /// <summary>
- /// 是否开启使用搜索栏
- /// </summary>
- public bool UserSearch
- {
- get
- {
- return userSearch;
- }
- set
- {
- userSearch = value;
- }
- }
- /// <summary>
- /// 返回root节点
- /// </summary>
- public FolderTreeViewItem RootItem
- {
- get { return (FolderTreeViewItem)rootItem; }
- }
- /// <summary>
- /// 设置数据
- /// </summary>
- /// <param name="datas"></param>
- public void SetData(List<string> datas)
- {
- allPath = datas;
- //BuildRoot();
- // BuildRows(rootItem);
- Reload();
- }
- private List<FolderTreeViewItem> GetFolderRows()
- {
- List<FolderTreeViewItem> list = new List<FolderTreeViewItem>();
- var rows = GetRows();
- if (rows != null)
- {
- foreach (var item in GetRows())
- {
- list.Add((FolderTreeViewItem)item);
- }
- }
- return list;
- }
- protected override TreeViewItem BuildRoot()
- {
- FolderTreeViewItem root = new FolderTreeViewItem { id = 0, depth = -1, displayName = "Root" };
- root.children = new List<TreeViewItem>();
- var rows = GetFolderRows();
- if (allPath == null)
- return root;
- foreach (var p in allPath)
- {
- string[] items = p.Split('/');
-
- string fullPath = "";
- for (int i = 0; i < items.Length; i++)
- {
-
- string displayName = items[i];
- if (string.IsNullOrEmpty(fullPath))
- fullPath = displayName;
- else
- fullPath += "/" + displayName;
- bool isDirectory = !(i == (items.Length - 1));
- bool isHaveItem = false;
- foreach (var item in rows)
- {
- if (item.fullPath ==fullPath && item.isDirectory==isDirectory)
- {
- isHaveItem = true;
- break;
- }
- }
- if ( !isHaveItem)
- {
- FolderTreeViewItem temp = new FolderTreeViewItem(index, i, displayName,fullPath,isDirectory);
- string dir= Path.GetDirectoryName(fullPath);
- FolderTreeViewItem parent = null;
- foreach (var item in rows)
- {
- if (item.fullPath == dir && item.isDirectory)
- {
- parent = item;
- break;
- }
- }
- if (parent != null)
- parent.AddChild(temp);
- else
- root.AddChild(temp);
- rows.Add(temp);
- index++;
- }
- }
- }
- List<TreeViewItem> list = new List<TreeViewItem>();
- foreach (var item in rows)
- {
- list.Add(item);
- }
-
- //SetupParentsAndChildrenFromDepths(root, list);
- foreach (var item in rows)
- {
- if (item.hasChildren)
- item.icon = folderIcon;
- else
- item.icon = fileIcon;
- }
- return root;
- }
-
- public override void OnGUI(Rect rect)
- {
- if (!userSearch)
- base.OnGUI(rect);
- else
- {
- Rect searchRect = new Rect(rect.x, rect.y, rect.width, EditorStyles.toolbar.fixedHeight);
- GUI.Box(searchRect, "", EditorStyles.toolbar);
- searchRect = new Rect(rect.x+ rect.width / 2, rect.y+3, rect.width / 2, EditorStyles.toolbar.fixedHeight);
- searchString = m_SearchField.OnToolbarGUI(searchRect, searchString);
-
- rect = new Rect(rect.x, rect.y + searchRect.height, rect.width, rect.height - searchRect.height);
- base.OnGUI(rect);
- }
- }
- protected override void RowGUI(RowGUIArgs args)
- {
- Event e = Event.current;
- if (e.type == EventType.MouseDown && args.rowRect.Contains(e.mousePosition) && selectCallBack != null)
- {
- selectCallBack((FolderTreeViewItem)args.item);
-
- }
- if (e.type == EventType.MouseDown && args.rowRect.Contains(e.mousePosition) && e.clickCount == 2 && dblclickItemCallBack != null)
- {
- dblclickItemCallBack((FolderTreeViewItem)args.item);
- }
- base.RowGUI(args);
- }
-
- }
- public class FolderTreeViewItem: TreeViewItem
- {
- private string m_fullPath = "";
- private bool m_isDirectory;
- public FolderTreeViewItem(int id, int depth, string displayName,string fullPath,bool isDirectory) : base(id, depth, displayName)
- {
- m_fullPath = fullPath;
- m_isDirectory = isDirectory;
- }
- public FolderTreeViewItem(int id, int depth, string displayName) : base(id, depth, displayName)
- {
- }
- public FolderTreeViewItem(int id, int depth) : base(id, depth)
- {
- }
- public FolderTreeViewItem(int id) : base(id)
- {
- }
- public FolderTreeViewItem() : base()
- {
- }
- public string fullPath
- {
- get
- {
- return m_fullPath;
- }
- set
- {
- m_fullPath = value;
- }
- }
- public bool isDirectory
- {
- get
- {
- return m_isDirectory;
- }
- set
- {
- m_isDirectory = value;
- }
- }
- }
|