最近有个项目需要在服务器下载视频资源到本地,然后在本地播放视频,一开始看Unity官方文档找到资源下载的方法,但是在下载超大资源(一个2.8G的.mp4格式的视频)的时候回出现未知错误导致视频不能下载下来,先看官方提供的方法如下。
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class FileDownloader : MonoBehaviour {
void Start () {
StartCoroutine(DownloadFile());
IEnumerator DownloadFile() {
var uwr = new UnityWebRequest("https://unity3d.com/", UnityWebRequest.kHttpVerbGET);
string path = Path.Combine(Application.persistentDataPath, "unity3d.html");
uwr.downloadHandler = new DownloadHandlerFile(path);
yield return uwr.SendWebRequest();
if (uwr.result != UnityWebRequest.Result.Success)
Debug.LogError(uwr.error);
Debug.Log("File successfully downloaded and saved to " + path);
于是我们采用断点续传和分段下载来实现超大资源的下载。
户端软件断点续传指的是在下载或上传时,将下载或上传任务(一个文件或一个压缩包)人为的划分为几个部分,每一个部分采用一个线程进行上传或下载,如果碰到网络故障,可以从已经上传或下载的部分开始继续上传下载未完成的部分,而没有必要从头开始上传下载。用户可以节省时间,提高速度。
获取到资源的总长度,获取到当前已经下载的长度,当下载中断再次请求下载的时候,先判断有没有下载完成,如果没有下载完成,从当前位置开始,下载剩下的资源。
GetResponseHeader("Content-Length")获取到资源的大小,下图中name=ueno&age=37正好是16个字节。这样我们就能获取到资源的总字节数。
SetRequestHeader("Range", "bytes=" + fileLength + "-")请求网络数据从第fileLength到最后的字节;
代码注释已经比较详细
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class FileDownloader : MonoBehaviour
public Text text;
private string MyURL = "http://education.jnaw.top/lubozhibouploaded/quanjing/1615791915263.mp4";
//private string MyURL = "http://education.jnaw.top/lubozhibouploaded/quanjing/1629796505018.mp4";
//十亿字节为一段
double loadedBytes = 1000000000;
void Start()
Debug.Log(Application.persistentDataPath);
StartCoroutine(BreakpointResume(MyURL, Application.persistentDataPath + "/MP4/1615791915263.mp4"));
/// <summary>
/// 分段,断点下载文件
/// </summary>
/// <param name="loadPath">下载地址</param>
/// <param name="savePath">保存路径</param>
/// <returns></returns>
IEnumerator BreakpointResume(string loadPath, string savePath)
//UnityWebRequest 经配置可传输 HTTP HEAD 请求的 UnityWebRequest。
UnityWebRequest headRequest = UnityWebRequest.Head(loadPath);
//开始与远程服务器通信。
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(loadPath);
//append设置为true文件写入方式为接续写入,不覆盖原文件。
Request.downloadHandler = new DownloadHandlerFile(savePath, true);
//创建文件
FileInfo file = new FileInfo(savePath);
//当前下载的文件长度
ulong fileLength = (ulong)file.Length;
//请求网络数据从第fileLength到最后的字节;
Request.SetRequestHeader("Range", "bytes=" + fileLength + "-");
if (!string.IsNullOrEmpty(Request.error))
Debug.LogError("下载失败" + Request.error);
if (fileLength < totalLength)
Request.SendWebRequest();
while (!Request.isDone)
double progress = (Request.downloadedBytes + fileLength) / (double)totalLength;
text.text = (progress * 100 + 0.01f).ToString("f2") + "%";
// Debug.Log("下载量" + Request.downloadedBytes);
//超过一定的字节关闭现在的协程,开启新的协程,将资源分段下载
if (Request.downloadedBytes >= loadedBytes)
StopCoroutine("BreakpointResume");
//如果 UnityWebRequest 在进行中,就停止。
Request.Abort();
if (!string.IsNullOrEmpty(Request.error))
Debug.LogError("下载失败" + Request.error);
yield return StartCoroutine(BreakpointResume(loadPath, savePath));
yield return null;
if (string.IsNullOrEmpty(Request.error))
Debug.Log("下载成功" + savePath);
text.text = "100%";
//表示不再使用此 UnityWebRequest,并且应清理它使用的所有资源。
Request.Dispose();
最近有个项目需要在服务器下载视频资源到本地,然后在本地播放视频,一开始看Unity官方文档找到资源下载的方法,但是在下载超大资源(一个2.8G的.mp4格式的视频)的时候回出现未知错误导致视频不能下载下来,先看官方提供的方法如下。using System.Collections;using System.IO;using UnityEngine;using UnityEngine.Networking;public class FileDownloader : MonoBehaviour {
为测试在本地搭建IIS服务器(本地搭建IIS服务器方法),将需要下载的文件拷贝到指定位置,通过url即可下载文件到指定的文件夹目录中。测试代码如下
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
PS:其实UnityWebRequest本身就支持多文件下载,注意不要使用全局的UnityWebRequest对象,不然会导致downloadHandler.data数据错乱,导致写入本地的文件错误。
Unity 工具类 之 WWW/UnityWebRequest 网络下载压缩文件(zip),解压到本地,且加载使用解压数据的简单案例(内也含压缩文件例子)
一、简单介绍
二、实现原理
三、注意事项
四、效果预览
五、实现步骤
六、关键代码
一、简单介绍
Unity 工具类,自己整理的一些游戏开发可能用到的模块,单...
Unity 最新
UnityWebRequest下载网络
资源,支持
断点续传、多文件同时
下载,同时显示
下载进度,和 显示网速,今天贴出来和大家分享
显示网速图片
附上案例链接 可
下载
title: unity-UnityWebRequest断点续传
categories: Unity3d
tags: [unity, UnityWebRequest, 下载, 断点续传]
date: 2020-03-14 22:31:28
comments: false
适用于下载较大的文件, 弱网环境, 及 网络抖动 的情况.
http 下载时, 一旦网络遇到断掉 (网络切换) ...