feat(DownloadSystem):支持自定义下载请求

pull/70/head
huanggongming 2023-02-24 12:18:18 +08:00
parent 0232e5adec
commit eb16ba8365
7 changed files with 54 additions and 5 deletions

View File

@ -0,0 +1,28 @@
using JetBrains.Annotations;
using UnityEngine.Networking;
namespace YooAsset
{
public delegate UnityWebRequest DownloadRequestDelegate(string url);
/// <summary>
/// 自定义下载器的请求
/// </summary>
internal static class DownloadRequestUtil
{
[CanBeNull] private static DownloadRequestDelegate _downloadRequestDelegate;
public static void SetRequestDelegate(DownloadRequestDelegate requestDelegate)
{
_downloadRequestDelegate = requestDelegate;
}
public static UnityWebRequest NewRequest(string requestURL)
{
return _downloadRequestDelegate != null
? _downloadRequestDelegate?.Invoke(requestURL)
: new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3b21da9f048045e2af56d9d396b11c2f
timeCreated: 1677207036

View File

@ -32,6 +32,15 @@ namespace YooAsset
/// </summary>
public static List<long> ClearFileResponseCodes { set; get; }
/// <summary>
/// 自定义下载请求
/// </summary>
/// <param name="requestDelegate"></param>
public static void SetRequestDelegate(DownloadRequestDelegate requestDelegate)
{
DownloadRequestUtil.SetRequestDelegate(requestDelegate);
}
/// <summary>
/// 更新所有下载器
/// </summary>

View File

@ -80,7 +80,7 @@ namespace YooAsset
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
_webRequest = DownloadRequestUtil.NewRequest(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_tempFilePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
@ -109,11 +109,11 @@ namespace YooAsset
}
#if UNITY_2019_4_OR_NEWER
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
_webRequest = DownloadRequestUtil.NewRequest(_requestURL);
var handler = new DownloadHandlerFile(_tempFilePath, true);
handler.removeFileOnAbort = false;
#else
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
_webRequest = UnityWebRequestUtil.NewRequest(_requestURL);
var handler = new DownloadHandlerFileRange(_tempFilePath, _bundleInfo.Bundle.FileSize, _webRequest);
_downloadHandle = handler;
#endif

View File

@ -29,7 +29,7 @@ namespace YooAsset
if (_webRequest == null)
{
URL = url;
_webRequest = new UnityWebRequest(URL, UnityWebRequest.kHttpVerbGET);
_webRequest = DownloadRequestUtil.NewRequest(URL);
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;

View File

@ -39,7 +39,7 @@ namespace YooAsset
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
_webRequest = new UnityWebRequest(URL, UnityWebRequest.kHttpVerbGET);
_webRequest = DownloadRequestUtil.NewRequest(URL);
DownloadHandlerFile handler = new DownloadHandlerFile(savePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;

View File

@ -168,6 +168,15 @@ namespace YooAsset
DownloadSystem.BreakpointResumeFileSize = fileBytes;
}
/// <summary>
/// 自定义下载请求
/// </summary>
/// <param name="requestDelegate"></param>
public static void SetDownloadSystemUnityWebRequest(DownloadRequestDelegate requestDelegate)
{
DownloadSystem.SetRequestDelegate(requestDelegate);
}
/// <summary>
/// 设置下载系统参数下载失败后清理文件的HTTP错误码
/// </summary>