Skip to content

Commit

Permalink
if UNITY_2022_2_OR_NEWER, use destroyCancellationToken
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Oct 24, 2022
1 parent 3407367 commit ed617a0
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ namespace Cysharp.Threading.Tasks
{
public static class UniTaskCancellationExtensions
{
#if UNITY_2022_2_OR_NEWER

/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
public static CancellationToken GetCancellationTokenOnDestroy(this MonoBehaviour monoBehaviour)
{
return monoBehaviour.destroyCancellationToken;
}

#endif

/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
public static CancellationToken GetCancellationTokenOnDestroy(this GameObject gameObject)
{
Expand All @@ -17,6 +27,13 @@ public static CancellationToken GetCancellationTokenOnDestroy(this GameObject ga
/// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
public static CancellationToken GetCancellationTokenOnDestroy(this Component component)
{
#if UNITY_2022_2_OR_NEWER
if (component is MonoBehaviour mb)
{
return mb.destroyCancellationToken;
}
#endif

return component.GetAsyncDestroyTrigger().CancellationToken;
}
}
Expand Down

0 comments on commit ed617a0

Please sign in to comment.