diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs index 49e7f0b..bad5a04 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Triggers/AsyncTriggerExtensions.cs @@ -8,6 +8,16 @@ namespace Cysharp.Threading.Tasks { public static class UniTaskCancellationExtensions { +#if UNITY_2022_2_OR_NEWER + + /// This CancellationToken is canceled when the MonoBehaviour will be destroyed. + public static CancellationToken GetCancellationTokenOnDestroy(this MonoBehaviour monoBehaviour) + { + return monoBehaviour.destroyCancellationToken; + } + +#endif + /// This CancellationToken is canceled when the MonoBehaviour will be destroyed. public static CancellationToken GetCancellationTokenOnDestroy(this GameObject gameObject) { @@ -17,6 +27,13 @@ namespace Cysharp.Threading.Tasks /// This CancellationToken is canceled when the MonoBehaviour will be destroyed. 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; } }