1
This commit is contained in:
68
Src/Core/Tween/BITween.cs
Normal file
68
Src/Core/Tween/BITween.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
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();
|
||||
}
|
||||
|
||||
public static async UniTask MoveToForward(
|
||||
Action<float> setter,
|
||||
float from,
|
||||
float to,
|
||||
float duration = 1,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
var t = 0f;
|
||||
var delta = 1 / duration;
|
||||
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);
|
||||
var next = math.lerp(from, to, t);
|
||||
//BIT4Log.Log<TweenSequence>($"当前进度:[{t}]next:[{next}]");
|
||||
|
||||
#if UNITY_64
|
||||
await UniTask.NextFrame(cancellationToken);
|
||||
await UniTask.SwitchToMainThread(cancellationToken);
|
||||
#else
|
||||
await UniTask.Yield();
|
||||
#endif
|
||||
setter(next);
|
||||
}
|
||||
if (cancellationToken.IsCancellationRequested is false)
|
||||
setter(to);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user