From eb16ba836578440277a82908f152c9f9c4d4d2d6 Mon Sep 17 00:00:00 2001 From: huanggongming Date: Fri, 24 Feb 2023 12:18:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(DownloadSystem):=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E4=B8=8B=E8=BD=BD=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DownloadSystem/DownloadRequestDelegate.cs | 28 +++++++++++++++++++ .../DownloadRequestDelegate.cs.meta | 3 ++ .../Runtime/DownloadSystem/DownloadSystem.cs | 9 ++++++ .../Downloader/FileDownloader.cs | 6 ++-- .../DownloadSystem/UnityWebDataRequester.cs | 2 +- .../DownloadSystem/UnityWebFileRequester.cs | 2 +- Assets/YooAsset/Runtime/YooAssets.cs | 9 ++++++ 7 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 Assets/YooAsset/Runtime/DownloadSystem/DownloadRequestDelegate.cs create mode 100644 Assets/YooAsset/Runtime/DownloadSystem/DownloadRequestDelegate.cs.meta diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadRequestDelegate.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadRequestDelegate.cs new file mode 100644 index 0000000..80ee87f --- /dev/null +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadRequestDelegate.cs @@ -0,0 +1,28 @@ +using JetBrains.Annotations; +using UnityEngine.Networking; + +namespace YooAsset +{ + public delegate UnityWebRequest DownloadRequestDelegate(string url); + + /// + /// 自定义下载器的请求 + /// + 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); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadRequestDelegate.cs.meta b/Assets/YooAsset/Runtime/DownloadSystem/DownloadRequestDelegate.cs.meta new file mode 100644 index 0000000..9b07fd2 --- /dev/null +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadRequestDelegate.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3b21da9f048045e2af56d9d396b11c2f +timeCreated: 1677207036 \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs index 83321d7..88514d1 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs @@ -32,6 +32,15 @@ namespace YooAsset /// public static List ClearFileResponseCodes { set; get; } + /// + /// 自定义下载请求 + /// + /// + public static void SetRequestDelegate(DownloadRequestDelegate requestDelegate) + { + DownloadRequestUtil.SetRequestDelegate(requestDelegate); + } + /// /// 更新所有下载器 /// diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index 5d3a942..ad3d686 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -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 diff --git a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs index cdd3918..31ad283 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs @@ -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; diff --git a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs index c85ed1a..ba2d73b 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs @@ -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; diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 4fce4f6..6e62354 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -168,6 +168,15 @@ namespace YooAsset DownloadSystem.BreakpointResumeFileSize = fileBytes; } + /// + /// 自定义下载请求 + /// + /// + public static void SetDownloadSystemUnityWebRequest(DownloadRequestDelegate requestDelegate) + { + DownloadSystem.SetRequestDelegate(requestDelegate); + } + /// /// 设置下载系统参数,下载失败后清理文件的HTTP错误码 ///