Motion
最終更新: 2022年3月30日
概要
Motionを使用することで、UnityのMecanimでステートマシンを組まずにスクリプトからアニメーションを再生することが出来ます。
Motionを使用するにはCubismFadeControllerコンポーネントとUnityのAnimatorが必要です。
AnimatorコンポーネントにAnimatorControllerを設定した場合、Motionは再生されずにAnimatorControllerのアニメーションが再生されます。
該当のチュートリアル記事は こちら をご覧ください。
Motionは以下の処理を行っています。
- PlayableGraphを作成
- CubismMotionLayerを作成
- アニメーション再生
- アニメーション停止
PlayableGraphを作成
PlayableGraphはアニメーションなどを出力するためのものです。
こちらの詳細につきましては、Unity公式のドキュメントをご覧ください。
// Animator側のPlayableGraphを無効
var graph = animator.playableGraph;
if(graph.IsValid())
{
graph.GetOutput(0).SetWeight(0);
}
// PlayableGraphを作成
_playableGraph = PlayableGraph.Create("Playable Graph : " + this.FindCubismModel().name);
_playableGraph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
// 結果出力用のPlayable作成
_playableOutput = AnimationPlayableOutput.Create(_playableGraph, "Animation", animator);
_playableOutput.SetWeight(1);
// レイヤーを合成するPlayable作成
_layerMixer = AnimationLayerMixerPlayable.Create(_playableGraph, LayerCount);
// デフォルト出力先を設定
_playableOutput.SetSourcePlayable(_layerMixer);
この処理はCubismMotionController.OnEnable()で行っています。
CubismMotionLayerを作成
CubismMotionLayerを利用すると、複数のモーションを並列して再生させることができます。
// Create cubism motion layers.
_motionLayers = new CubismMotionLayer[LayerCount];
for(var i = 0; i < LayerCount; ++i)
{
_motionLayers[i] = CubismMotionLayer.CreateCubismMotionLayer(_playableGrap, _cubismFadeMotionList);
_motionLayers[i].AnimationEndHandler += OnAnimationEnd;
_layerMixer.ConnectInput(i, _motionLayers[i].PlayableOutput, 0);
_layerMixer.SetInputWeight(i, 1.0f);
}
この処理はCubismMotionController.OnEnable()で行っています。
複数のレイヤーでモーションを同時に再生させたときに、それらが同一のCubismParameter.Valueを操作すると、後に設定される値で上書きされます。
上書きするウェイトは、CubismMotionLayer.SetLayerWeight(float weight)で設定することができます。
CubismMotionLayerにウェイトを設定した場合、AnimationLayerMixerPlayableにも同時にウェイトを設定する必要があります。
AnimationLayerMixerPlayableの詳細につきましては、Unity公式のドキュメントをご覧ください。
_motionLayers[layerIndex].SetLayerWeight(weight); _layerMixer.SetInputWeight(layerIndex, weight);
アニメーション再生
CubismMotionController.PlayAnimation(AnimationClip clip, int layerIndex = 0, int priority = CubismMotionPriority.PriorityNormal, bool isLoop = true, float speed = 1.0f) を使うことで、アニメーションの再生が出来ます。
- AnimationClip clip:再生するアニメーションクリップ。
- int layerIndex:アニメーション再生レイヤーのインデックス。
- int priority:再生するアニメーションの優先度。現在再生されているアニメーションに差し込んで再生させる場合、そのアニメーションに設定されたpriorityよりも大きな値にする。
- bool isLoop:アニメーションはループ再生かどうか、デフォルトはループ再生。
- float speed:アニメーションの再生速度、範囲は0以上、デフォルトは1(通常の再生速度)。
アニメーション停止
指定インデックスのアニメーションを停止する場合は CubismMotionController.StopAnimation(int animationIndex, int layerIndex = 0) を使います。
- int animationIndex:停止するアニメーションのインデックス。
- int layerIndex:停止するアニメーションのレイヤーインデックス。
すべてのアニメーションを停止する場合は CubismMotionController.StopAllAnimation() を使います。