Unity 学习分享

Unity 大文件断点续传

2022-04-26  本文已影响0人  游戏创作者

使用 UnityWebRequest 下载大文件时我们需要额外的一些处理,由于设备运行内存有限,下载大文件就得采用实时下载写入,实时释放内存的方法。考虑到网络环境不稳定时下载被迫中断的情况,重新从头下载会导致不必要的资源浪费,断点续传也很有必要。UnityWebRequest 的 DownloadHandlerFile 可以做到实时下载写入,不必占用整个文件大小的内存。在 Unity 新版本中,DownloadHandlerFile 新增的 append 参数设置为true时文件写入方式为接续写入,不覆盖原文件。首先要获取服务器将要下载文件的大小这时可用 UnityWebRequest.Head 再通过 GetResponseHeader("Content-Length") 获取文件大小。需要注意的是如果服务器文件会有变动,可以自己加入数据校验,校验通过再续传文件。还有确保下载的文件URL是可以直接下载文件而不是重定向的 url 。

下面是一段演示代码:

IEnumerator StartDownLoadApk(string url, string localPath)
  {
    Debug.Log("下载地址:" + url + ",保存路径:" + localPath);
    UnityWebRequest headRequest = UnityWebRequest.Head(url);
    yield return headRequest.SendWebRequest();
    if (!string.IsNullOrEmpty(headRequest.error))
    {
      Debug.LogError("获取下载的文件大小失败");
      yield break;
    }
    ulong totalLength = ulong.Parse(headRequest.GetResponseHeader("Content-Length"));//获取文件总大小
    Debug.Log("获取大小" + totalLength);
    headRequest.Dispose();
    UnityWebRequest Request = UnityWebRequest.Get(url);
    Request.downloadHandler = new DownloadHandlerFile(localPath, true);//append设置为true文件写入方式为接续写入,不覆盖原文件。
    FileInfo file = new FileInfo(localPath);
    ulong fileLength = (ulong)file.Length;
    Debug.Log("文件总共大小:" + fileLength + ",totalLength=" + totalLength);
    //设置文件从什么位置开始下载和写入
    Request.SetRequestHeader("Range", "bytes=" + fileLength + "-");
    if (fileLength < totalLength)
    {
      Request.SendWebRequest();
      while (!Request.isDone)
      {
        double p = (Request.downloadedBytes + fileLength) / (double)totalLength;
        //进度条.value = (float)progress;
        Debug.Log("下载进度:" + p);
        yield return null;
      }
    }
    if (string.IsNullOrEmpty(Request.error))
    {
      Debug.Log("下载成功:" + localPath);
    }
    else
    {
      Debug.LogError("下载失败:" + Request.error);
    }
    Request.Dispose();
  }

调用:

string url = "http://xrlmall.top/PuzzleHero/PuzzleHero.apk";
string localPath = Application.persistentDataPath + "/test.apk";
StartCoroutine(StartDownLoadApk(url, localPath));

代码都在这里了,赶紧来试试吧!

上一篇 下一篇

猜你喜欢

热点阅读