AnimeTask
February 19, 2023 · View on GitHub
UnityのTaskアニメーションライブラリ
Rx Version! -> kyubuns/AnimeRx
Read this document in other languages: English

Sample
-> 使用例
Basic
(-5f, 0f, 0f) から (5f, 0f, 0f) へ2秒かけて移動する。
await Easing.Create<Linear>(new Vector3(-5f, 0f, 0f), new Vector3(5f, 0f, 0f), 2f).ToLocalPosition(cube);
PlayTo
現在地から指定した位置まで移動する。
await Easing.Create<Linear>(new Vector3(-5f, 3f, 0f), 2f).ToLocalPosition(cube);
Easing
EasingのInCubicを利用して、指定した位置まで移動する。
await Easing.Create<InCubic>(new Vector3(-5f, 3f, 0f), 2f).ToLocalPosition(cube);
Linear
秒速1で、2秒間移動する。
await Moving.Linear(1f, 2f).ToLocalPositionX(cube);
Gravity
const float xRange = 5f;
const float yRangeMin = 5f;
const float yRangeMax = 10f;
await Moving.Gravity(
new Vector2(Random.Range(-xRange, xRange), Random.Range(yRangeMin, yRangeMax)),
Vector2.down * 9.8f,
5f
).ToLocalPosition(shape)
CalcDuration
距離から移動時間を計算して移動する。
await Easing.Create<OutCubic>(new Vector3(5f, 0f, 0f), x => x / 2f)
.Concat(Easing.Create<OutCubic>(new Vector3(5f, 2f, 0f), x => x / 2f))
.Concat(Easing.Create<OutCubic>(new Vector3(-5f, 0f, 0f), x => x / 2f))
.ToLocalPosition(cubes);
TranslateTo.Action
TranslateTo.Actionを利用すると、アニメーションした値を自由に使用出来る。
Easing.Create<Linear>(0, 100, 2f).ToAction<float>(x => Debug.Log(x))
UnscaledTime
スケジューラーは自作出来るので、特定のオブジェクトだけ時間を止めたりすることが可能。
デフォルトはTime.timeを利用していて、Time.unscaledTimeを利用するUnscaledTimeSchedulerも利用できる。
Easing.Create<Linear>(new Vector3(-5f, 0f, 0f), new Vector3(5f, 0f, 0f), 2f)
.ToLocalPosition(shape, default, new UnscaledTimeScheduler());
Update Timing
スケジューラーで更新タイミングを指定すれば、Update 以外のタイミングで値を更新することも可能です。
public class CustomScheduler : IScheduler
{
public float DeltaTime => Time.deltaTime;
public PlayerLoopTiming UpdateTiming => PlayerLoopTiming.PreUpdate;
}
Cancel
var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Token.Register(() => Debug.Log("Cancel"));
cancellationTokenSource.CancelAfter(500);
await Easing.Create<OutCubic>(new Vector3(5f, 0f, 0f), 2f).ToLocalPosition(cubes[0], cancellationTokenSource.Token);
Delay
2秒間右に等速で移動しつつ、最後の0.2秒でScaleを0にする
await UniTask.WhenAll(
Moving.Linear(3f, 2f).ToLocalPositionX(cube),
Animator.Delay(1.8f, Easing.Create<Linear>(Vector3.zero, 0.2f)).ToLocalScale(cube),
);
Convert
floatの推移を円運動に変換する。
await Easing.Create<OutCubic>(0.0f, Mathf.PI * 2.0f, 2f)
.Convert(x => new Vector3(Mathf.Sin(x), Mathf.Cos(x), 0.0f) * 3.0f)
.ToLocalPosition(go);
Concat
2秒で5fから0fまで移動し、1秒停止したあと、2秒で-5fへ移動する。
await Easing.Create<OutCubic>(5f, 0f, 2f)
.Delay(1f)
.Concat(Easing.Create<OutCubic>(0f, -5f, 2f))
.ToLocalPositionX(cubes[0]);
IProgress
IProgress をサポートしています。
await Easing.Create<Linear>(2f).ToProgress(Progress.Create<float>(x => Debug.Log(x)));
AnimationCanceller
var canceller = go.GetAnimationCanceller().Cancel();
Easing.Create<Linear>(1.0f, 0.5f).ToLocalPositionX(go, canceller.Token);
// 他のクラス/スコープ内で、他のアニメを中断する
var canceller = go.GetAnimationCanceller().Cancel();
Easing.Create<Linear>(0.0f, 0.5f).ToLocalPositionX(go, canceller.Token);
Skip
- (CancellationTokenを用いた) Cancelは
Cancelした瞬間に、その位置に停止します。 - (SkipTokenを用いた) Skipは
Skipした瞬間に、終了位置まで移動します。
var skipTokenSource = new SkipTokenSource();
Easing.Create<OutCubic>(new Vector3(5f, 0f, 0f), 5f).ToLocalPosition(cubes[0], default, skipTokenSource.Token).Forget();
await UniTask.Delay(TimeSpan.FromSeconds(1));
skipTokenSource.Skip();
UniRx.Extensions
var score = new ReactiveProperty<int>(0);
score
.SubscribeTask(async (x, cancellationToken) =>
{
scoreCounter.text = $"{x}";
await Easing.Create<OutBounce>(2f, 1f, 0.5f).ToLocalScale(scoreCounter, cancellationToken);
});
Instructions
- UniTaskをインポート
- AnimeTaskをインポート
- Package Manager
https://github.com/kyubuns/AnimeTask.git?path=Assets/AnimeTask - UnityPackage
- Package Manager
考え方
PlayやPlayToには2つの引数を渡します。
1個目がAnimator、2個目がTranslatorで、これらは明確に役割が異なります。
Animator
経過時間を受け取り、現在の値を返す。
Translator
値を反映する。
Requirements
- Unity2019.4 以降。
License
MIT License (see LICENSE)
Buy me a coffee
もしこのプロジェクトが気に入ったなら、ぜひコーヒーを奢ってください!
https://www.buymeacoffee.com/kyubuns
「ゲームに使ったよ!」
「このゲームにこのライブラリ使ったよ!」という報告を貰えるとめっちゃ喜びます!
メールやtwitterでお気軽にご連絡ください。
(MITライセンスのため、報告は義務ではありません。)
メッセージフォーム