#if LITMOTION_SUPPORT_UNITASK
using System.Threading;
using Cysharp.Threading.Tasks;
namespace LitMotion
{
///
/// Provides extension methods for UniTask integration.
///
public static class LitMotionUniTaskExtensions
{
///
/// Convert motion handle to UniTask.
///
/// This motion handle
/// CancellationToken
///
public static UniTask ToUniTask(this MotionHandle handle, CancellationToken cancellationToken = default)
{
if (!handle.IsActive()) return UniTask.CompletedTask;
return new UniTask(UniTaskMotionConfiguredSource.Create(handle, CancelBehaviour.CancelAndCancelAwait, cancellationToken, out var token), token);
}
///
/// Convert motion handle to UniTask.
///
/// This motion handle
/// Behavior when canceling
/// CancellationToken
///
public static UniTask ToUniTask(this MotionHandle handle, CancelBehaviour cancelBehaviour, CancellationToken cancellationToken = default)
{
if (!handle.IsActive()) return UniTask.CompletedTask;
return new UniTask(UniTaskMotionConfiguredSource.Create(handle, cancelBehaviour, cancellationToken, out var token), token);
}
///
/// Create a motion data and bind it to AsyncReactiveProperty.
///
/// The type of value to animate
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
/// Target object that implements IProgress
/// Handle of the created motion data.
public static MotionHandle BindToAsyncReactiveProperty(this MotionBuilder builder, AsyncReactiveProperty reactiveProperty)
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(reactiveProperty);
return builder.BindWithState(reactiveProperty, static (x, target) =>
{
target.Value = x;
});
}
}
}
#endif