写法模板:
DirNode node = new DirNode("D:/Project/Unity2018Test/Assets"); GetDirectoryInfo("D:/Project/Unity2018Test/Assets",2,node); //模板 private void GetDirectoryInfo(string dir ,int depth,Node node) { if (depth <= 0) return; depth--; if (Directory.Exists(dir)) { string[] subDirs = Directory.GetDirectories(dir); foreach (var item in subDirs) { DirNode dirNode = new DirNode(item); node.AddSubNode(dirNode); GetDirectoryInfo(item,depth,dirNode); } string[] subFiles = Directory.GetFiles(dir); foreach (var item in subFiles) { GetDirectoryInfo(item,depth ,node); } } else if (File.Exists(dir)) { FileNode fileNode = new FileNode(dir); node.AddSubNode(fileNode); } } } public abstract class Node { public enum NodeType { Dir, File } public abstract NodeType type { get; } public List<Node> subNode = new List<Node>(); public string path; public void AddSubNode(Node node) { subNode.Add(node); } } public class FileNode : Node { public override NodeType type => NodeType.File; public FileNode(string str) { path = str; } } public class DirNode : Node { public override NodeType type => NodeType.Dir; public DirNode(string str) { path = str; } }
下面是引用的写法---当然有需要可以提取成代码模板
//UITreeData UI UI数据结构类 UITreeData nodeData; private void Ini() { nodeData = new UITreeData("root", -1); DirNode node = new DirNode(GameApp.Ins.cash.equipmentModelPath); defParentNode = nodeData; node.curUINode = nodeData; GetDirectoryInfo(GameApp.Ins.cash.equipmentModelPath, 5, node); } private UITreeData defParentNode = new UITreeData(); //节点类-虚函数 public abstract class Node { public enum NodeType { Dir,File } public abstract NodeType type { get; } public List<Node> subNode = new List<Node>(); public UITreeData curUINode; public DirectoryInfo parentDirInfo; public string path; public void AddSubNode(Node node) { subNode.Add(node); } } //文件节点 public class FileNode :Node { public override NodeType type => NodeType.File; public FileNode(string str) { path = str; } } //文件夹节点 public class DirNode : Node { public override NodeType type => NodeType.Dir; public DirNode(string str) { path = str; parentDirInfo = new DirectoryInfo(str); } } /// <summary> /// 遍历算法--递归查找 /// </summary> /// <param name="dir">文件路径</param> /// <param name="depth">遍历目标层级---默认递归深度</param> /// <param name="node">当前根文件夹</param> private void GetDirectoryInfo(string dir, int depth, Node node) { if (depth <= 0) return; depth--; Debug.Log(node.curUINode.Name + "____aaa"); if (Directory.Exists(dir)) //判断文件夹是否存在 { //查找文件夹 string[] subDirs = Directory.GetDirectories(dir); foreach (var item in subDirs) { DirNode dirNode = new DirNode(item); node.AddSubNode(dirNode); //添加文件夹入列表 //var info = new DirectoryInfo(item); if (dirNode.parentDirInfo.Name == "Materials" || dirNode.parentDirInfo.Name == "Textures") //判断文件名 { continue; } var forderdata = new UITreeData(dirNode.parentDirInfo.Name); forderdata.rightMenu = forderMenu; forderdata.rightClick = OnForderRightMenu; forderdata.SetCustomData<FileData>(new FileData(false, item, null)); //设置树形节点 node.curUINode.AddChild(forderdata); dirNode.curUINode = forderdata; Debug.Log(item + "___DirNode_111"); GetDirectoryInfo(item, depth, dirNode); //递归 } //查找文件 string[] subFiles = Directory.GetFiles(dir); foreach (var item in subFiles) { //Debug.Log(item); if (isSysExt(Path.GetExtension(item).ToLower())) { UITreeData childdata = new UITreeData(Path.GetFileName(item)); childdata.SetCustomData<FileData>(new FileData(false, item, null)); childdata.rightMenu = modelMenu; childdata.rightClick = OnModelRightBtn; childdata.leftClick = ShowModel; node.curUINode.AddChild(childdata); datas.Add(childdata); //添加到列表 } Debug.Log(item + "___FileNode_222"); GetDirectoryInfo(item, depth, node);//递归 } } else if (File.Exists(dir)) //判断文件是存在 { FileNode fileNode = new FileNode(dir); node.AddSubNode(fileNode); //return; } }
欢迎各位指正交流,如有兴趣的可以把这个写法提取为代码模板