using System; using System.Collections; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using Unity.Mathematics; namespace BITKit.Tween { public static class BITween { public class TweenSequence { internal TweenSequence(){} private readonly List tasks = new(); public TweenSequence Append(UniTask task) { tasks.Add(task); return this; } public async UniTask Play(CancellationToken cancellationToken=default) { foreach (var task in tasks) { await task; if (cancellationToken.IsCancellationRequested) return; } } } public static TweenSequence CreateSequence() { return new TweenSequence(); } public static async UniTask MoveToForward(Action setter, T from, T to, float delta, Func func, CancellationToken cancellationToken = default) { setter(from); while (Equals(from,to) is false && cancellationToken.IsCancellationRequested is false) { from = func(from, to, delta*BITApp.Time.DeltaTime); #if UNITY_5_3_OR_NEWER try { await UniTask.NextFrame(PlayerLoopTiming.FixedUpdate, cancellationToken); } catch (OperationCanceledException) { return; } #else await UniTask.Yield(); #endif setter(from); } if (cancellationToken.IsCancellationRequested is false) setter(to); } public static async UniTask Lerp(Action setter,T from,T to,float duration, Func func,CancellationToken cancellationToken = default) { var t = 0f; var delta = 1f / duration; setter(from); //BIT4Log.Log($"已创建Tween,from:[{from}]to:[{to}]duration:[{duration}]delta:[{delta}]"); while (t < 1 && cancellationToken.IsCancellationRequested is false) { t = math.clamp(t + delta*BITApp.Time.DeltaTime, 0, 1); var next = func(from, to, t); #if UNITY_5_3_OR_NEWER await UniTask.NextFrame(cancellationToken); #else await UniTask.Yield(); #endif setter(next); } if (cancellationToken.IsCancellationRequested is false) setter(to); } } }