diff --git a/Assets/YooAsset/Runtime/PatchSystem/Download/FileDownloader.cs b/Assets/YooAsset/Runtime/PatchSystem/Download/FileDownloader.cs index 40a8e13..3c29c5b 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Download/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Download/FileDownloader.cs @@ -238,5 +238,13 @@ namespace YooAsset { YooLogger.Error($"Failed to download : {_requestURL} Error : {_lastError}"); } + + /// + /// 获取最近一条错误日志 + /// + public string GetLastError() + { + return _lastError; + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebRequester.cs b/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebDataRequester.cs similarity index 98% rename from Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebRequester.cs rename to Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebDataRequester.cs index 0e11dcc..9424ef9 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebRequester.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebDataRequester.cs @@ -9,7 +9,7 @@ namespace YooAsset /// 下载器 /// 说明:UnityWebRequest(UWR) supports reading streaming assets since 2017.1 /// - internal class UnityWebRequester + internal class UnityWebDataRequester { protected UnityWebRequest _webRequest; protected UnityWebRequestAsyncOperation _operationHandle; diff --git a/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebRequester.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebDataRequester.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebRequester.cs.meta rename to Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebDataRequester.cs.meta diff --git a/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebFileRequester.cs b/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebFileRequester.cs new file mode 100644 index 0000000..9ce83e2 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebFileRequester.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine.Networking; + +namespace YooAsset +{ + /// + /// 下载器 + /// 说明:UnityWebRequest(UWR) supports reading streaming assets since 2017.1 + /// + internal class UnityWebFileRequester + { + protected UnityWebRequest _webRequest; + protected UnityWebRequestAsyncOperation _operationHandle; + + /// + /// 请求URL地址 + /// + public string URL { private set; get; } + + + /// + /// 发送GET请求 + /// + public void SendRequest(string url, string savePath) + { + if (_webRequest == null) + { + URL = url; + _webRequest = new UnityWebRequest(URL, UnityWebRequest.kHttpVerbGET); + DownloadHandlerFile handler = new DownloadHandlerFile(savePath); + handler.removeFileOnAbort = true; + _webRequest.downloadHandler = handler; + _webRequest.disposeDownloadHandlerOnDispose = true; + _operationHandle = _webRequest.SendWebRequest(); + } + } + + /// + /// 释放下载器 + /// + public void Dispose() + { + if (_webRequest != null) + { + _webRequest.Dispose(); + _webRequest = null; + _operationHandle = null; + } + } + + /// + /// 是否完毕(无论成功失败) + /// + public bool IsDone() + { + if (_operationHandle == null) + return false; + return _operationHandle.isDone; + } + + /// + /// 下载是否发生错误 + /// + public bool HasError() + { +#if UNITY_2020_3_OR_NEWER + return _webRequest.result != UnityWebRequest.Result.Success; +#else + if (_webRequest.isNetworkError || _webRequest.isHttpError) + return true; + else + return false; +#endif + } + + /// + /// 获取错误信息 + /// + public string GetError() + { + if (_webRequest != null) + { + return $"URL : {URL} Error : {_webRequest.error}"; + } + return string.Empty; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebFileRequester.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebFileRequester.cs.meta new file mode 100644 index 0000000..7e74c68 --- /dev/null +++ b/Assets/YooAsset/Runtime/PatchSystem/Download/UnityWebFileRequester.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2ab5f2486d06e2642ba7aa9c9623430e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs index e960567..a796b40 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs @@ -41,7 +41,7 @@ namespace YooAsset private OfflinePlayModeImpl _impl; private ESteps _steps = ESteps.None; - private UnityWebRequester _downloader; + private UnityWebDataRequester _downloader; private string _downloadURL; internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl) @@ -61,7 +61,7 @@ namespace YooAsset { string filePath = PathHelper.MakeStreamingLoadPath(ResourceSettingData.Setting.PatchManifestFileName); _downloadURL = PathHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebRequester(); + _downloader = new UnityWebDataRequester(); _downloader.SendRequest(_downloadURL); _steps = ESteps.CheckAppManifest; } @@ -106,7 +106,7 @@ namespace YooAsset private HostPlayModeImpl _impl; private ESteps _steps = ESteps.None; - private UnityWebRequester _downloader; + private UnityWebDataRequester _downloader; private string _downloadURL; internal HostPlayModeInitializationOperation(HostPlayModeImpl impl) @@ -151,7 +151,7 @@ namespace YooAsset YooLogger.Log($"Load application patch manifest."); string filePath = PathHelper.MakeStreamingLoadPath(ResourceSettingData.Setting.PatchManifestFileName); _downloadURL = PathHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebRequester(); + _downloader = new UnityWebDataRequester(); _downloader.SendRequest(_downloadURL); _steps = ESteps.CheckAppManifest; } diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs index c18e212..f3e846d 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/UpdateManifestOperation.cs @@ -64,8 +64,8 @@ namespace YooAsset private readonly int _updateResourceVersion; private readonly int _timeout; private ESteps _steps = ESteps.None; - private UnityWebRequester _downloaderHash; - private UnityWebRequester _downloaderManifest; + private UnityWebDataRequester _downloaderHash; + private UnityWebDataRequester _downloaderManifest; private float _verifyTime; public HostPlayModeUpdateManifestOperation(HostPlayModeImpl impl, int updateResourceVersion, int timeout) @@ -97,7 +97,7 @@ namespace YooAsset { string webURL = GetPatchManifestRequestURL(_updateResourceVersion, ResourceSettingData.Setting.PatchManifestHashFileName); YooLogger.Log($"Beginning to request patch manifest hash : {webURL}"); - _downloaderHash = new UnityWebRequester(); + _downloaderHash = new UnityWebDataRequester(); _downloaderHash.SendRequest(webURL, _timeout); _steps = ESteps.CheckWebManifestHash; } @@ -139,7 +139,7 @@ namespace YooAsset { string webURL = GetPatchManifestRequestURL(_updateResourceVersion, ResourceSettingData.Setting.PatchManifestFileName); YooLogger.Log($"Beginning to request patch manifest : {webURL}"); - _downloaderManifest = new UnityWebRequester(); + _downloaderManifest = new UnityWebDataRequester(); _downloaderManifest.SendRequest(webURL, _timeout); _steps = ESteps.CheckWebManifest; }