diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleFileLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleFileLoader.cs index 3597a2d..5d69f18 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleFileLoader.cs @@ -191,7 +191,7 @@ namespace YooAsset { if (_createRequest != null) { - if (_isWaitForAsyncComplete) + if (_isWaitForAsyncComplete || IsForceDestroyComplete) { // 强制挂起主线程(注意:该操作会很耗时) YooLogger.Warning("Suspend the main thread to load unity bundle."); diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs index 4c12424..0d651b0 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs @@ -46,6 +46,7 @@ namespace YooAsset private readonly List _providers = new List(100); private readonly List _removeList = new List(100); + protected bool IsForceDestroyComplete { private set; get; } = false; internal AssetBundle CacheBundle { set; get; } internal string FileLoadPath { set; get; } internal float DownloadProgress { set; get; } @@ -165,6 +166,18 @@ namespace YooAsset } } + /// + /// 强制销毁资源提供者 + /// + public void ForceDestroyComplete() + { + IsForceDestroyComplete = true; + + // 注意:主动轮询更新完成同步加载 + // 说明:如果正在下载或解压也可以放心销毁。 + Update(); + } + /// /// 主线程等待异步操作完毕 /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs index 5430619..b312ea8 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs @@ -66,9 +66,9 @@ namespace YooAsset } else { - Error = "Scene is invalid or is not loaded."; _steps = ESteps.Done; Status = EOperationStatus.Failed; + Error = "Scene is invalid or is not loaded."; } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs index b30ad31..db0b388 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs @@ -65,7 +65,7 @@ namespace YooAsset // 2. 加载资源对象 if (Status == EStatus.Loading) { - if (IsWaitForAsyncComplete) + if (IsWaitForAsyncComplete || IsForceDestroyComplete) { if (MainAssetInfo.AssetType == null) AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets(); @@ -87,7 +87,7 @@ namespace YooAsset { if (_cacheRequest != null) { - if (IsWaitForAsyncComplete) + if (IsWaitForAsyncComplete || IsForceDestroyComplete) { // 强制挂起主线程(注意:该操作会很耗时) YooLogger.Warning("Suspend the main thread to load unity asset."); diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs index dcff7d4..158ccda 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs @@ -65,7 +65,7 @@ namespace YooAsset // 2. 加载资源对象 if (Status == EStatus.Loading) { - if (IsWaitForAsyncComplete) + if (IsWaitForAsyncComplete || IsForceDestroyComplete) { if (MainAssetInfo.AssetType == null) AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath); @@ -87,7 +87,7 @@ namespace YooAsset { if (_cacheRequest != null) { - if (IsWaitForAsyncComplete) + if (IsWaitForAsyncComplete || IsForceDestroyComplete) { // 强制挂起主线程(注意:该操作会很耗时) YooLogger.Warning("Suspend the main thread to load unity asset."); diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs index 70ce28e..ca958e7 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs @@ -65,7 +65,7 @@ namespace YooAsset // 2. 加载资源对象 if (Status == EStatus.Loading) { - if (IsWaitForAsyncComplete) + if (IsWaitForAsyncComplete || IsForceDestroyComplete) { if (MainAssetInfo.AssetType == null) AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath); @@ -87,7 +87,7 @@ namespace YooAsset { if (_cacheRequest != null) { - if (IsWaitForAsyncComplete) + if (IsWaitForAsyncComplete || IsForceDestroyComplete) { // 强制挂起主线程(注意:该操作会很耗时) YooLogger.Warning("Suspend the main thread to load unity asset."); diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs index 5a6ca6a..1e2a9e9 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs @@ -93,6 +93,7 @@ namespace YooAsset protected BundleLoaderBase OwnerBundle { private set; get; } protected DependAssetBundles DependBundles { private set; get; } protected bool IsWaitForAsyncComplete { private set; get; } = false; + protected bool IsForceDestroyComplete { private set; get; } = false; private readonly List _handles = new List(); @@ -121,7 +122,7 @@ namespace YooAsset public abstract void Update(); /// - /// 销毁资源对象 + /// 销毁资源提供者 /// public void Destroy() { @@ -193,6 +194,17 @@ namespace YooAsset RefCount--; } + /// + /// 释放所有资源句柄 + /// + public void ReleaseAllHandles() + { + foreach (var handle in _handles) + { + handle.ReleaseInternal(); + } + } + /// /// 等待异步执行完毕 /// @@ -206,10 +218,22 @@ namespace YooAsset // 验证结果 if (IsDone == false) { - YooLogger.Warning($"WaitForAsyncComplete failed to loading : {MainAssetInfo.AssetPath}"); + YooLogger.Warning($"{nameof(WaitForAsyncComplete)} failed to loading : {MainAssetInfo.AssetPath}"); } } + /// + /// 强制销毁资源提供者 + /// + public void ForceDestroyComplete() + { + IsForceDestroyComplete = true; + + // 注意:主动轮询更新完成同步加载 + // 说明:如果资源包未准备完毕也可以放心销毁。 + Update(); + } + /// /// 处理特殊异常 /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs index 2bb6b5a..54e0eab 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs @@ -167,23 +167,45 @@ namespace YooAsset /// /// 强制回收所有资源 + /// 注意:加载器在销毁后关联的下载器还会继续下载! /// public void ForceUnloadAllAssets() { #if UNITY_WEBGL throw new Exception($"WebGL not support invoke {nameof(ForceUnloadAllAssets)}"); #else + // 注意:因为场景无法异步转同步,需要等待所有场景加载完毕! + foreach (var sceneHandlePair in _sceneHandles) + { + var sceneHandle = sceneHandlePair.Value; + if (sceneHandle.PackageName == PackageName) + { + if (sceneHandle.IsDone == false) + throw new Exception($"{nameof(ForceUnloadAllAssets)} cannot be called when loading the scene !"); + } + } + + // 释放所有资源句柄 foreach (var provider in _providerList) { - provider.WaitForAsyncComplete(); + provider.ReleaseAllHandles(); + } + + // 强制销毁资源提供者 + foreach (var provider in _providerList) + { + provider.ForceDestroyComplete(); provider.Destroy(); } + + // 强制销毁资源加载器 foreach (var loader in _loaderList) { - loader.WaitForAsyncComplete(); + loader.ForceDestroyComplete(); loader.Destroy(); } + // 清空数据 _providerList.Clear(); _providerDic.Clear(); _loaderList.Clear(); @@ -409,6 +431,10 @@ namespace YooAsset _providerDic.Remove(provider.ProviderGUID); } } + internal bool HasAnyLoader() + { + return _loaderList.Count > 0; + } private BundleLoaderBase CreateAssetBundleLoaderInternal(BundleInfo bundleInfo) { @@ -476,15 +502,6 @@ namespace YooAsset } return result; } - internal List GetLoadedBundleInfos() - { - List result = new List(100); - foreach (var loader in _loaderList) - { - result.Add(loader.MainBundleInfo); - } - return result; - } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs index 7a48f19..0af4ee2 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs @@ -300,7 +300,13 @@ namespace YooAsset public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion = true, int timeout = 60) { DebugCheckInitialize(false); - DebugCheckUpdateManifest(); + + // 注意:强烈建议在更新之前保持加载器为空! + if (_resourceMgr.HasAnyLoader()) + { + YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(ForceUnloadAllAssets)} method to release loaded bundle !"); + } + return _playModeImpl.UpdatePackageManifestAsync(packageVersion, autoSaveVersion, timeout); } @@ -1128,16 +1134,6 @@ namespace YooAsset } } - [Conditional("DEBUG")] - private void DebugCheckUpdateManifest() - { - var loadedBundleInfos = _resourceMgr.GetLoadedBundleInfos(); - if (loadedBundleInfos.Count > 0) - { - YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(ForceUnloadAllAssets)} method to release loaded bundle !"); - } - } - [Conditional("DEBUG")] private void DebugCheckRawFileLoadMethod(string method) {