Skip to content

Commit

Permalink
Merge pull request #5 from Haruma-K/feature/fix_scene_release
Browse files Browse the repository at this point in the history
Feature/fix scene release
  • Loading branch information
Haruma-K authored Nov 16, 2024
2 parents 278d4bf + 60c3478 commit 4fb29e9
Showing 1 changed file with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;

namespace Addler.Runtime.Core.LifetimeBinding
{
Expand All @@ -14,19 +15,19 @@ public static class AsyncOperationHandleExtensions
/// <param name="gameObject"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
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.");
}

if (!gameObject.TryGetComponent(out MonoBehaviourBasedReleaseEvent releaseEvent))
releaseEvent = gameObject.AddComponent<MonoBehaviourBasedReleaseEvent>();

return BindTo(self, releaseEvent);
return BindTo(self, releaseEvent, isScene);
}

/// <summary>
Expand All @@ -41,12 +42,12 @@ public static AsyncOperationHandle<T> BindTo<T>(this AsyncOperationHandle<T> 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;
}

Expand All @@ -57,18 +58,22 @@ public static AsyncOperationHandle<T> BindTo<T>(this AsyncOperationHandle<T> sel
/// <param name="releaseEvent"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
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;
}

Expand All @@ -88,13 +93,21 @@ public static AsyncOperationHandle<T> BindTo<T>(this AsyncOperationHandle<T> 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);
}
}
}
}

0 comments on commit 4fb29e9

Please sign in to comment.