上篇文章介绍了基于HttpWebRequest接口实现的资源请求下载方案,本篇介绍基于UnityWebRequest接口实现的方案。并且是开启协程执行的。
首先,写一个全局的协程管理控制器CoroutineManager工具类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* Author:W
* 线程工具【全局】
*/
namespace W.GameFramework.HotUpdate
{
public class CoroutineManager : MonoBehaviour
{
private static CoroutineManager _instance;
public static CoroutineManager Instance
{
get
{
if (_instance == null)
{
GameObject coroutineManagerObj = new GameObject("CoroutineTool");
coroutineManagerObj.name = "CoroutineTool";
GameObject.DontDestroyOnLoad(coroutineManagerObj);
_instance = coroutineManagerObj.AddComponent<CoroutineManager>();
}
return _instance;
}
}
void Awake()
{
if (_instance != null)
{
GameObject.Destroy(this.gameObject);
}
}
/// <summary>
/// 开启全局协程执行某函数
/// </summary>
/// <param name="delegateMethod"></param>
public void StartGlobalCoroutine(IEnumerator delegateMethod)
{
StartCoroutine(delegateMethod);
}
/// <summary>
/// 关闭全局协程
/// </summary>
/// <param name="delegateMethod"></param>
public void StopGlobalCoroutine(IEnumerator delegateMethod)
{
StopCoroutine(delegateMethod);
}
/// <summary>
/// 停止所有的协程
/// </summary>
public void StopAllGlobalCoroutines()
{
StopAllCoroutines();
}
}
}
然后是核心类UnityWebRequestHelper的实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
/*
* Author:W
* Unity引擎网络请求下载接口封装
*/
namespace W.GameFramework.HotUpdate
{
public class UnityWebHttpRequestHelper: HttpHelper
{
private FileStream fileStream = null;
private UnityWebRequest headRequest = null;
private UnityWebRequest webRequest = null;
public override void SendHttpRequest(string url, string path, DownloadType downloadType = DownloadType.New, OnDownloadBeginHandler onDownloadBeginHandler = null, OnDownloadEndHandler onDownloadEndHandler = null,
OnDownloadProgressHanlder onDownloadProgressHanlder = null, OnDownloadErrorHandler onDownloadErrorHandler = null)
{
base.SendHttpRequest(url, path, downloadType, onDownloadBeginHandler, onDownloadEndHandler,onDownloadProgressHanlder, onDownloadErrorHandler);
CoroutineManager.Instance.StartGlobalCoroutine(SendUnityWebHttpRequest());
}
/// <summary>
/// 开启协程请求下载资源
/// </summary>
/// <returns></returns>
IEnumerator SendUnityWebHttpRequest()
{
//先发一次请求,只获取关于资源大小的Head
headRequest = UnityWebRequest.Head(url);
yield return headRequest.SendWebRequest();
if (headRequest.isNetworkError || headRequest.isHttpError)
{
if (OnDownloadErrorHandler != null)
OnDownloadErrorHandler(headRequest.error);
}
else
{
fileLength = long.Parse(headRequest.GetResponseHeader("Content-Length"));
if (downloadType == DownloadType.New)
{
if (File.Exists(path))
File.Delete(path);
}
fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
fileCurLength = fileStream.Length;
progress = (float)fileCurLength / fileLength;
if (OnDownloadBeginHandler != null)
OnDownloadBeginHandler();
//当前请求下载的资源未完成下载,继续下载
if (fileCurLength < fileLength)
{
fileStream.Seek(fileCurLength, SeekOrigin.Begin);
webRequest = UnityWebRequest.Get(url);
webRequest.SetRequestHeader("Range", "bytes=" + fileCurLength + "-" + fileLength);
webRequest.SendWebRequest();
if (webRequest.isNetworkError || webRequest.isHttpError)
{
if (OnDownloadErrorHandler != null)
OnDownloadErrorHandler(webRequest.error);
}
else
{
var index = 0;
while (fileCurLength < fileLength)
{
//停止请求下载
if (IsStop)
{
Clear();
break;
}
yield return null;
var buff = webRequest.downloadHandler.data;
if (buff != null)
{
var length = buff.Length - index;
fileStream.Write(buff, index, length);
index += length;
fileCurLength += length;
progress = (float)fileCurLength / fileLength;
}
}
}
}
else
{
progress = 1f;
if (OnDownloadEndHandler != null)
OnDownloadEndHandler();
}
Clear();
}
}
public override void Clear()
{
if (fileStream != null)
{
fileStream.Flush();
fileStream.Close();
fileStream = null;
}
if (webRequest != null)
{
webRequest.Abort();
webRequest = null;
}
if (headRequest != null)
{
headRequest.Abort();
headRequest = null;
}
CoroutineManager.Instance.StopGlobalCoroutine(SendUnityWebHttpRequest());
}
}
}
下篇文章 https://blog.csdn.net/wlqchengzhangji/article/details/118705159