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("源目錄不存在");
        }
    }

 

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM