LoginSignup
1
2

More than 3 years have passed since last update.

Live2DモデルをAnimatorを使わずにスクリプトでアニメーションさせる

Posted at

Animatorを使わずにスクリプトでアニメーションさせる方法です。
まずLive2D.Cubism.Coreをインポートします

追加インポート
using Live2D.Cubism.Core;

次にアニメーションを管理するClassを宣言します。

actionclip
    public class actionclip
    {
        string ease = "Linear";
        public float startValue = 0;
        public float endValue = 0;
        public float duration = 0;
        public float delay = 0;

        private float currentDurationTime = 0;
        private float currentDelayTime = 0;

        public actionclip(float StartValue, float EndValue, float Duration, float NextDelay = 0, string Ease = "Linear")
        {
            startValue = StartValue;
            endValue = EndValue;
            duration = Duration < 0 ? 0 : Duration;
            delay = NextDelay < 0 ? 0 : NextDelay;
            ease = Ease;

            currentDurationTime = currentDelayTime = 0;
        }

        public void Reflesh()
        {
            currentDurationTime = currentDelayTime = 0;
        }

        public float Value
        {
            get
            {
                float p = endValue;
                if (currentDurationTime <= duration)
                {
                    currentDurationTime +=  Time.deltaTime;
                    float t = 1.0f;
                    if (duration > 0)
                    {
                        t = currentDurationTime / duration;
                    }
                    else
                    {
                        t = 1.0f;
                    }
                    t = t > 1.0f ? 1.0f : t;
                    switch (ease.ToUpper())
                    {
                        case "LINEAR":
                            break;
                        case "EASEIN":
                            t = Calc.EasyEaseIn(t);
                            break;
                        case "EASEOUT":
                            t = Calc.EasyEaseOut(t);
                            break;
                        case "EASEINOUT":
                            t = Calc.EasyEaseInOut(t);
                            break;
                    }
                    p = Mathf.Lerp(startValue, endValue, t);

                }
                else
                {
                    currentDelayTime += Time.deltaTime;
                }
                return p;
            }
        }
        public bool isAnimationEnd
        {
            get
            {
                return (Value == endValue && currentDelayTime > delay);
            }
        }
    }

Valueを呼び出すごとに開始値から終了値までの遷移値を取り出し、終了値になったらDelay時間待つだけのスクリプトです。前回の簡単イージングはここで使っています。
https://qiita.com/RYUMAGE/items/0351af94eff77dc32187

そしてLive2Dのパラメータを管理するクラスを宣言します

param
    public class param
    {
        public CubismParameter dat;
        //Animation
        private actionclip[] animations;
        public bool isRepeat = false;

        private int currentAnimCnt = 0;
        private float startValue = 0;

        public string ID
        {
            get
            {
                return dat.Id;
            }
        }

        public void SetAnimation(actionclip[] Animations, bool Repeat = false)
        {
            currentAnimCnt = 0;
            animations = Animations;
            isRepeat = Repeat;

            startValue = dat.Value;
        }
        public void StopAnimation()
        {
            isRepeat = false;
            animations = null;
        }

        public bool isRunning
        {
            get
            {
                return animations != null;
            }
        }

        public void Update()
        {
            if (animations != null)
            {
                dat.Value = animations[currentAnimCnt].Value;
                if (animations[currentAnimCnt].isAnimationEnd)
                {
                    animations[currentAnimCnt].Reflesh();
                    currentAnimCnt++;
                    if (currentAnimCnt >= animations.Length)
                    {
                        if (isRepeat)
                        {
                            currentAnimCnt = 0;
                        }
                        else
                        {
                            StopAnimation();
                        }
                    }
                }
            }
        }
    }

最後にLive2Dモデルデータのパラメーターをこのクラス内のdatに参照させます。
さらにLateUpdate()内でparamをUpdateさせます。

LateUpdate
    private param[] Live2DParams;
    private CubismModel Model;

    public void InitializeParameters(CubismModel model)
    {
        Model = model;
        Live2DParams = new param[model.Parameters.Length];

        for (int i = 0; i < model.Parameters.Length; i++)
        {
            Live2DParams[i] = new param();
            Live2DParams[i].dat = model.Parameters[i];
        }
    }

    void LateUpdate()
    {
        for (int i = 0; i < Live2DParams.Length; i++)
        {
            Live2DParams[i].Update();
        }
    }

こうやってMistyLink TouchableではAnimatorに寄らないスクリプトによるリアルタイムアニメーションをやっていました。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2