From d64b31f6ceb8592237743c957cc6139be9959c02 Mon Sep 17 00:00:00 2001 From: hevinci Date: Wed, 20 Jul 2022 15:05:28 +0800 Subject: [PATCH] Fixed #26 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复多个场景打进一个AB包时,卸载子场景时抛出异常。 --- .../Runtime/AssetSystem/AssetSystem.cs | 51 +++++++------------ .../Loader/AssetBundleLoaderBase.cs | 4 +- .../Provider/BundledAssetProvider.cs | 2 +- .../AssetSystem/Provider/BundledProvider.cs | 2 +- .../Provider/BundledSceneProvider.cs | 2 +- .../Provider/BundledSubAssetsProvider.cs | 2 +- .../AssetSystem/Provider/CompletedProvider.cs | 2 +- .../Provider/DatabaseAssetProvider.cs | 2 +- .../Provider/DatabaseSceneProvider.cs | 2 +- .../Provider/DatabaseSubAssetsProvider.cs | 2 +- .../AssetSystem/Provider/ProviderBase.cs | 8 ++- .../YooAsset/Runtime/PatchSystem/AssetInfo.cs | 4 +- 12 files changed, 36 insertions(+), 47 deletions(-) diff --git a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs index c1c1256..9b41846 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs @@ -11,7 +11,8 @@ namespace YooAsset private static readonly List _loaders = new List(1000); private static readonly List _providers = new List(1000); private static readonly Dictionary _sceneHandles = new Dictionary(100); - + private static long _sceneCreateCount = 0; + private static bool _simulationOnEditor; private static int _loadingMaxNumber; public static IDecryptionServices DecryptionServices { private set; get; } @@ -144,24 +145,20 @@ namespace YooAsset return completedProvider.CreateHandle(); } - // 注意:场景句柄永远保持唯一 - string providerGUID = assetInfo.ProviderGUID; - if (_sceneHandles.ContainsKey(providerGUID)) - return _sceneHandles[providerGUID]; - // 如果加载的是主场景,则卸载所有缓存的场景 if (sceneMode == LoadSceneMode.Single) { UnloadAllScene(); } - ProviderBase provider = TryGetProvider(providerGUID); - if (provider == null) + // 注意:同一个场景的ProviderGUID每次加载都会变化 + string providerGUID = $"{assetInfo.GUID}-{++_sceneCreateCount}"; + ProviderBase provider; { if (_simulationOnEditor) - provider = new DatabaseSceneProvider(assetInfo, sceneMode, activateOnLoad, priority); + provider = new DatabaseSceneProvider(providerGUID, assetInfo, sceneMode, activateOnLoad, priority); else - provider = new BundledSceneProvider(assetInfo, sceneMode, activateOnLoad, priority); + provider = new BundledSceneProvider(providerGUID, assetInfo, sceneMode, activateOnLoad, priority); provider.InitSpawnDebugInfo(); _providers.Add(provider); } @@ -183,13 +180,14 @@ namespace YooAsset return completedProvider.CreateHandle(); } - ProviderBase provider = TryGetProvider(assetInfo.ProviderGUID); + string providerGUID = assetInfo.GUID; + ProviderBase provider = TryGetProvider(providerGUID); if (provider == null) { if (_simulationOnEditor) - provider = new DatabaseAssetProvider(assetInfo); + provider = new DatabaseAssetProvider(providerGUID, assetInfo); else - provider = new BundledAssetProvider(assetInfo); + provider = new BundledAssetProvider(providerGUID, assetInfo); provider.InitSpawnDebugInfo(); _providers.Add(provider); } @@ -208,13 +206,14 @@ namespace YooAsset return completedProvider.CreateHandle(); } - ProviderBase provider = TryGetProvider(assetInfo.ProviderGUID); + string providerGUID = assetInfo.GUID; + ProviderBase provider = TryGetProvider(providerGUID); if (provider == null) { if (_simulationOnEditor) - provider = new DatabaseSubAssetsProvider(assetInfo); + provider = new DatabaseSubAssetsProvider(providerGUID, assetInfo); else - provider = new BundledSubAssetsProvider(assetInfo); + provider = new BundledSubAssetsProvider(providerGUID, assetInfo); provider.InitSpawnDebugInfo(); _providers.Add(provider); } @@ -223,7 +222,7 @@ namespace YooAsset internal static void UnloadSubScene(ProviderBase provider) { - string providerGUID = provider.MainAssetInfo.ProviderGUID; + string providerGUID = provider.ProviderGUID; if (_sceneHandles.ContainsKey(providerGUID) == false) throw new Exception("Should never get here !"); @@ -233,12 +232,6 @@ namespace YooAsset // 卸载未被使用的资源(包括场景) AssetSystem.UnloadUnusedAssets(); - - // 检验子场景是否销毁 - if (provider.IsDestroyed == false) - { - throw new Exception("Should never get here !"); - } } internal static void UnloadAllScene() { @@ -251,16 +244,6 @@ namespace YooAsset // 卸载未被使用的资源(包括场景) AssetSystem.UnloadUnusedAssets(); - - // 检验所有场景是否销毁 - foreach (var provider in _providers) - { - if (provider.IsSceneProvider()) - { - if (provider.IsDestroyed == false) - throw new Exception("Should never get here !"); - } - } } internal static AssetBundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo) @@ -324,7 +307,7 @@ namespace YooAsset for (int i = 0; i < _providers.Count; i++) { ProviderBase temp = _providers[i]; - if (temp.MainAssetInfo.ProviderGUID.Equals(providerGUID)) + if (temp.ProviderGUID.Equals(providerGUID)) { provider = temp; break; diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoaderBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoaderBase.cs index c1b9e07..7fe1538 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoaderBase.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoaderBase.cs @@ -130,14 +130,14 @@ namespace YooAsset if (IsDone() == false) return; - // 注意:必须等待所有Provider可以销毁的时候,才可以释放Bundle文件。 + // 条件1:必须等待所有Provider可以销毁 foreach (var provider in _providers) { if (provider.CanDestroy() == false) return; } - // 除了自己没有其它引用 + // 条件2:除了自己没有其它引用 if (RefCount > _providers.Count) return; diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs index 93bb5ff..3923ba8 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs @@ -17,7 +17,7 @@ namespace YooAsset } } - public BundledAssetProvider(AssetInfo assetInfo) : base(assetInfo) + public BundledAssetProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledProvider.cs index ac4599a..b2daac8 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledProvider.cs @@ -8,7 +8,7 @@ namespace YooAsset protected AssetBundleLoaderBase OwnerBundle { private set; get; } protected DependAssetBundleGroup DependBundleGroup { private set; get; } - public BundledProvider(AssetInfo assetInfo) : base(assetInfo) + public BundledProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) { OwnerBundle = AssetSystem.CreateOwnerAssetBundleLoader(assetInfo); OwnerBundle.Reference(); diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs index 5ac6320..ef8c323 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs @@ -23,7 +23,7 @@ namespace YooAsset } } - public BundledSceneProvider(AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(assetInfo) + public BundledSceneProvider(string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(providerGUID, assetInfo) { SceneMode = sceneMode; _sceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs index 2349e35..8a10a34 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs @@ -17,7 +17,7 @@ namespace YooAsset } } - public BundledSubAssetsProvider(AssetInfo assetInfo) : base(assetInfo) + public BundledSubAssetsProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/CompletedProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/CompletedProvider.cs index 3b487a2..9ce9095 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/CompletedProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/CompletedProvider.cs @@ -14,7 +14,7 @@ namespace YooAsset } } - public CompletedProvider(AssetInfo assetInfo) : base(assetInfo) + public CompletedProvider(AssetInfo assetInfo) : base(string.Empty, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs index 52ca771..b99694e 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs @@ -17,7 +17,7 @@ namespace YooAsset } } - public DatabaseAssetProvider(AssetInfo assetInfo) : base(assetInfo) + public DatabaseAssetProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs index 5a96241..d2f1878 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs @@ -19,7 +19,7 @@ namespace YooAsset } } - public DatabaseSceneProvider(AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(assetInfo) + public DatabaseSceneProvider(string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(providerGUID, assetInfo) { SceneMode = sceneMode; _activateOnLoad = activateOnLoad; diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs index db7579b..d87f1f6 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs @@ -17,7 +17,7 @@ namespace YooAsset } } - public DatabaseSubAssetsProvider(AssetInfo assetInfo) : base(assetInfo) + public DatabaseSubAssetsProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs index ae51528..be09c41 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs @@ -17,6 +17,11 @@ namespace YooAsset Fail, } + /// + /// 资源提供者唯一标识符 + /// + public string ProviderGUID { private set; get; } + /// /// 资源信息 /// @@ -85,8 +90,9 @@ namespace YooAsset private readonly List _handles = new List(); - public ProviderBase(AssetInfo assetInfo) + public ProviderBase(string providerGUID, AssetInfo assetInfo) { + ProviderGUID = providerGUID; MainAssetInfo = assetInfo; } diff --git a/Assets/YooAsset/Runtime/PatchSystem/AssetInfo.cs b/Assets/YooAsset/Runtime/PatchSystem/AssetInfo.cs index 9642b16..6b534b7 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/AssetInfo.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/AssetInfo.cs @@ -7,9 +7,9 @@ namespace YooAsset private string _providerGUID; /// - /// 资源提供者唯一标识符 + /// 唯一标识符 /// - internal string ProviderGUID + internal string GUID { get {