• Unity 获取指定路径文件夹下所有文件集合(递归写法)(一)


    写法模板:

    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;
            }
    
        }

    欢迎各位指正交流,如有兴趣的可以把这个写法提取为代码模板

  • 相关阅读:
    (七)微信小程序:收藏功能
    (六)微信小程序:制作新闻详情页
    (五)微信小程序:模板template
    (四)微信小程序:新闻列表渲染
    (三)微信小程序:焦点轮播图功能
    (二)微信小程序:实现页面跳转
    Docker和jenkins实现springboot自动部署
    (桥接)完美解决linux设置静态ip。
    一个简单的对任意list分页的工具-----PageUtil
    java8实战二------lambda表达式和函数式接口,简单就好
  • 原文地址:https://www.cnblogs.com/Roz-001/p/13749685.html
Copyright © 2020-2023  润新知