From 9420f8561fdd5772c7badb90225326b3fff7fcd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Thu, 4 Jul 2024 21:59:24 +0800 Subject: [PATCH] update web file system --- .../Internal/UnityWebRequestOperation.cs | 2 +- .../DefaultWebFileSystem.cs | 26 ++++++++++---- .../Operation/DWFSDownloadFileOperation.cs | 34 +++++++++++++------ .../YooAsset/Runtime/InitializeParameters.cs | 8 +++-- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Operation/Internal/UnityWebRequestOperation.cs b/Assets/YooAsset/Runtime/DownloadSystem/Operation/Internal/UnityWebRequestOperation.cs index 26bd625..c7b7c9b 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Operation/Internal/UnityWebRequestOperation.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Operation/Internal/UnityWebRequestOperation.cs @@ -57,7 +57,7 @@ namespace YooAsset // 注意:在连续时间段内无新增下载数据及判定为超时 if (_isAbort == false) { - if (_latestDownloadBytes != _webRequest.downloadedBytes) + if ( _latestDownloadBytes != _webRequest.downloadedBytes) { _latestDownloadBytes = _webRequest.downloadedBytes; _latestDownloadRealtime = Time.realtimeSinceStartup; diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs index 1b7e9d0..b7f791c 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs @@ -61,12 +61,20 @@ namespace YooAsset } #region 自定义参数 - public bool AllowCrossAccess { private set; get; } = false; - /// /// 自定义参数:远程服务接口 /// public IRemoteServices RemoteServices { private set; get; } = null; + + /// + /// 禁用Unity的网络缓存 + /// + public bool DisableUnityWebCache { private set; get; } = false; + + /// + /// 允许跨域访问和下载 + /// + public bool AllowCrossAccess { private set; get; } = false; #endregion @@ -124,14 +132,18 @@ namespace YooAsset public virtual void SetParameter(string name, object value) { - if (name == "ALLOW_CROSS_ACCESS") - { - AllowCrossAccess = (bool)value; - } - else if (name == "REMOTE_SERVICES") + if (name == "REMOTE_SERVICES") { RemoteServices = (IRemoteServices)value; } + else if (name == "DISABLE_UNITY_WEB_CACHE") + { + DisableUnityWebCache = (bool)value; + } + else if (name == "ALLOW_CROSS_ACCESS") + { + AllowCrossAccess = (bool)value; + } else { YooLogger.Warning($"Invalid parameter : {name}"); diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/Operation/DWFSDownloadFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/Operation/DWFSDownloadFileOperation.cs index 46ad353..9318fc0 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/Operation/DWFSDownloadFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/Operation/DWFSDownloadFileOperation.cs @@ -110,20 +110,34 @@ namespace YooAsset private void CreateWebRequest() { - uint unityCRC = Bundle.UnityCRC; - Hash128 fileHash = Hash128.Parse(Bundle.FileHash); - - // 注意:优先从浏览器缓存里获取文件 - // The file hash defining the version of the asset bundle. _webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL); - _downloadhandler = new DownloadHandlerAssetBundle(_requestURL, fileHash, unityCRC); -#if UNITY_2020_3_OR_NEWER - _downloadhandler.autoLoadAssetBundle = false; -#endif - _webRequest.downloadHandler = _downloadhandler; + _webRequest.downloadHandler = CreateDownloadHandler(); _webRequest.disposeDownloadHandlerOnDispose = true; _webRequest.SendWebRequest(); } + private DownloadHandlerAssetBundle CreateDownloadHandler() + { + if (_fileSystem.DisableUnityWebCache) + { + var downloadhandler = new DownloadHandlerAssetBundle(_requestURL, 0); +#if UNITY_2020_3_OR_NEWER + downloadhandler.autoLoadAssetBundle = false; +#endif + return downloadhandler; + } + else + { + // 注意:优先从浏览器缓存里获取文件 + // The file hash defining the version of the asset bundle. + uint unityCRC = Bundle.UnityCRC; + Hash128 fileHash = Hash128.Parse(Bundle.FileHash); + var downloadhandler = new DownloadHandlerAssetBundle(_requestURL, fileHash, unityCRC); +#if UNITY_2020_3_OR_NEWER + downloadhandler.autoLoadAssetBundle = false; +#endif + return downloadhandler; + } + } private void DisposeWebRequest() { if (_webRequest != null) diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index cd4684b..17a6b16 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -160,10 +160,11 @@ namespace YooAsset /// /// 创建默认的Web文件系统参数 /// - public static FileSystemParameters CreateDefaultWebFileSystemParameters() + public static FileSystemParameters CreateDefaultWebFileSystemParameters(bool disableUnityWebCache = false) { string fileSystemClass = typeof(DefaultWebFileSystem).FullName; var fileSystemParams = new FileSystemParameters(fileSystemClass, null); + fileSystemParams.AddParameter("DISABLE_UNITY_WEB_CACHE", disableUnityWebCache); fileSystemParams.AddParameter("ALLOW_CROSS_ACCESS", false); return fileSystemParams; } @@ -172,12 +173,13 @@ namespace YooAsset /// 创建默认的Web文件系统参数 /// /// 远端资源地址查询服务类 - public static FileSystemParameters CreateDefaultWebFileSystemParameters(IRemoteServices remoteServices) + public static FileSystemParameters CreateDefaultWebFileSystemParameters(IRemoteServices remoteServices, bool disableUnityWebCache = false) { string fileSystemClass = typeof(DefaultWebFileSystem).FullName; var fileSystemParams = new FileSystemParameters(fileSystemClass, null); - fileSystemParams.AddParameter("ALLOW_CROSS_ACCESS", true); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); + fileSystemParams.AddParameter("DISABLE_UNITY_WEB_CACHE", disableUnityWebCache); + fileSystemParams.AddParameter("ALLOW_CROSS_ACCESS", true); return fileSystemParams; } }