Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix scene release #5

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Loading