diff --git a/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs b/Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs index 7084e1d..01ee0fc 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,22 @@ 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 +93,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