From 7b41fd82a47ce4709f0eee511ca15c8f379bc637 Mon Sep 17 00:00:00 2001 From: hevinci Date: Wed, 8 Mar 2023 12:10:07 +0800 Subject: [PATCH] update asset system --- .../Runtime/AssetSystem/AssetSystem.cs | 2 +- .../Loader/AssetBundleFileLoader.cs | 2 +- .../Loader/AssetBundleWebLoader.cs | 11 +- .../AssetSystem/Loader/RawBundleFileLoader.cs | 12 +- .../AssetSystem/Loader/RawBundleWebLoader.cs | 151 ++++++++++++++++++ .../Loader/RawBundleWebLoader.cs.meta | 11 ++ 6 files changed, 177 insertions(+), 12 deletions(-) create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleWebLoader.cs create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleWebLoader.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs index ac6aefe..40aeda1 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs @@ -387,7 +387,7 @@ namespace YooAsset // 新增下载需求 #if UNITY_WEBGL if (bundleInfo.Bundle.IsRawFile) - loader = new RawBundleFileLoader(this, bundleInfo); + loader = new RawBundleWebLoader(this, bundleInfo); else loader = new AssetBundleWebLoader(this, bundleInfo); #else diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs index a8232eb..efdd1c0 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs @@ -300,7 +300,7 @@ namespace YooAsset if (_isShowWaitForAsyncError == false) { _isShowWaitForAsyncError = true; - YooLogger.Error($"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"); + YooLogger.Error($"{nameof(WaitForAsyncComplete)} failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"); } break; } diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs index 8e2a3d6..0b21ebd 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs @@ -7,6 +7,9 @@ using UnityEngine.Networking; namespace YooAsset { + /// + /// WebGL平台加载器 + /// internal sealed class AssetBundleWebLoader : BundleLoaderBase { private enum ESteps @@ -24,7 +27,6 @@ namespace YooAsset private ESteps _steps = ESteps.None; private float _tryTimer = 0; - private bool _isShowWaitForAsyncError = false; private DownloaderBase _downloader; private UnityWebRequest _webRequest; private AssetBundleCreateRequest _createRequest; @@ -225,10 +227,11 @@ namespace YooAsset /// public override void WaitForAsyncComplete() { - if (_isShowWaitForAsyncError == false) + if (IsDone() == false) { - _isShowWaitForAsyncError = true; - YooLogger.Error($"WebGL platform not support {nameof(WaitForAsyncComplete)} ! Use the async load method instead of the sync load method !"); + Status = EStatus.Failed; + LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !"; + YooLogger.Error(LastError); } } } diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleFileLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleFileLoader.cs index 1ebf7ff..d2394a2 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleFileLoader.cs @@ -16,7 +16,6 @@ namespace YooAsset } private ESteps _steps = ESteps.None; - private bool _isShowWaitForAsyncError = false; private DownloaderBase _unpacker; private DownloaderBase _downloader; @@ -42,7 +41,7 @@ namespace YooAsset } else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) { -#if UNITY_ANDROID || UNITY_WEBGL +#if UNITY_ANDROID _steps = ESteps.Unpack; FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath; #else @@ -156,14 +155,15 @@ namespace YooAsset } // 保险机制 - // 注意:如果需要从WEB端下载资源,可能会触发保险机制! + // 注意:如果需要从远端下载资源,可能会触发保险机制! frame--; if (frame == 0) { - if (_isShowWaitForAsyncError == false) + if (IsDone() == false) { - _isShowWaitForAsyncError = true; - YooLogger.Error($"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"); + Status = EStatus.Failed; + LastError = $"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"; + YooLogger.Error(LastError); } break; } diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleWebLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleWebLoader.cs new file mode 100644 index 0000000..6459873 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleWebLoader.cs @@ -0,0 +1,151 @@ +using System.IO; + +namespace YooAsset +{ + /// + /// WebGL平台加载器 + /// + internal class RawBundleWebLoader : BundleLoaderBase + { + private enum ESteps + { + None, + Download, + CheckDownload, + Website, + CheckWebsite, + CheckFile, + Done, + } + + private ESteps _steps = ESteps.None; + private DownloaderBase _website; + private DownloaderBase _downloader; + + + public RawBundleWebLoader(AssetSystemImpl impl, BundleInfo bundleInfo) : base(impl, bundleInfo) + { + } + + /// + /// 轮询更新 + /// + public override void Update() + { + if (_steps == ESteps.Done) + return; + + if (_steps == ESteps.None) + { + if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) + { + _steps = ESteps.Download; + FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath; + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) + { + _steps = ESteps.Website; + FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath; + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) + { + _steps = ESteps.CheckFile; + FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath; + } + else + { + throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); + } + } + + // 1. 下载远端文件 + if (_steps == ESteps.Download) + { + int failedTryAgain = int.MaxValue; + _downloader = DownloadSystem.BeginDownload(MainBundleInfo, failedTryAgain); + _steps = ESteps.CheckDownload; + } + + // 2. 检测下载结果 + if (_steps == ESteps.CheckDownload) + { + DownloadProgress = _downloader.DownloadProgress; + DownloadedBytes = _downloader.DownloadedBytes; + if (_downloader.IsDone() == false) + return; + + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = _downloader.GetLastError(); + } + else + { + _steps = ESteps.CheckFile; + } + } + + // 3. 从站点下载 + if (_steps == ESteps.Website) + { + int failedTryAgain = 1; + var bundleInfo = PatchManifestTools.GetUnpackInfo(MainBundleInfo.Bundle); + _website = DownloadSystem.BeginDownload(bundleInfo, failedTryAgain); + _steps = ESteps.CheckWebsite; + } + + // 4. 检测站点下载 + if (_steps == ESteps.CheckWebsite) + { + DownloadProgress = _website.DownloadProgress; + DownloadedBytes = _website.DownloadedBytes; + if (_website.IsDone() == false) + return; + + if (_website.HasError()) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = _website.GetLastError(); + } + else + { + _steps = ESteps.CheckFile; + } + } + + // 5. 检测结果 + if (_steps == ESteps.CheckFile) + { + // 设置下载进度 + DownloadProgress = 1f; + DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; + + _steps = ESteps.Done; + if (File.Exists(FileLoadPath)) + { + Status = EStatus.Succeed; + } + else + { + Status = EStatus.Failed; + LastError = $"Raw file not found : {FileLoadPath}"; + } + } + } + + /// + /// 主线程等待异步操作完毕 + /// + public override void WaitForAsyncComplete() + { + if (IsDone() == false) + { + Status = EStatus.Failed; + LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !"; + YooLogger.Error(LastError); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleWebLoader.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleWebLoader.cs.meta new file mode 100644 index 0000000..6420e98 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/RawBundleWebLoader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1d16b689f73611e44bd01a4cc429a6ac +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: