Unity拷貝文件到指定路徑


主要用於將StreamingAssets文件拷貝到persistentDataPath路徑下,應為比較簡單我就直接上代碼了。

 

using System.IO;
using UnityEngine;
using System;

/// <summary>拷貝文件</summary>
public class CopyFiles
{
    /// <summary>
    /// 拷貝文件到指定路徑(需要加文件后綴)
    /// </summary>
    /// <param name="pStrFilePath">需要拷貝文件的路徑</param>
    /// <param name="pPerFilePath">拷貝到路徑</param>
    /// <param name="finish">結束回調</param>
    public static void Copy(string pStrFilePath, string pPerFilePath, Action<string> finish = null)
    {
        if (string.IsNullOrEmpty(pStrFilePath) || string.IsNullOrEmpty(pPerFilePath))
        {
            Debug.LogWarning("CopyFiles/Copy/" + "copy file wrong! file path is null!");
            return;
        }

        if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.OSXEditor)
        {
            pStrFilePath = @"file://" + pStrFilePath;
        }

        else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            pStrFilePath = @"file:///" + pStrFilePath;
        }

        string[] tempPerFilePathArr = pPerFilePath.Split('/');
        string tempPerFilePath = pPerFilePath.Replace(tempPerFilePathArr[tempPerFilePathArr.Length - 1], null);
        if (!Directory.Exists(tempPerFilePath)) Directory.CreateDirectory(tempPerFilePath);

        WWW ww = new WWW(pStrFilePath);

        while (!ww.isDone) { Debug.Log(ww.progress); }
        if (string.IsNullOrEmpty(ww.error))
        {
            var buffer = ww.bytes;
            if (File.Exists(pPerFilePath))
                File.Delete(pPerFilePath);
            var ws = File.Create(pPerFilePath);
            ws.Write(buffer, 0, buffer.Length);
            ws.Close();

            if (finish != null) finish(null);

            Debug.Log("CopyFiles/Copy/" + "copy file success:" + pPerFilePath);
        }
        else
        {
            if (finish != null) finish(ww.error);

            Debug.LogWarning("CopyFiles/Copy/" + "copy file wrong !!!!   " + ww.error);
        }
        ww.Dispose();
    }

}

 

代碼的使用:(下面代碼參考使用,涉及到其他模塊代碼我沒有貼出來,主要是先查找到需要下載資源的清單使用遞歸進行下載)

  private int copyFileIndex = 0;


    /// <summary>拷貝資源文件</summary>
    private void CheckingAsset()
    {
        string localAssetListStr = ResoucesMgr.Instance.Load<TextAsset>(FilePaths.LocalAssetListPath, false).text;
        CopyFileInfos localCopyFileInfos = JsonUtility.FromJson<CopyFileInfos>(localAssetListStr);
        if (localCopyFileInfos == null || localCopyFileInfos.AssetListConfig == null || localCopyFileInfos.AssetListConfig.Count < 1)
        {
            LoadScene(ScenesName.MainScene);
            return;
        }

        copyFileIndex = 0;
        List<CopyFileInfo> localAssetListConfig = localCopyFileInfos.AssetListConfig;
        CopyFile(localAssetListConfig);
    }

    private void CopyFile(List<CopyFileInfo> assetListConfig)
    {
        if (copyFileIndex > assetListConfig.Count - 1)
        {
            LoadScene(ScenesName.MainScene);
            return;
        }

        CopyFileInfo info = assetListConfig[copyFileIndex];

        string strFilePath = Application.streamingAssetsPath + "/" + info.FolderPath + "/" + info.Name;
        string perFilePath = Application.persistentDataPath + "/" + info.FolderPath + "/" + info.Name;
        CopyFiles.Copy(strFilePath, perFilePath, (error) =>
         {
             if (!string.IsNullOrEmpty(error))
             {
                 DebugManager.LogWarning(GetType() + "/CopyFile()/ Copy file error! ErrorInfo:" + error);
             }

             if (copyFileIndex == assetListConfig.Count - 1) { LoadScene(ScenesName.MainScene); }
             else
             {
                 copyFileIndex++;
                 CopyFile(assetListConfig);
             }
         });
    }

    /// <summary>加載場景</summary>
    private void LoadScene(string sceneName)
    {
        UserModel.IsCopyFile = true;
        UserModel.NextSceneName = sceneName;
        SceneManager.LoadScene(ScenesName.LoadScene);
    }


PS:Unity2019對WWW棄用了,換成了新的API:UnityWebReques ;如果拷貝文件比較大需要知道拷貝進度可以加上進度回調,后期有時間在更改。

Unity QQ交流群:299412191 歡迎對Unity感興趣的同學加入.


免責聲明!

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



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