找回密码
 立即注册
查看: 269|回复: 0

Unity 大文件断点续传

[复制链接]
发表于 2022-4-27 17:34 | 显示全部楼层 |阅读模式
使用 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));
代码都在这里了,赶紧来试试吧!
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-22 13:22 , Processed in 0.091723 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表