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 @ 2021-02-26 10:29  三页菌  阅读(606)  评论(0编辑  收藏  举报