yotiky Tech Blog

とあるエンジニアの備忘録

LitMotion Animation を拡張する

注意点として、Awake で実行すると予期せぬ動きになるのでStart以降で実行するのが良さそう。

完了

モーションを完了させる。未再生の場合は再生してから完了させる。 モーションは再生中のままになる。

public static void Complete(this LitMotionAnimation animation)
{
    if (!animation.IsPlaying) animation.Play();
    
    foreach (var component in animation.Components)
    {
        var handle = component.TrackedHandle;
        handle.TryComplete();
        component.TrackedHandle = handle;
    }
}

キャンセル

再生中のモーションをキャンセルする。 備え付けのStopだとモーションの状態が破棄されてオブジェクトの元の状態に戻る。 Cancelはキャンセルした時の状態を残す。

public static void Cancel(this LitMotionAnimation animation)
{
    foreach (var component in animation.Components)
    {
        var handle = component.TrackedHandle;
        handle.TryCancel();
        component.TrackedHandle = handle;
    }
}

再生速度を指定して再生

再生速度を指定して再生する。引数は倍率。1fで等倍、0で一時停止。

public static void Play(this LitMotionAnimation animation, float playbackSpeed)
{
    if (!animation.IsPlaying) animation.Play();
    
    foreach (var component in animation.Components)
    {
        var handle = component.TrackedHandle;
        handle.PlaybackSpeed = playbackSpeed;
        component.TrackedHandle = handle;
    }
}       

モーションの最初の状態にする

モーションの最初の状態に遷移させる。再生はキャンセルするので未再生状態になる。

public static void Rewind(this LitMotionAnimation animation)
{
    if (!animation.IsPlaying) animation.Play();
    
    foreach (var component in animation.Components)
    {
        var handle = component.TrackedHandle;
        handle.Time = 0;
        component.TrackedHandle = handle;
    }
    animation.Cancel();
}

こっちは上手く動かないバージョン。連続で呼ぶと機能しないっぽい。

public static void RewindBug(this LitMotionAnimation animation)
{
    animation.Restart();
    animation.Cancel();
}

モーションの最初の位置を取得する(不可)

settings.StartValueはアクセスできないので位置を取得することは不可能だった。

public static void StartPosition(this LitMotionAnimation animation)
{
    var startPos = (TransformPositionAnimation)animation.Components.FirstOrDefault(x => x is TransformPositionAnimation);
    if (startPos == null) return;
    // アクセス不可
    //Debug.Log(startPos.settings.StartValue);
}