スクリプトからモーションを再生させる
最終更新: 2020年1月30日
このページでは、CubismのモデルにMotionControllerの設定手順を説明します。
以下は「SDKをインポート」を行ったプロジェクトに追加することを前提としています。
概要
UnityのMecanimを利用せず、スクリプトからアニメーションを再生する場合、Cubism SDK for Unity では「CubismMotionController」というコンポーネントを利用することで設定することができます。
CubismMotionContollerの詳細につきましては「Motion」 をご覧ください。
CubismMotionControllerの動作には、Animatorコンポーネントを必要とします。
しかし、CubismMotionControllerはAnimatorのAnimatorControllerを用いたモーション再生と共存させることはできません。
そのため、CubismMotionControllerによってモーションを再生させる場合、AnimatorにAnimatorControllerは設定しないでください。
Cubismのモデルに上記の設定をする場合には、以下の手順を行います。
- CubismMotionControllerをアタッチ
- AnimationClipを再生させる命令を呼び出すコンポーネントを用意
- モーションを再生させるボタンを作成
1. CubismMotionControllerをアタッチ
モデルのルートとなるGameObjectに、アニメーションを再生する「CubismMotionController」をアタッチします。


CubismMotionControllerには、設定項目が1つあります。
– Layer Count:CubismMotionControllerが管理するアニメーションのレイヤーの総数。設定する値の範囲は1以上。
CubismMotionControllerをアタッチしたとき、そのGameObjectにAnimatorやCubismFadeControllerがアタッチされていない場合、同時にアタッチされます。
CubismMotionControllerは、モーションの再生にPlayableを利用しているためAnimatorを必要とします。
AnimatorコンポーネントのControllerにAnimatorControllerが設定されている場合、そちらが優先されます。
CubismMotionControllerを利用するのであれば、ここには何も設定しないでください。
また、CubismFadeControllerは、CubismMotionControllerがモーションとモーションのブレンドにMotionFadeを利用しているため必要となります。
CubismFadeController.CubismFadeMotionListが設定されていない場合、生成された.cadeMotionListを設定してください。
.fadeMotionListの設定につきましては 「Mecanimでモーションフェードを有効にする」 をご覧ください。
2. AnimationClipを再生させる命令を呼び出すコンポーネントを用意
「MotionPlayer」というC#スクリプトを作成し、コードを以下のように書き換えます。
ここでは、引数のAnimationClipを再生させるようにしております。
using Live2D.Cubism.Framework.Motion; using UnityEngine; public class MotionPlayer : MonoBehaviour { CubismMotionController _motionController; private void Start() { _motionController = GetComponent<CubismMotionController>(); } public void PlayMotion(AnimationClip animation) { if ((_motionController == null) || (animation == null)) { return; } _motionController.PlayAnimation(animation, isLoop: false); } }
1でCubismMotionControllerをアタッチしたGameObjectに、作成したMotionPlayerをアタッチします。

以上でスクリプトからモーションを再生する設定は完了です。
外部のスクリプトなどからMotionPlayer.PlayMotion()を呼び出し、引数に再生したいAnimationClipを渡すことでモーションを再生させることができます。
ここではボタンからこの関数を呼び出します。
3. モーションを再生させるボタンを作成
Hierarchyウィンドウ上部のCreateボタンまたはHierarchyウィンドウを右クリックからUI > Buttonをクリックし、ButtonをSceneに作成します。
作成したButtonは任意の場所に配置します。

作成したButtonを選択状態にし、InspectorウィンドウからButtonの「On Click()」下部の「+」をクリックします。
追加したリストに、Hirarchy内のモデルのGameObjectをドラッグアンドドロップし、右のドロップダウンメニューから MotionPlayer / PlayMotion() を選択します。



最後に、引数に再生させるAnimationClipを設定します。

以上で設定は完了です。 この状態で実行すると、Gameビューのボタンをクリックしたときにモーションが再生されます。
