From fb28bcc535013913377cef4bbc719e239c0adb43 Mon Sep 17 00:00:00 2001 From: hevinci Date: Thu, 29 Jun 2023 15:47:10 +0800 Subject: [PATCH] update download system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增方法YooAssets.SetDownloadSystemRedirectLimit() --- .../Loader/AssetBundleWebLoader.cs | 8 ++--- .../Runtime/DownloadSystem/DownloadSystem.cs | 29 +++++++++++++++++-- .../Downloader/FileDownloader.cs | 14 --------- Assets/YooAsset/Runtime/YooAssets.cs | 15 ++++++++++ 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs index d775d18..3b81a8f 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs @@ -171,16 +171,16 @@ namespace YooAsset if (CacheSystem.DisableUnityCacheOnWebGL) { _webRequest = UnityWebRequestAssetBundle.GetAssetBundle(FileLoadPath); - _webRequest.SendWebRequest(); - _steps = ESteps.CheckLoadWebFile; } else { var hash = Hash128.Parse(MainBundleInfo.Bundle.FileHash); _webRequest = UnityWebRequestAssetBundle.GetAssetBundle(FileLoadPath, hash); - _webRequest.SendWebRequest(); - _steps = ESteps.CheckLoadWebFile; } + + DownloadSystem.SetUnityWebRequest(_webRequest); + _webRequest.SendWebRequest(); + _steps = ESteps.CheckLoadWebFile; } // 6. 检测AssetBundle加载结果 diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs index de18ee6..b7a7723 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs @@ -32,6 +32,11 @@ namespace YooAsset /// public static CertificateHandler CertificateHandlerInstance = null; + /// + /// 网络重定向次数 + /// + public static int RedirectLimit { set; get; } = -1; + /// /// 启用断点续传功能文件的最小字节数 /// @@ -128,11 +133,29 @@ namespace YooAsset /// public static UnityWebRequest NewRequest(string requestURL) { + UnityWebRequest webRequest; if (RequestDelegate != null) - return RequestDelegate.Invoke(requestURL); + webRequest = RequestDelegate.Invoke(requestURL); + else + webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET); - var request = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET); - return request; + SetUnityWebRequest(webRequest); + return webRequest; + } + + /// + /// 设置网络请求的自定义参数 + /// + public static void SetUnityWebRequest(UnityWebRequest webRequest) + { + if (RedirectLimit >= 0) + webRequest.redirectLimit = RedirectLimit; + + if (CertificateHandlerInstance != null) + { + webRequest.certificateHandler = CertificateHandlerInstance; + webRequest.disposeCertificateHandlerOnDispose = false; + } } /// diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index 6071af7..943c7b1 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -101,13 +101,6 @@ namespace YooAsset handler.removeFileOnAbort = true; _webRequest.downloadHandler = handler; _webRequest.disposeDownloadHandlerOnDispose = true; - - if (DownloadSystem.CertificateHandlerInstance != null) - { - _webRequest.certificateHandler = DownloadSystem.CertificateHandlerInstance; - _webRequest.disposeCertificateHandlerOnDispose = false; - } - _webRequest.SendWebRequest(); _steps = ESteps.CheckDownload; } @@ -137,13 +130,6 @@ namespace YooAsset _webRequest.disposeDownloadHandlerOnDispose = true; if (fileLength > 0) _webRequest.SetRequestHeader("Range", $"bytes={fileLength}-"); - - if (DownloadSystem.CertificateHandlerInstance != null) - { - _webRequest.certificateHandler = DownloadSystem.CertificateHandlerInstance; - _webRequest.disposeCertificateHandlerOnDispose = false; - } - _webRequest.SendWebRequest(); _steps = ESteps.CheckDownload; } diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index fa83938..3984d24 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -217,6 +217,21 @@ namespace YooAsset DownloadSystem.RequestDelegate = requestDelegate; } + /// + /// 设置下载系统参数,网络重定向次数(Unity引擎默认值32) + /// 注意:不支持设置为负值 + /// + public static void SetDownloadSystemRedirectLimit(int redirectLimit) + { + if (redirectLimit < 0) + { + YooLogger.Warning($"Invalid param value : {redirectLimit}"); + return; + } + + DownloadSystem.RedirectLimit = redirectLimit; + } + /// /// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒) ///