Skip to content

Commit 4fb29e9

Browse files
authored
Merge pull request #5 from Haruma-K/feature/fix_scene_release
Feature/fix scene release
2 parents 278d4bf + 60c3478 commit 4fb29e9

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

Assets/Addler/Runtime/Core/LifetimeBinding/AsyncOperationHandleExtensions.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using UnityEngine;
33
using UnityEngine.AddressableAssets;
44
using UnityEngine.ResourceManagement.AsyncOperations;
5+
using UnityEngine.ResourceManagement.ResourceProviders;
56

67
namespace Addler.Runtime.Core.LifetimeBinding
78
{
@@ -14,19 +15,19 @@ public static class AsyncOperationHandleExtensions
1415
/// <param name="gameObject"></param>
1516
/// <returns></returns>
1617
/// <exception cref="ArgumentNullException"></exception>
17-
public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, GameObject gameObject)
18+
public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, GameObject gameObject, bool isScene)
1819
{
1920
if (gameObject == null)
2021
{
21-
Addressables.Release(self);
22+
ReleaseHandle(self, isScene);
2223
throw new ArgumentNullException(nameof(gameObject),
2324
$"{nameof(gameObject)} is null so the handle can't be bound and will be released immediately.");
2425
}
2526

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

29-
return BindTo(self, releaseEvent);
30+
return BindTo(self, releaseEvent, isScene);
3031
}
3132

3233
/// <summary>
@@ -41,12 +42,12 @@ public static AsyncOperationHandle<T> BindTo<T>(this AsyncOperationHandle<T> sel
4142
{
4243
if (gameObject == null)
4344
{
44-
Addressables.Release(self);
45+
ReleaseHandle(self, typeof(T) == typeof(SceneInstance));
4546
throw new ArgumentNullException(nameof(gameObject),
4647
$"{nameof(gameObject)} is null so the handle can't be bound and will be released immediately.");
4748
}
4849

49-
((AsyncOperationHandle)self).BindTo(gameObject);
50+
((AsyncOperationHandle)self).BindTo(gameObject, typeof(T) == typeof(SceneInstance));
5051
return self;
5152
}
5253

@@ -57,18 +58,22 @@ public static AsyncOperationHandle<T> BindTo<T>(this AsyncOperationHandle<T> sel
5758
/// <param name="releaseEvent"></param>
5859
/// <returns></returns>
5960
/// <exception cref="ArgumentNullException"></exception>
60-
public static AsyncOperationHandle BindTo(this AsyncOperationHandle self, IReleaseEvent releaseEvent)
61+
public static AsyncOperationHandle BindTo(
62+
this AsyncOperationHandle self,
63+
IReleaseEvent releaseEvent,
64+
bool isScene
65+
)
6166
{
6267
if (releaseEvent == null)
6368
{
64-
Addressables.Release(self);
69+
ReleaseHandle(self, isScene);
6570
throw new ArgumentNullException(nameof(releaseEvent),
6671
$"{nameof(releaseEvent)} is null so the handle can't be bound and will be released immediately.");
6772
}
6873

6974
void OnRelease()
7075
{
71-
Addressables.Release(self);
76+
ReleaseHandle(self, isScene);
7277
releaseEvent.Dispatched -= OnRelease;
7378
}
7479

@@ -88,13 +93,21 @@ public static AsyncOperationHandle<T> BindTo<T>(this AsyncOperationHandle<T> sel
8893
{
8994
if (releaseEvent == null)
9095
{
91-
Addressables.Release(self);
96+
ReleaseHandle(self, typeof(T) == typeof(SceneInstance));
9297
throw new ArgumentNullException(nameof(releaseEvent),
9398
$"{nameof(releaseEvent)} is null so the handle can't be bound and will be released immediately.");
9499
}
95100

96-
((AsyncOperationHandle)self).BindTo(releaseEvent);
101+
((AsyncOperationHandle)self).BindTo(releaseEvent, typeof(T) == typeof(SceneInstance));
97102
return self;
98103
}
104+
105+
private static void ReleaseHandle(AsyncOperationHandle handle, bool isScene)
106+
{
107+
if (isScene)
108+
Addressables.UnloadSceneAsync(handle);
109+
else
110+
Addressables.Release(handle);
111+
}
99112
}
100-
}
113+
}

0 commit comments

Comments
 (0)