LoginSignup
15
14

More than 5 years have passed since last update.

UnityでLive2Dの複数モーション再生

Last updated at Posted at 2016-02-02

Live2Dでアイドルモーションしつつ、顔だけモーション変えたいという場合に有効な方法をメモしておきます。
やり方は簡単で、モーション管理してるクラスのMotionQueueManagerを2つ作ればできます。

開発環境

・Live2D_SDK_Unity_2.1.00_1_jpのMotionプロジェクト
・Unity 5.2.0

複数モーション再生のやり方

1)まずは顔だけのモーションを作ります。
SDKサンプルのモーションファイル(.mtn)を開き、顔以外のパラメーターは削除しました。
ここではざっくりやりますが、本当は尺の長さは再生するもう1つのモーションと合わせた方がいい感じになるかと思います
スクリーンショット 0028-02-02 17.51.28.png

2)Live2D Viewerで動作確認
モーション1を再生しつつ、顔だけモーション再生してみます

・モーション1を再生した場合
defaultmotion.gif

・モーション1再生後に顔だけモーション再生した場合
mixmotion.gif

3)ソースを修正して、Inspectorにモーションファイルをアタッチ

SimpleModel.cs
using UnityEngine;
using System;
using System.IO;
using System.Collections;
using live2d;

[ExecuteInEditMode]
public class SimpleModel: MonoBehaviour 
{
    private Live2DModelUnity live2DModel;
    private Live2DMotion motion;
    private Live2DMotion faceMotion;            // Add
    private MotionQueueManager motionMgr;
    private MotionQueueManager faceMotionMgr;   // Add

    private Matrix4x4 live2DCanvasPos;

    public TextAsset mocFile ;
    public Texture2D[] textureFiles ;
    public TextAsset motionFile;
    public TextAsset faceMotionFile;    // Add

    void Start () 
    {
        Live2D.init();

        live2DModel = Live2DModelUnity.loadModel(mocFile.bytes);

        for (int i = 0; i < textureFiles.Length; i++)
        {
            live2DModel.setTexture(i, textureFiles[i]);
        }

        float modelWidth = live2DModel.getCanvasWidth();
        live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50.0f, 50.0f);

        motionMgr = new MotionQueueManager();
        faceMotionMgr = new MotionQueueManager();   // Add
        motion = Live2DMotion.loadMotion(motionFile.bytes);
        // Add
        faceMotion = Live2DMotion.loadMotion(faceMotionFile.bytes); 
    }


    void Update()
    {
        if (live2DModel == null) return;
        live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);

        if (!Application.isPlaying)
        {
            live2DModel.update();
            return;
        }

        if (motionMgr.isFinished())
        {
            motionMgr.startMotion(motion);
        }
        motionMgr.updateParam(live2DModel);

        // Add
        if (faceMotionMgr.isFinished())
        {
            faceMotionMgr.startMotion(faceMotion);
        }

        faceMotionMgr.updateParam(live2DModel);

        live2DModel.update();
    }


    void OnRenderObject()
    {
        if (live2DModel == null) return;
        live2DModel.draw();
    }
}

これで顔のパラメータが上書きされ複数モーション再生できました
finalmotion.gif

15
14
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
15
14