unity文件夹复制

如果是编辑器不使用运行时的话,直接使用UnityEditor下的API即可

FileUtil.CopyFileOrDirectory

如果是运行时

复制代码
    /// <summary>
    /// 文件夹拷贝
    /// </summary>
    /// <param name="sourcePath">源路径</param>
    /// <param name="destPath">目标路径</param>
    private void CopyFolder(string sourcePath, string destPath)
    {
        if (Directory.Exists(sourcePath))
        {
            if (!Directory.Exists(destPath))
            {
                try
                {
                    Directory.CreateDirectory(destPath);
                }
                catch (Exception ex)
                {
                    Debug.LogError("创建失败");
                }
            }
            
            List<string> files = new List<string>(Directory.GetFiles(sourcePath));
            files.ForEach(c =>
            {
                //排除meta文件
                if (!c.EndsWith(".meta"))
                {
                    string destFile = Path.Combine(destPath, Path.GetFileName(c));
                    File.Copy(c, destFile, true);
                }
            });
            List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));
            folders.ForEach(c =>
            {
                string destDir = Path.Combine(destPath,Path.GetFileName(c));
                CopyFolder(c, destDir);
            });
        }
        else
        {
            Debug.LogError("源目录不存在");
        }
    }
复制代码

 

 

 


posted @   三页菌  阅读(1100)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示