Skip to content

Instantly share code, notes, and snippets.

@tetya tetya/SimpleModel.cs
Created Mar 24, 2016

Embed
What would you like to do?
Live2Dサンプルにモーションを追加(待機モーションが再生されない&ニッコリしない)
using UnityEngine;
using System;
using System.Collections;
using live2d;
using live2d.framework;
[ExecuteInEditMode]
public class SimpleModel : MonoBehaviour
{
public TextAsset mocFile;
public TextAsset physicsFile;
public Texture2D[] textureFiles;
public TextAsset[] mtnFiles; // mtnファイル
private Live2DModelUnity live2DModel;
private EyeBlinkMotion eyeBlink = new EyeBlinkMotion();
private L2DTargetPoint dragMgr = new L2DTargetPoint();
private L2DPhysics physics;
private Matrix4x4 live2DCanvasPos;
private Live2DMotion[] motion = new Live2DMotion[8]; // Live2Dモーションクラス
private MotionQueueManager motionManager; // モーション管理クラス
private MotionQueueManager motionManager2; // モーション管理クラス
void Start()
{
Live2D.init();
// モーションのインスタンスの作成(mtnの読み込み
for(int i = 0 ; i < 8 ; i++){
motion [i] = Live2DMotion.loadMotion (mtnFiles [i].bytes);
}
//待機モーションはループ設定にしておく
motion[0].setLoop( true );
// モーション管理クラスのインスタンスの作成
motionManager = new MotionQueueManager();
motionManager2 = new MotionQueueManager();
// モーションの再生
motionManager.startMotion( motion[0], false );
load();
}
void load()
{
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);
if (physicsFile != null) physics = L2DPhysics.load(physicsFile.bytes);
}
void Update()
{
MotionUpdate ();
if (live2DModel == null) load();
live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);
if (!Application.isPlaying)
{
live2DModel.update();
return;
}
var pos = Input.mousePosition;
if (Input.GetMouseButtonDown(0))
{
//
}
else if (Input.GetMouseButton(0))
{
dragMgr.Set(pos.x / Screen.width * 2 - 1, pos.y / Screen.height * 2 - 1);
}
else if (Input.GetMouseButtonUp(0))
{
dragMgr.Set(0, 0);
}
dragMgr.update();
live2DModel.setParamFloat("PARAM_ANGLE_X", dragMgr.getX() * 30);
live2DModel.setParamFloat("PARAM_ANGLE_Y", dragMgr.getY() * 30);
live2DModel.setParamFloat("PARAM_BODY_ANGLE_X", dragMgr.getX() * 10);
live2DModel.setParamFloat("PARAM_EYE_BALL_X", -dragMgr.getX());
live2DModel.setParamFloat("PARAM_EYE_BALL_Y", -dragMgr.getY());
double timeSec = UtSystem.getUserTimeMSec() / 1000.0;
double t = timeSec * 2 * Math.PI;
live2DModel.setParamFloat("PARAM_BREATH", (float)(0.5f + 0.5f * Math.Sin(t / 3.0)));
eyeBlink.setParam(live2DModel);
if (physics != null) physics.updateParam(live2DModel);
live2DModel.update();
}
void MotionUpdate()
{
if(Input.GetButtonDown("Fire2")){
//乱数の用意
int random = UnityEngine.Random.Range (1,7);
// モーションの再生
motionManager2.startMotion( motion[random], false );
Debug.Log (random);
}
// 再生中のモーションからモデルパラメータを更新
motionManager.updateParam( live2DModel );
motionManager2.updateParam( live2DModel );
}
void OnRenderObject()
{
if (live2DModel == null) load();
if (live2DModel.getRenderMode() == Live2D.L2D_RENDER_DRAW_MESH_NOW) live2DModel.draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.