原教程:siki:二次元日系游戏制作工具 - live2dSDK入门教程
http://www.sikiedu.com/my/course/282
一、准备工作
1、下载安装:Live2D_Cubism_Setup_3.2.05_en
2、解压Live2D_SDK_Unity_2.1.04_2_jp
导入framework、lib、tool 文件夹
3、导入:Epsilon
复制上,加后缀
二、学习部分
1、环境初始化-Live2dModel.cs
using live2d;
//初始化
Live2D.init();
2、读取模型
public TextAsset modeFile;
private Live2DModelUnity live2DModel;
//Live2DModelUnity.loadModel(Application.dataPath+ "/Resources/Epsilon/runtime/Epsilon.moc");
//TextAsset mocFile = Resources.Load("Epsilon/runtime/Epsilon.moc");
live2DModel= Live2DModelUnity.loadModel(modeFile.bytes);
//Texture2D texture2D1 = Resources.Load("Epsilon/runtime/Epsilon.1024/texture_00");
//Texture2D texture2D2 = Resources.Load("Epsilon/runtime/Epsilon.1024/texture_01");
//Texture2D texture2D3 = Resources.Load("Epsilon/runtime/Epsilon.1024/texture_02");
//live2DModel.setTexture(0, texture2D1);
//live2DModel.setTexture(1, texture2D2);
//live2DModel.setTexture(3, texture2D3);
public Texture2D[ ] textures;
for (int i = 0; i < textures.Length; i++)
{
live2DModel.setTexture(i, textures[i]);
}
4、显示小姐姐
初始化
private Matrix4x4 Live2DCanvasPos;
float modelWidth=live2DModel.getCanvasWidth();
Live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50, 50);
更新
live2DModel.setMatrix(transform.localToWorldMatrix*Live2DCanvasPos);
//更新模型所有的信息
live2DModel.update();
绘制
private void OnRenderObject()
{
live2DModel.draw();
}
//private Live2DMotion live2DMotionIdle;//读取动作
public TextAsset[] motionFiles; //动作文件
private Live2DMotion[] motions; //动作数组
//live2DMontionIdle = Live2DMotion.loadMotion(Application.dataPath+"");
//TextAsset mtnFile = Resources.Load("");
//live2DMontionIdle= Live2DMotion.loadMotion(mtnFile.bytes);
motions = new Live2DMotion[motionFiles.Length];
for (int i = 0; i < motions.Length ; i++)
{
motions[i] = Live2DMotion.loadMotion(motionFiles[i].bytes);
}
动作属性
private MotionQueueManager motionQueueManager; //动作管理
motions[0].setLoopFadeIn(false); //淡入 false 重复播放不淡入
motions[0].setFadeOut(1000); //淡出的时间 以毫秒为单位
motions[0].setFadeIn(1000); //淡出
motions[0].setLoop(true ); //循环播放
motionQueueManager = new MotionQueueManager();
motionQueueManager.startMotion(motions[0]);
更新
motionQueueManager.updateParam(live2DModel);
public int motionIndex;
if (Input .GetKeyDown(KeyCode.M))
{
motionIndex++;
if (motionIndex>=motions .Length)
{
motionIndex = 0;
}
motionQueueManager.startMotion(motions [motionIndex]);
}
6、设置优先级
设置优先级标准
1、动作未进行的状态,优先级为0
2、待机动作发生时,优先级为1
3、其他动作进行时,优先级为2
4、无视优先级,强制发生,优先级为3
using live2d.framework;
private L2DMotionManager l2DMotionManager;
l2DMotionManager = new L2DMotionManager();
//判断待机动作
if (l2DMotionManager.isFinished())
{
StarMotion(0, 1);
}
else if (Input .GetKeyDown(KeyCode.M))
{
StarMotion(14, 2);
}
l2DMotionManager.updateParam(live2DModel);
//motionIndex 当前动作优先级
//priority 想要播放动作的优先级
private void StarMotion(int motionIndex,int priority)
{
//想要播放动作的优先级小于正在播放的优先级
if (l2DMotionManager.getCurrentPriority()>=priority)
{
return;
}
l2DMotionManager.startMotion(motions[motionIndex]);
}
//直接确定角度ID, 参数值,权重
live2DModel.setParamFloat("PARAM_ANGLE_X",1,1);
if (Input .GetKeyDown(KeyCode.M) )
{
//累加a
live2DModel.addToParamFloat("PARAM_ANGLE_X", a);
}
//扩大倍数
live2DModel.multParamFloat("PARAM_ANGLE_X", a);
//通过索引获取参数并设置
int paramAngleX;
paramAngleX = live2DModel.getParamIndex("PARAM_ANGLE_X");
live2DModel.setParamFloat(paramAngleX,30);
//参数的保存与回复
live2DModel.setParamFloat("PARAM_ANGLE_X",30);
//保存与回复的参数是整个模型的所有参数,并不是之前方法里设置的某几个参数
live2DModel.saveParam();
live2DModel.loadParam();
//自动眨眼
private EyeBlinkMotion eyeblinkmition;
eyeblinkmition = new EyeBlinkMotion();
eyeblinkmition.setParam(live2DModel);
//设定模型某一部分的不透明度
live2DModel.setPartsOpacity("PARTS_01_FACE_001",0);
private L2DTargetPoint drag;
drag = new L2DTargetPoint();
Vector3 pos = Input.mousePosition;//屏幕坐标
if (Input.GetMouseButton(0))
{
drag.Set(pos.x/Screen.width*2-1,pos.y/Screen.height*2-1);
}
else if (Input.GetMouseButtonUp(0))
{
drag.Set(0, 0);
}
//参数及时更新,考虑到加速度等自然因素,计算坐标,进行逐帧更新
drag.update();
if (drag.getX()!=0)
{
live2DModel.setParamFloat("PARAM_ANGLE_X", 30 * drag.getX());
live2DModel.setParamFloat("PARAM_ANGLE_Y", 30 * drag.getY());
live2DModel.setParamFloat("PARAM_BODY_ANGLE_X", 10 * drag.getX());
live2DModel.setParamFloat("PARAM_EYE_BALL_X", 1 * drag.getX());
live2DModel.setParamFloat("PARAM_EYE_BALL_Y", 1 * drag.getX());
}
10、物理系统—设置头发长度重量与受到的空气阻力
//套用物理运算的设定
private PhysicsHair physicsHairSideLeft;
private PhysicsHair physicsHairSideRight;
private PhysicsHair physicsHairBack;
//头发的摇摆
physicsHairSideLeft = new PhysicsHair();
physicsHairSideRight = new PhysicsHair();
physicsHairBack = new PhysicsHair();
//套用物理运算
//长度(公尺)-影响摇摆的快慢(周期)
//空气阻力(0-1,预设0.5)-影响变慢的速度
//头发质量(千克)
physicsHairSideLeft.setup(0.2f, 0.5f, 0.14f);
physicsHairSideRight.setup(0.2f, 0.5f, 0.14f);
//设置输入参数
//设置哪一个部分变动时进行哪一个物理运算
PhysicsHair.Src srcXLeft = PhysicsHair.Src.SRC_TO_X; //横向摇摆
PhysicsHair.Src srcXRight = PhysicsHair.Src.SRC_TO_X; //横向摇摆
//"PARAM_ANGLE_X"变动时头发受到0.005倍影响度的输入参数
physicsHairSideLeft.addSrcParam(srcXLeft, "PARAM_ANGLE_X", 0.005f, 1);
physicsHairSideRight.addSrcParam(srcXRight, "PARAM_ANGLE_X", 0.005f, 1);
//设置输出表现
PhysicsHair.Target targetLeft = PhysicsHair.Target.TARGET_FROM_ANGLE;
PhysicsHair.Target targetRight = PhysicsHair.Target.TARGET_FROM_ANGLE;
physicsHairSideLeft.addTargetParam(targetLeft, "PARAM_HAIR_SIDE_L",0.005f,1);
physicsHairSideRight.addTargetParam(targetRight, "PARAM_HAIR_SIDE_R",0.005f,1);
//更新
long time=UtSystem.getUserTimeMSec(); //执行时间
physicsHairSideLeft.update (live2DModel,time);
physicsHairSideRight.update(live2DModel, time);