#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System.Threading;
using UnityEngine;
using Cysharp.Threading.Tasks.Triggers;
namespace Cysharp.Threading.Tasks
{
public static class UniTaskCancellationExtensions
{
/// This CancellationToken is canceled when the MonoBehaviour will be destroyed.
public static CancellationToken GetCancellationTokenOnDestroy(this GameObject gameObject)
{
return gameObject.GetAsyncDestroyTrigger().CancellationToken;
}
/// This CancellationToken is canceled when the MonoBehaviour will be destroyed.
public static CancellationToken GetCancellationTokenOnDestroy(this Component component)
{
return component.GetAsyncDestroyTrigger().CancellationToken;
}
}
}
namespace Cysharp.Threading.Tasks.Triggers
{
public static partial class AsyncTriggerExtensions
{
// Util.
static T GetOrAddComponent(GameObject gameObject)
where T : Component
{
#if UNITY_2019_2_OR_NEWER
if (!gameObject.TryGetComponent(out var component))
{
component = gameObject.AddComponent();
}
#else
var component = gameObject.GetComponent();
if (component == null)
{
component = gameObject.AddComponent();
}
#endif
return component;
}
// Special for single operation.
/// This function is called when the MonoBehaviour will be destroyed.
public static UniTask OnDestroyAsync(this GameObject gameObject)
{
return gameObject.GetAsyncDestroyTrigger().OnDestroyAsync();
}
/// This function is called when the MonoBehaviour will be destroyed.
public static UniTask OnDestroyAsync(this Component component)
{
return component.GetAsyncDestroyTrigger().OnDestroyAsync();
}
public static UniTask StartAsync(this GameObject gameObject)
{
return gameObject.GetAsyncStartTrigger().StartAsync();
}
public static UniTask StartAsync(this Component component)
{
return component.GetAsyncStartTrigger().StartAsync();
}
public static UniTask AwakeAsync(this GameObject gameObject)
{
return gameObject.GetAsyncAwakeTrigger().AwakeAsync();
}
public static UniTask AwakeAsync(this Component component)
{
return component.GetAsyncAwakeTrigger().AwakeAsync();
}
}
}