2024-03-31 23:31:00 +08:00
|
|
|
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<UniTask> 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();
|
|
|
|
}
|
|
|
|
|
2025-03-24 14:42:40 +08:00
|
|
|
public static async UniTask MoveToForward<T>(Action<T> setter, T from, T to, float delta,
|
|
|
|
Func<T, T, float, T> 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
|
2025-04-14 15:39:28 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await UniTask.NextFrame(PlayerLoopTiming.FixedUpdate, cancellationToken);
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2025-03-24 14:42:40 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
await UniTask.Yield();
|
|
|
|
#endif
|
|
|
|
setter(from);
|
|
|
|
}
|
|
|
|
if (cancellationToken.IsCancellationRequested is false)
|
|
|
|
setter(to);
|
|
|
|
}
|
|
|
|
public static async UniTask Lerp<T>(Action<T> setter,T from,T to,float duration, Func<T, T,float, T> func,CancellationToken cancellationToken = default)
|
2024-03-31 23:31:00 +08:00
|
|
|
{
|
|
|
|
var t = 0f;
|
2025-03-09 13:38:23 +08:00
|
|
|
var delta = 1f / duration;
|
2024-03-31 23:31:00 +08:00
|
|
|
setter(from);
|
|
|
|
//BIT4Log.Log<TweenSequence>($"已创建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);
|
2025-03-24 14:42:40 +08:00
|
|
|
var next = func(from, to, t);
|
2024-06-14 14:11:28 +08:00
|
|
|
#if UNITY_5_3_OR_NEWER
|
2024-03-31 23:31:00 +08:00
|
|
|
await UniTask.NextFrame(cancellationToken);
|
|
|
|
#else
|
|
|
|
await UniTask.Yield();
|
|
|
|
#endif
|
|
|
|
setter(next);
|
|
|
|
}
|
|
|
|
if (cancellationToken.IsCancellationRequested is false)
|
|
|
|
setter(to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|