refactor : 重构网络下载模块

This commit is contained in:
何冠峰
2026-01-07 10:23:11 +08:00
parent 454afc9ba6
commit ee67a55c0f
20 changed files with 215 additions and 95 deletions

View File

@@ -12,7 +12,7 @@ namespace YooAsset
/// </remarks> /// </remarks>
internal sealed class UnityWebRequestBackend : IDownloadBackend internal sealed class UnityWebRequestBackend : IDownloadBackend
{ {
private readonly UnityWebRequestDelegate _webRequestCreator; private readonly UnityWebRequestCreator _webRequestCreator;
/// <summary> /// <summary>
/// 后端名称 /// 后端名称
@@ -36,7 +36,7 @@ namespace YooAsset
/// 自定义 UnityWebRequest 创建委托(可选)。 /// 自定义 UnityWebRequest 创建委托(可选)。
/// 如果为 null则使用默认的 UnityWebRequest 构造方式。 /// 如果为 null则使用默认的 UnityWebRequest 构造方式。
/// </param> /// </param>
public UnityWebRequestBackend(UnityWebRequestDelegate webRequestCreator) public UnityWebRequestBackend(UnityWebRequestCreator webRequestCreator)
{ {
_webRequestCreator = webRequestCreator; _webRequestCreator = webRequestCreator;
} }

View File

@@ -0,0 +1,10 @@
using UnityEngine.Networking;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 自定义下载器的请求委托
/// </summary>
public delegate UnityWebRequest UnityWebRequestCreator(string url);
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0fa3e6346decc4d4db3b03e6ae93e57a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -26,7 +26,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="args">AssetBundle 下载参数</param> /// <param name="args">AssetBundle 下载参数</param>
/// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param> /// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param>
public UnityWebRequestAssetBundleDownloader(DownloadAssetBundleRequestArgs args, UnityWebRequestDelegate webRequestCreator) public UnityWebRequestAssetBundleDownloader(DownloadAssetBundleRequestArgs args, UnityWebRequestCreator webRequestCreator)
: base(args.URL, webRequestCreator) : base(args.URL, webRequestCreator)
{ {
_args = args; _args = args;
@@ -75,17 +75,12 @@ namespace YooAsset
} }
else else
{ {
// 使用 Unity 缓存(需要 FileHash // 使用 Unity 缓存
// The file hash defining the version of the asset bundle. // 说明:The file hash defining the version of the asset bundle.
Hash128 fileHash = Hash128.Parse(_args.FileHash); Hash128 fileHash = Hash128.Parse(_args.FileHash);
handler = new DownloadHandlerAssetBundle(URL, fileHash, _args.UnityCRC); handler = new DownloadHandlerAssetBundle(URL, fileHash, _args.UnityCRC);
} }
#if UNITY_2020_3_OR_NEWER
// 禁用自动加载,允许手动控制加载时机
handler.autoLoadAssetBundle = false;
#endif
return handler; return handler;
} }
} }

View File

@@ -23,7 +23,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="args">数据下载参数</param> /// <param name="args">数据下载参数</param>
/// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param> /// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param>
public UnityWebRequestBytesDownloader(DownloadDataRequestArgs args, UnityWebRequestDelegate webRequestCreator) public UnityWebRequestBytesDownloader(DownloadDataRequestArgs args, UnityWebRequestCreator webRequestCreator)
: base(args.URL, webRequestCreator) : base(args.URL, webRequestCreator)
{ {
_args = args; _args = args;

View File

@@ -13,7 +13,7 @@ namespace YooAsset
/// </remarks> /// </remarks>
internal abstract class UnityWebRequestDownloaderBase : IDownloadRequest internal abstract class UnityWebRequestDownloaderBase : IDownloadRequest
{ {
private readonly UnityWebRequestDelegate _webRequestCreator; private readonly UnityWebRequestCreator _webRequestCreator;
protected UnityWebRequest _webRequest; protected UnityWebRequest _webRequest;
// 看门狗相关 // 看门狗相关
@@ -76,7 +76,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="url">请求地址</param> /// <param name="url">请求地址</param>
/// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param> /// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param>
protected UnityWebRequestDownloaderBase(string url, UnityWebRequestDelegate webRequestCreator) protected UnityWebRequestDownloaderBase(string url, UnityWebRequestCreator webRequestCreator)
{ {
URL = url; URL = url;
_webRequestCreator = webRequestCreator; _webRequestCreator = webRequestCreator;

View File

@@ -26,7 +26,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="args">文件下载参数</param> /// <param name="args">文件下载参数</param>
/// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param> /// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param>
public UnityWebRequestFileDownloader(DownloadFileRequestArgs args, UnityWebRequestDelegate webRequestCreator) public UnityWebRequestFileDownloader(DownloadFileRequestArgs args, UnityWebRequestCreator webRequestCreator)
: base(args.URL, webRequestCreator) : base(args.URL, webRequestCreator)
{ {
_args = args; _args = args;

View File

@@ -69,7 +69,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="args">数据下载参数</param> /// <param name="args">数据下载参数</param>
/// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param> /// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param>
public UnityWebRequestHeadDownloader(DownloadDataRequestArgs args, UnityWebRequestDelegate webRequestCreator) public UnityWebRequestHeadDownloader(DownloadDataRequestArgs args, UnityWebRequestCreator webRequestCreator)
: base(args.URL, webRequestCreator) : base(args.URL, webRequestCreator)
{ {
_args = args; _args = args;

View File

@@ -23,7 +23,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="args">数据下载参数</param> /// <param name="args">数据下载参数</param>
/// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param> /// <param name="webRequestCreator">UnityWebRequest 创建器(可选)</param>
public UnityWebRequestTextDownloader(DownloadDataRequestArgs args, UnityWebRequestDelegate webRequestCreator) public UnityWebRequestTextDownloader(DownloadDataRequestArgs args, UnityWebRequestCreator webRequestCreator)
: base(args.URL, webRequestCreator) : base(args.URL, webRequestCreator)
{ {
_args = args; _args = args;

View File

@@ -75,6 +75,7 @@ namespace YooAsset
public VirtualFileDownloader(DownloadSimulateRequestArgs args) public VirtualFileDownloader(DownloadSimulateRequestArgs args)
{ {
_args = args; _args = args;
URL = args.URL;
Status = EDownloadRequestStatus.None; Status = EDownloadRequestStatus.None;
} }
@@ -142,7 +143,7 @@ namespace YooAsset
#if UNITY_2020_3_OR_NEWER #if UNITY_2020_3_OR_NEWER
return UnityEngine.Time.realtimeSinceStartupAsDouble; return UnityEngine.Time.realtimeSinceStartupAsDouble;
#else #else
return = UnityEngine.Time.realtimeSinceStartup; return UnityEngine.Time.realtimeSinceStartup;
#endif #endif
} }
} }

View File

@@ -3,32 +3,8 @@ using UnityEngine;
namespace YooAsset namespace YooAsset
{ {
/// <summary>
/// 自定义下载器的请求委托
/// </summary>
public delegate UnityWebRequest UnityWebRequestDelegate(string url);
internal class DownloadSystemHelper internal class DownloadSystemHelper
{ {
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void OnRuntimeInitialize()
{
UnityWebRequestCreater = null;
}
#endif
public static UnityWebRequestDelegate UnityWebRequestCreater = null;
public static UnityWebRequest NewUnityWebRequestGet(string requestURL)
{
UnityWebRequest webRequest;
if (UnityWebRequestCreater != null)
webRequest = UnityWebRequestCreater.Invoke(requestURL);
else
webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET);
return webRequest;
}
/// <summary> /// <summary>
/// 获取WWW加载本地资源的路径 /// 获取WWW加载本地资源的路径
/// </summary> /// </summary>

View File

@@ -2,7 +2,7 @@
## 模块概述 ## 模块概述
DownloadSystem 是 YooAsset 资源管理系统的**底层网络下载层**,负责处理所有 HTTP 网络请求。该模块提供了统一的下载接口抽象,支持文件下载、断点续传、并发控制、看门狗监控等功能。 DownloadSystem 是 YooAsset 资源管理系统的**底层网络下载层**,负责处理所有 HTTP 网络请求。该模块提供了统一的下载接口抽象,支持文件下载、断点续传、并发请求(由上层调度)、看门狗监控等功能。
### 核心职责 ### 核心职责
@@ -14,13 +14,23 @@ DownloadSystem 是 YooAsset 资源管理系统的**底层网络下载层**,负
--- ---
## 边界与上层协作
DownloadSystem 的职责是提供“可替换后端 + 统一请求接口 + 轮询式生命周期”的基础能力:
- **本模块不负责并发队列/限流调度**:并发通常由上层同时创建多个 request 并自行控制并发数。
- **本模块不负责重试/回退策略**:失败后的重试、切换 CDN、降级等策略通常由上层系统实现。
- **本模块不负责持久化下载任务**:断点续传依赖本地已有文件与 `Range` 请求头,并由上层管理断点信息。
---
## 设计目标 ## 设计目标
| 目标 | 说明 | | 目标 | 说明 |
|------|------| |------|------|
| **可扩展性** | 支持可插拔的网络库后端UnityWebRequest/BestHTTP/自研) | | **可扩展性** | 支持可插拔的网络库后端UnityWebRequest/BestHTTP/自研) |
| **鲁棒性** | 看门狗超时保护、自动清理失败文件、完整错误信息 | | **鲁棒性** | 看门狗超时保护、自动清理失败文件、完整错误信息 |
| **高性能** | 轮询模式无阻塞、支持并发请求 | | **高性能** | 轮询模式无阻塞、支持并发请求(并发数由上层调度) |
| **易用性** | 流畅的参数构建 API、清晰的状态转换 | | **易用性** | 流畅的参数构建 API、清晰的状态转换 |
--- ---
@@ -70,7 +80,8 @@ DownloadSystem/
│ └── IDownloadRequest.cs # 请求接口层次结构 │ └── IDownloadRequest.cs # 请求接口层次结构
├── DefaultDownloadBackend/ # 默认后端实现 ├── DefaultDownloadBackend/ # 默认后端实现
── UnityWebRequestBackend.cs # UnityWebRequest 后端 ── UnityWebRequestBackend.cs # UnityWebRequest 后端
│ └── UnityWebRequestCreator.cs # UnityWebRequest 创建委托
├── DefaultDownloadRequest/ # 默认请求实现 ├── DefaultDownloadRequest/ # 默认请求实现
│ ├── UnityWebRequestDownloaderBase.cs # 基础下载器(抽象类) │ ├── UnityWebRequestDownloaderBase.cs # 基础下载器(抽象类)
@@ -256,13 +267,15 @@ public struct DownloadSimulateRequestArgs
- 无需手动调用 Update()UnityWebRequest 自动驱动 - 无需手动调用 Update()UnityWebRequest 自动驱动
```csharp ```csharp
// 自定义 UnityWebRequest 创建 // 自定义 UnityWebRequest 创建(建议通过 backend 构造函数传入)
DownloadSystemHelper.UnityWebRequestCreater = (url) => UnityWebRequestCreator creator = (url) =>
{ {
var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET); var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
// 自定义配置... // 自定义配置...
return request; return request;
}; };
IDownloadBackend backend = new UnityWebRequestBackend(creator);
``` ```
### UnityWebRequestDownloaderBase ### UnityWebRequestDownloaderBase
@@ -303,12 +316,11 @@ None ──► SendRequest() ──► Running ──► PollingRequest() ──
```csharp ```csharp
// 1. 创建后端和请求 // 1. 创建后端和请求
IDownloadBackend backend = new UnityWebRequestBackend(); IDownloadBackend backend = new UnityWebRequestBackend();
var args = new DownloadFileRequestArgs var args = new DownloadFileRequestArgs(
{ url: "https://example.com/file.zip",
URL = "https://example.com/file.zip", savePath: "/path/to/save/file.zip",
SavePath = "/path/to/save/file.zip", timeout: 30,
Timeout = 30 watchdogTime: 0);
};
IDownloadFileRequest request = backend.CreateFileRequest(args); IDownloadFileRequest request = backend.CreateFileRequest(args);
// 2. 发起并轮询 // 2. 发起并轮询
@@ -340,14 +352,14 @@ request.Dispose();
// 获取已下载的文件大小 // 获取已下载的文件大小
long existingFileSize = new FileInfo(savePath).Length; long existingFileSize = new FileInfo(savePath).Length;
var args = new DownloadFileRequestArgs var args = new DownloadFileRequestArgs(
{ url: url,
URL = url, savePath: savePath,
SavePath = savePath, timeout: 30,
ResumeFromBytes = existingFileSize, // 断点位置 watchdogTime: 0,
AppendToFile = true, // 追加写入 appendToFile: true, // 追加写入
RemoveFileOnAbort = false // 中止时保留文件 removeFileOnAbort: false, // 中止时保留文件
}; resumeFromBytes: existingFileSize); // 断点位置
IDownloadFileRequest request = backend.CreateFileRequest(args); IDownloadFileRequest request = backend.CreateFileRequest(args);
request.SendRequest(); request.SendRequest();
@@ -357,12 +369,11 @@ request.SendRequest();
### 看门狗保护 ### 看门狗保护
```csharp ```csharp
var args = new DownloadFileRequestArgs var args = new DownloadFileRequestArgs(
{ url: url,
URL = url, savePath: path,
SavePath = path, timeout: 30,
WatchdogTime = 30 // 30秒无数据自动中止 watchdogTime: 30); // 30秒无数据自动中止
};
IDownloadFileRequest request = backend.CreateFileRequest(args); IDownloadFileRequest request = backend.CreateFileRequest(args);
request.SendRequest(); request.SendRequest();
@@ -382,10 +393,10 @@ if (request.Status == EDownloadRequestStatus.Aborted)
### HEAD 请求获取文件信息 ### HEAD 请求获取文件信息
```csharp ```csharp
var args = new DownloadDataRequestArgs var args = new DownloadDataRequestArgs(
{ url: "https://example.com/file.zip",
URL = "https://example.com/file.zip" timeout: 30,
}; watchdogTime: 0);
IDownloadHeadRequest request = backend.CreateHeadRequest(args); IDownloadHeadRequest request = backend.CreateHeadRequest(args);
request.SendRequest(); request.SendRequest();
@@ -408,10 +419,10 @@ if (request.Status == EDownloadRequestStatus.Succeed)
### 下载字节数据 ### 下载字节数据
```csharp ```csharp
var args = new DownloadDataRequestArgs var args = new DownloadDataRequestArgs(
{ url: "https://example.com/data.json",
URL = "https://example.com/data.json" timeout: 30,
}; watchdogTime: 0);
IDownloadBytesRequest request = backend.CreateBytesRequest(args); IDownloadBytesRequest request = backend.CreateBytesRequest(args);
request.SendRequest(); request.SendRequest();
@@ -519,9 +530,9 @@ VirtualFileDownloader (独立实现) ──► IDownloadFileRequest
提供跨平台的工具函数: 提供跨平台的工具函数:
| 方法 | 说明 | | 成员 | 说明 |
|------|------| |------|------|
| `NewUnityWebRequestGet()` | 创建 GET 请求(支持自定义工厂 | | `UnityWebRequestCreater` | 兼容保留的全局 UnityWebRequest 创建委托(默认文件系统不再读取,推荐改为 FileSystemParameters 注入 |
| `ConvertToWWWPath()` | 转换本地路径为 WWW 协议 URL | | `ConvertToWWWPath()` | 转换本地路径为 WWW 协议 URL |
| `IsRequestLocalFile()` | 判断是否本地文件请求 | | `IsRequestLocalFile()` | 判断是否本地文件请求 |
@@ -543,6 +554,9 @@ int count = WebRequestCounter.GetRequestFailedCount(packageName, eventName);
1. **资源释放**:使用完毕后务必调用 `Dispose()` 释放资源 1. **资源释放**:使用完毕后务必调用 `Dispose()` 释放资源
2. **断点续传**:需要服务器支持 `Range` 请求头和 `206 Partial Content` 响应 2. **断点续传**:需要服务器支持 `Range` 请求头和 `206 Partial Content` 响应
- 若服务端不支持 Range 仍返回 200全量内容可能会被追加写入导致文件损坏
3. **看门狗超时**:设置为 0 表示禁用,建议根据网络环境设置合理值 3. **看门狗超时**:设置为 0 表示禁用,建议根据网络环境设置合理值
4. **内存下载**`IDownloadBytesRequest` 会将整个响应体加载到内存,不适合大文件 4. **内存下载**`IDownloadBytesRequest` 会将整个响应体加载到内存,不适合大文件
5. **线程安全**所有下载请求的创建和轮询应在主线程进行 5. **驱动更新**部分第三方网络库实现的 backend 可能需要每帧调用 `IDownloadBackend.Update()` 进行驱动
6. **中止语义**`Aborted` 可能来自用户主动 `AbortRequest()` 或看门狗超时;中止场景下 `HttpCode/Error` 可能为默认值(例如 0/空)
7. **线程安全**:所有下载请求的创建和轮询应在主线程进行

View File

@@ -58,6 +58,11 @@ namespace YooAsset
} }
#region #region
/// <summary>
/// 自定义参数UnityWebRequest 创建委托
/// </summary>
public UnityWebRequestCreator WebRequestCreator { private set; get; }
/// <summary> /// <summary>
/// 自定义参数:覆盖安装缓存清理模式 /// 自定义参数:覆盖安装缓存清理模式
/// </summary> /// </summary>
@@ -178,7 +183,15 @@ namespace YooAsset
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParametersDefine.INSTALL_CLEAR_MODE) if (name == FileSystemParametersDefine.DOWNLOAD_BACKEND)
{
DownloadBackend = (IDownloadBackend)value;
}
else if (name == FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR)
{
WebRequestCreator = (UnityWebRequestCreator)value;
}
else if (name == FileSystemParametersDefine.INSTALL_CLEAR_MODE)
{ {
InstallClearMode = (EOverwriteInstallClearMode)value; InstallClearMode = (EOverwriteInstallClearMode)value;
} }
@@ -239,12 +252,14 @@ namespace YooAsset
// 创建默认的下载后台接口 // 创建默认的下载后台接口
if (DownloadBackend == null) if (DownloadBackend == null)
DownloadBackend = new UnityWebRequestBackend(DownloadSystemHelper.UnityWebRequestCreater); DownloadBackend = new UnityWebRequestBackend(WebRequestCreator);
// 创建解压文件系统 // 创建解压文件系统
var remoteServices = new DefaultUnpackRemoteServices(_packageRoot); var remoteServices = new DefaultUnpackRemoteServices(_packageRoot);
_unpackFileSystem = new DefaultUnpackFileSystem(); _unpackFileSystem = new DefaultUnpackFileSystem();
_unpackFileSystem.SetParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); _unpackFileSystem.SetParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.DOWNLOAD_BACKEND, DownloadBackend);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR, WebRequestCreator);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, InstallClearMode); _unpackFileSystem.SetParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, InstallClearMode);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel); _unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_MAX_CONCURRENCY, FileVerifyMaxConcurrency); _unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_MAX_CONCURRENCY, FileVerifyMaxConcurrency);
@@ -255,6 +270,17 @@ namespace YooAsset
} }
public virtual void OnDestroy() public virtual void OnDestroy()
{ {
if (_unpackFileSystem != null)
{
_unpackFileSystem.OnDestroy();
_unpackFileSystem = null;
}
if (DownloadBackend != null)
{
DownloadBackend.Dispose();
DownloadBackend = null;
}
} }
public virtual bool Belong(PackageBundle bundle) public virtual bool Belong(PackageBundle bundle)
@@ -481,4 +507,4 @@ namespace YooAsset
} }
#endregion #endregion
} }
} }

View File

@@ -60,6 +60,11 @@ namespace YooAsset
} }
#region #region
/// <summary>
/// 自定义参数UnityWebRequest 创建委托
/// </summary>
public UnityWebRequestCreator WebRequestCreator { private set; get; }
/// <summary> /// <summary>
/// 自定义参数:远程服务接口的实例类 /// 自定义参数:远程服务接口的实例类
/// </summary> /// </summary>
@@ -237,7 +242,15 @@ namespace YooAsset
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParametersDefine.REMOTE_SERVICES) if (name == FileSystemParametersDefine.DOWNLOAD_BACKEND)
{
DownloadBackend = (IDownloadBackend)value;
}
else if (name == FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR)
{
WebRequestCreator = (UnityWebRequestCreator)value;
}
else if (name == FileSystemParametersDefine.REMOTE_SERVICES)
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
@@ -334,7 +347,7 @@ namespace YooAsset
// 创建默认的下载后台接口 // 创建默认的下载后台接口
if (DownloadBackend == null) if (DownloadBackend == null)
DownloadBackend = new UnityWebRequestBackend(DownloadSystemHelper.UnityWebRequestCreater); DownloadBackend = new UnityWebRequestBackend(WebRequestCreator);
} }
public virtual void OnDestroy() public virtual void OnDestroy()
{ {
@@ -343,6 +356,12 @@ namespace YooAsset
DownloadScheduler.Dispose(); DownloadScheduler.Dispose();
DownloadScheduler = null; DownloadScheduler = null;
} }
if (DownloadBackend != null)
{
DownloadBackend.Dispose();
DownloadBackend = null;
}
} }
public virtual bool Belong(PackageBundle bundle) public virtual bool Belong(PackageBundle bundle)
@@ -673,4 +692,4 @@ namespace YooAsset
} }
#endregion #endregion
} }
} }

View File

@@ -44,6 +44,11 @@ namespace YooAsset
} }
#region #region
/// <summary>
/// 自定义参数UnityWebRequest 创建委托
/// </summary>
public UnityWebRequestCreator WebRequestCreator { private set; get; }
/// <summary> /// <summary>
/// 模拟WebGL平台模式 /// 模拟WebGL平台模式
/// </summary> /// </summary>
@@ -117,7 +122,15 @@ namespace YooAsset
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParametersDefine.VIRTUAL_WEBGL_MODE) if (name == FileSystemParametersDefine.DOWNLOAD_BACKEND)
{
DownloadBackend = (IDownloadBackend)value;
}
else if (name == FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR)
{
WebRequestCreator = (UnityWebRequestCreator)value;
}
else if (name == FileSystemParametersDefine.VIRTUAL_WEBGL_MODE)
{ {
VirtualWebGLMode = Convert.ToBoolean(value); VirtualWebGLMode = Convert.ToBoolean(value);
} }
@@ -153,10 +166,15 @@ namespace YooAsset
// 创建默认的下载后台接口 // 创建默认的下载后台接口
if (DownloadBackend == null) if (DownloadBackend == null)
DownloadBackend = new UnityWebRequestBackend(DownloadSystemHelper.UnityWebRequestCreater); DownloadBackend = new UnityWebRequestBackend(WebRequestCreator);
} }
public virtual void OnDestroy() public virtual void OnDestroy()
{ {
if (DownloadBackend != null)
{
DownloadBackend.Dispose();
DownloadBackend = null;
}
} }
public virtual bool Belong(PackageBundle bundle) public virtual bool Belong(PackageBundle bundle)
@@ -247,4 +265,4 @@ namespace YooAsset
} }
#endregion #endregion
} }
} }

View File

@@ -43,6 +43,11 @@ namespace YooAsset
} }
#region #region
/// <summary>
/// 自定义参数UnityWebRequest 创建委托
/// </summary>
public UnityWebRequestCreator WebRequestCreator { private set; get; }
/// <summary> /// <summary>
/// 禁用Unity的网络缓存 /// 禁用Unity的网络缓存
/// </summary> /// </summary>
@@ -109,7 +114,15 @@ namespace YooAsset
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE) if (name == FileSystemParametersDefine.DOWNLOAD_BACKEND)
{
DownloadBackend = (IDownloadBackend)value;
}
else if (name == FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR)
{
WebRequestCreator = (UnityWebRequestCreator)value;
}
else if (name == FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE)
{ {
DisableUnityWebCache = Convert.ToBoolean(value); DisableUnityWebCache = Convert.ToBoolean(value);
} }
@@ -136,10 +149,15 @@ namespace YooAsset
// 创建默认的下载后台接口 // 创建默认的下载后台接口
if (DownloadBackend == null) if (DownloadBackend == null)
DownloadBackend = new UnityWebRequestBackend(DownloadSystemHelper.UnityWebRequestCreater); DownloadBackend = new UnityWebRequestBackend(WebRequestCreator);
} }
public virtual void OnDestroy() public virtual void OnDestroy()
{ {
if (DownloadBackend != null)
{
DownloadBackend.Dispose();
DownloadBackend = null;
}
} }
public virtual bool Belong(PackageBundle bundle) public virtual bool Belong(PackageBundle bundle)
@@ -179,4 +197,4 @@ namespace YooAsset
#region #region
#endregion #endregion
} }
} }

View File

@@ -57,6 +57,11 @@ namespace YooAsset
} }
#region #region
/// <summary>
/// 自定义参数UnityWebRequest 创建委托
/// </summary>
public UnityWebRequestCreator WebRequestCreator { private set; get; }
/// <summary> /// <summary>
/// 禁用Unity的网络缓存 /// 禁用Unity的网络缓存
/// </summary> /// </summary>
@@ -118,7 +123,15 @@ namespace YooAsset
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE) if (name == FileSystemParametersDefine.DOWNLOAD_BACKEND)
{
DownloadBackend = (IDownloadBackend)value;
}
else if (name == FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR)
{
WebRequestCreator = (UnityWebRequestCreator)value;
}
else if (name == FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE)
{ {
DisableUnityWebCache = Convert.ToBoolean(value); DisableUnityWebCache = Convert.ToBoolean(value);
} }
@@ -146,10 +159,15 @@ namespace YooAsset
// 创建默认的下载后台接口 // 创建默认的下载后台接口
if (DownloadBackend == null) if (DownloadBackend == null)
DownloadBackend = new UnityWebRequestBackend(DownloadSystemHelper.UnityWebRequestCreater); DownloadBackend = new UnityWebRequestBackend(WebRequestCreator);
} }
public virtual void OnDestroy() public virtual void OnDestroy()
{ {
if (DownloadBackend != null)
{
DownloadBackend.Dispose();
DownloadBackend = null;
}
} }
public virtual bool Belong(PackageBundle bundle) public virtual bool Belong(PackageBundle bundle)
@@ -237,4 +255,4 @@ namespace YooAsset
} }
#endregion #endregion
} }
} }

View File

@@ -13,6 +13,8 @@ namespace YooAsset
public const string DISABLE_CATALOG_FILE = "DISABLE_CATALOG_FILE"; public const string DISABLE_CATALOG_FILE = "DISABLE_CATALOG_FILE";
public const string DISABLE_UNITY_WEB_CACHE = "DISABLE_UNITY_WEB_CACHE"; public const string DISABLE_UNITY_WEB_CACHE = "DISABLE_UNITY_WEB_CACHE";
public const string DISABLE_ONDEMAND_DOWNLOAD = "DISABLE_ONDEMAND_DOWNLOAD"; public const string DISABLE_ONDEMAND_DOWNLOAD = "DISABLE_ONDEMAND_DOWNLOAD";
public const string UNITY_WEB_REQUEST_CREATOR = "UNITY_WEB_REQUEST_CREATOR";
public const string DOWNLOAD_BACKEND = "DOWNLOAD_BACKEND";
public const string DOWNLOAD_MAX_CONCURRENCY = "DOWNLOAD_MAX_CONCURRENCY"; public const string DOWNLOAD_MAX_CONCURRENCY = "DOWNLOAD_MAX_CONCURRENCY";
public const string DOWNLOAD_MAX_REQUEST_PER_FRAME = "DOWNLOAD_MAX_REQUEST_PER_FRAME"; public const string DOWNLOAD_MAX_REQUEST_PER_FRAME = "DOWNLOAD_MAX_REQUEST_PER_FRAME";
public const string DOWNLOAD_WATCH_DOG_TIME = "DOWNLOAD_WATCH_DOG_TIME"; public const string DOWNLOAD_WATCH_DOG_TIME = "DOWNLOAD_WATCH_DOG_TIME";
@@ -28,4 +30,4 @@ namespace YooAsset
public const string COPY_LOCAL_FILE_SERVICES = "COPY_LOCAL_FILE_SERVICES"; public const string COPY_LOCAL_FILE_SERVICES = "COPY_LOCAL_FILE_SERVICES";
public const string UNPACK_FILE_SYSTEM_ROOT = "UNPACK_FILE_SYSTEM_ROOT"; public const string UNPACK_FILE_SYSTEM_ROOT = "UNPACK_FILE_SYSTEM_ROOT";
} }
} }

View File

@@ -354,6 +354,17 @@ var webRemoteParams = FileSystemParameters.CreateDefaultWebRemoteFileSystemParam
```csharp ```csharp
var cacheParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices); var cacheParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
// 设置 UnityWebRequest 创建委托(用于证书/代理/自定义 Header 等)
cacheParams.AddParameter(FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR, (UnityWebRequestCreator)((url) =>
{
var request = new UnityEngine.Networking.UnityWebRequest(url, UnityEngine.Networking.UnityWebRequest.kHttpVerbGET);
// 自定义配置...
return request;
}));
// 设置自定义下载后端(若设置则不再创建默认 UnityWebRequestBackend
// cacheParams.AddParameter(FileSystemParametersDefine.DOWNLOAD_BACKEND, myDownloadBackend);
// 设置文件校验级别 // 设置文件校验级别
cacheParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, EFileVerifyLevel.High); cacheParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, EFileVerifyLevel.High);

View File

@@ -241,9 +241,10 @@ namespace YooAsset
/// <summary> /// <summary>
/// 设置下载系统参数,自定义下载请求 /// 设置下载系统参数,自定义下载请求
/// </summary> /// </summary>
public static void SetDownloadSystemUnityWebRequest(UnityWebRequestDelegate createDelegate) [Obsolete("This method is deprecated. Please use FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR instead.", true)]
public static void SetDownloadSystemUnityWebRequest(UnityWebRequestCreator createDelegate)
{ {
DownloadSystemHelper.UnityWebRequestCreater = createDelegate; throw new NotImplementedException();
} }
/// <summary> /// <summary>