diff --git a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs index 510522a..cabcdf3 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs @@ -8,13 +8,11 @@ namespace YooAsset { internal class AssetSystemImpl { - private static string SceneRunningPackage = string.Empty; - private readonly List _loaders = new List(1000); private readonly List _providers = new List(1000); - private readonly Dictionary _sceneHandles = new Dictionary(100); + private readonly static Dictionary _sceneHandles = new Dictionary(100); + private static long _sceneCreateCount = 0; - private long _sceneCreateCount = 0; private bool _simulationOnEditor; private int _loadingMaxNumber; public IDecryptionServices DecryptionServices { private set; get; } @@ -71,9 +69,9 @@ namespace YooAsset /// public void DestroyAll() { - _loaders.Clear(); _providers.Clear(); - _sceneHandles.Clear(); + _loaders.Clear(); + ClearSceneHandle(); DecryptionServices = null; BundleServices = null; @@ -123,15 +121,14 @@ namespace YooAsset { provider.Destroy(); } - _providers.Clear(); - foreach (var loader in _loaders) { loader.Destroy(true); } - _loaders.Clear(); - _sceneHandles.Clear(); + _providers.Clear(); + _loaders.Clear(); + ClearSceneHandle(); // 注意:调用底层接口释放所有资源 Resources.UnloadUnusedAssets(); @@ -150,19 +147,6 @@ namespace YooAsset return completedProvider.CreateHandle(); } - // 注意:场景只允许运行在一个资源包内 - if (string.IsNullOrEmpty(SceneRunningPackage)) - { - SceneRunningPackage = BundleServices.GetPackageName(); - } - if (BundleServices.GetPackageName() != SceneRunningPackage) - { - CompletedProvider completedProvider = new CompletedProvider(assetInfo); - string error = $"Scene are allowed to running within only {SceneRunningPackage}"; - completedProvider.SetCompleted(error); - return completedProvider.CreateHandle(); - } - // 如果加载的是主场景,则卸载所有缓存的场景 if (sceneMode == LoadSceneMode.Single) { @@ -182,6 +166,7 @@ namespace YooAsset } var handle = provider.CreateHandle(); + handle.PackageName = BundleServices.GetPackageName(); _sceneHandles.Add(providerGUID, handle); return handle; } @@ -265,6 +250,24 @@ namespace YooAsset // 卸载未被使用的资源(包括场景) UnloadUnusedAssets(); } + internal void ClearSceneHandle() + { + // 释放资源包下的所有场景 + string packageName = BundleServices.GetPackageName(); + List removeList = new List(); + foreach (var valuePair in _sceneHandles) + { + if (valuePair.Value.PackageName == packageName) + { + removeList.Add(valuePair.Key); + } + } + + foreach (var key in removeList) + { + _sceneHandles.Remove(key); + } + } internal AssetBundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo) { diff --git a/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs index d269c4d..983565c 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs @@ -3,8 +3,9 @@ namespace YooAsset { public class SceneOperationHandle : OperationHandleBase - { + { private System.Action _callback; + internal string PackageName { set; get; } internal SceneOperationHandle(ProviderBase provider) : base(provider) {