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

扫描二维码关注公众号,回复: 8726989 查看本文章

            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感兴趣的同学加入.

发布了2 篇原创文章 · 获赞 3 · 访问量 192

猜你喜欢

转载自blog.csdn.net/a451319296/article/details/104039087