From 31850a452ac04bbfe6f9b67b75f4aa0efdd0d566 Mon Sep 17 00:00:00 2001 From: Haruki Yano Date: Sat, 16 Nov 2024 23:20:12 +0900 Subject: [PATCH 1/2] fix a bug when releasing scene --- .../AsyncOperationHandleExtensions.cs | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs b/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs index 7084e1d..a7a3831 100644 --- a/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs +++ b/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs @@ -2,6 +2,7 @@ using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; +using UnityEngine.ResourceManagement.ResourceProviders; namespace Addler.Runtime.Core.LifetimeBinding { @@ -14,11 +15,11 @@ public static class AsyncOperationHandleExtensions /// /// /// - public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, GameObject gameObject) + public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, GameObject gameObject, bool isScene) { if (gameObject == null) { - Addressables.Release(self); + ReleaseHandle(self, isScene); throw new ArgumentNullException(nameof(gameObject), $"{nameof(gameObject)} is null so the handle can't be bound and will be released immediately."); } @@ -26,7 +27,7 @@ public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, GameOb if (!gameObject.TryGetComponent(out MonoBehaviourBasedReleaseEvent releaseEvent)) releaseEvent = gameObject.AddComponent(); - return BindTo(self, releaseEvent); + return BindTo(self, releaseEvent, isScene); } /// @@ -41,12 +42,12 @@ public static AsyncOperationHandle BindTo(this AsyncOperationHandle sel { if (gameObject == null) { - Addressables.Release(self); + ReleaseHandle(self, typeof(T) == typeof(SceneInstance)); throw new ArgumentNullException(nameof(gameObject), $"{nameof(gameObject)} is null so the handle can't be bound and will be released immediately."); } - ((AsyncOperationHandle)self).BindTo(gameObject); + ((AsyncOperationHandle)self).BindTo(gameObject, typeof(T) == typeof(SceneInstance)); return self; } @@ -57,18 +58,18 @@ public static AsyncOperationHandle BindTo(this AsyncOperationHandle sel /// /// /// - public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, IReleaseEvent releaseEvent) + public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, IReleaseEvent releaseEvent, bool isScene) { if (releaseEvent == null) { - Addressables.Release(self); + ReleaseHandle(self, isScene); throw new ArgumentNullException(nameof(releaseEvent), $"{nameof(releaseEvent)} is null so the handle can't be bound and will be released immediately."); } void OnRelease() { - Addressables.Release(self); + ReleaseHandle(self, isScene); releaseEvent.Dispatched -= OnRelease; } @@ -88,13 +89,21 @@ public static AsyncOperationHandle BindTo(this AsyncOperationHandle sel { if (releaseEvent == null) { - Addressables.Release(self); + ReleaseHandle(self, typeof(T) == typeof(SceneInstance)); throw new ArgumentNullException(nameof(releaseEvent), $"{nameof(releaseEvent)} is null so the handle can't be bound and will be released immediately."); } - ((AsyncOperationHandle)self).BindTo(releaseEvent); + ((AsyncOperationHandle)self).BindTo(releaseEvent, typeof(T) == typeof(SceneInstance)); return self; } + + private static void ReleaseHandle(AsyncOperationHandle handle, bool isScene) + { + if (isScene) + Addressables.UnloadSceneAsync(handle); + else + Addressables.Release(handle); + } } -} +} \ No newline at end of file From 60c3478541b59cf35acc55df37e668a0ecf0fc2b Mon Sep 17 00:00:00 2001 From: Haruki Yano Date: Sat, 16 Nov 2024 23:20:23 +0900 Subject: [PATCH 2/2] code cleanup --- .../Core/LifetimeBinding/AsyncOperationHandleExtensions.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs b/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs index a7a3831..01ee0fc 100644 --- a/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs +++ b/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs @@ -58,7 +58,11 @@ public static AsyncOperationHandle BindTo(this AsyncOperationHandle sel /// /// /// - public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, IReleaseEvent releaseEvent, bool isScene) + public static AsyncOperationHandle BindTo( + this AsyncOperationHandle self, + IReleaseEvent releaseEvent, + bool isScene + ) { if (releaseEvent == null) {