今天我們讓我們的老婆做一些簡單地動作
廢話少說開始吧!!!
首先我們先打開我們的unity
然后在unity里打開Epsilon里面的runtime檔案夾里面的motions檔案夾
在檔案夾的空白處右鍵點擊選中Show in explorer就會出現一個檔案夾
然后打開montions檔案
右鍵點擊空白處選擇按型別排序
然后選中前15個檔案

然后ctrl c和ctrl v 一下并且把所有復制的副本后邊加個后綴.bytes
之后創一個檔案夾把我們的副本檔案和復制前的檔案全部拉到那個檔案里

然后就可以關掉了這個檔案夾了
我們打開我們的腳本
我們來設定一下我們的動作物件
并且實體化
然后管理影片和播放影片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using live2d;
public class live2dModel : MonoBehaviour {
public TextAsset modelFile;
private Live2DModelUnity live2DModel;
private Matrix4x4 live2DCanvasPos;
public Texture2D[] textures;
public TextAsset[] motionFiles;
private Live2DMotion[] motions;
private MotionQueueManager motionQueueManager;
public int motionIndex;
// Use this for initialization
void Start () {
//初始化
Live2D.init();
//讀取模型
//TextAsset modelFile = Resources.Load<TextAsset>("Epsilon/runtime/Epsilon.moc");
live2DModel = Live2DModelUnity.loadModel(modelFile.bytes);
//與貼圖建立聯系
Live2DModelUnity.loadModel(modelFile.bytes);
for (int i = 0; i < textures.Length; i++)
{
live2DModel.setTexture(i, textures[i]);
}
//指定顯示位置與尺寸(實用正交矩陣與相關API顯示影像,再由游戲物體的位置和攝像機的size調整到合適的位置)
float modelWidth = live2DModel.getCanvasWidth();
live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50, 50);
//播放動作
//實體化動作物件
motions = new Live2DMotion[motionFiles.Length];
for (int i = 0; i < motions.Length; i++)
{
motions[i] = Live2DMotion.loadMotion(motionFiles[i].bytes);
}
//設定某一個影片的屬性
//重復播放不淡入
motions[0].setLoopFadeIn(false);
//設定淡入淡出時間,引數單位為毫秒
motions[0].setFadeOut(1000);
motions[0].setFadeIn(1000);
//影片是否回圈播放
motions[0].setLoop(true);
motionQueueManager = new MotionQueueManager();
motionQueueManager.startMotion(motions[0]);
}
// Update is called once per frame
void Update () {
//模型位置(矩陣形式的區域坐標轉換成世界坐標)
live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);
if (Input.GetKeyDown(KeyCode.M))
{
motionIndex++;
if (motionIndex>=motions.Length)
{
motionIndex = 0;
}
motionQueueManager.startMotion(motions[motionIndex]);
}
motionQueueManager.updateParam(live2DModel);
//更新模型定點等
live2DModel.update();
}
private void OnRenderObject()
{
live2DModel.draw();
}
}
之后回到我們的unity
點擊我們的空物體
把我們剛剛改的bytes檔案全部拉入

點擊運行我們的老婆就可以做一些簡單地動作啦
一開始的眨眼晃動
點擊m
1.點頭 2.搖頭 3.右傾頭 4.左擺頭微笑 5.叉腰前傾生氣 6.無奈 7.不開心睜眼8.害羞向下看 9.放電 10.懵逼 震驚 11.哭了 12.很生氣 13.不喜歡 14.搖來搖去
動作有這些你們可以自己試一下中間也有可能標錯的
然后我們就打開代碼
我們注釋掉一些內容
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using live2d;
public class live2dModel : MonoBehaviour {
public TextAsset modelFile;
private Live2DModelUnity live2DModel;
private Matrix4x4 live2DCanvasPos;
public Texture2D[] textures;
public TextAsset[] motionFiles;
private Live2DMotion[] motions;
private MotionQueueManager motionQueueManager;
public int motionIndex;
// Use this for initialization
void Start () {
//初始化
Live2D.init();
//讀取模型
//TextAsset modelFile = Resources.Load<TextAsset>("Epsilon/runtime/Epsilon.moc");
live2DModel = Live2DModelUnity.loadModel(modelFile.bytes);
//與貼圖建立聯系
//Live2DModelUnity.loadModel(modelFile.bytes);
for (int i = 0; i < textures.Length; i++)
{
live2DModel.setTexture(i, textures[i]);
}
//指定顯示位置與尺寸(實用正交矩陣與相關API顯示影像,再由游戲物體的位置和攝像機的size調整到合適的位置)
float modelWidth = live2DModel.getCanvasWidth();
live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50, 50);
//播放動作
//實體化動作物件
motions = new Live2DMotion[motionFiles.Length];
for (int i = 0; i < motions.Length; i++)
{
motions[i] = Live2DMotion.loadMotion(motionFiles[i].bytes);
}
//設定某一個影片的屬性
//重復播放不淡入
motions[0].setLoopFadeIn(false);
//設定淡入淡出時間,引數單位為毫秒
motions[0].setFadeOut(1000);
motions[0].setFadeIn(1000);
//影片是否回圈播放
//motions[0].setLoop(true);
//motionQueueManager = new MotionQueueManager();
//motionQueueManager.startMotion(motions[0]);
}
// Update is called once per frame
void Update () {
//模型位置(矩陣形式的區域坐標轉換成世界坐標)
live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);
//if (Input.GetKeyDown(KeyCode.M))
//{
// motionIndex++;
// if (motionIndex>=motions.Length)
// {
// motionIndex = 0;
// }
// motionQueueManager.startMotion(motions[motionIndex]);
//}
//motionQueueManager.updateParam(live2DModel);
//更新模型定點等
live2DModel.update();
}
private void OnRenderObject()
{
live2DModel.draw();
}
}
我們打開live2D這個軟體和epsilon檔案夾(這個是我們一開始在網盤里下載的模型源檔案)
把我們的Epsilon.cmox拖入live2d軟體里

大家可以試試這個軟體怎么用這里我就不詳細說了
我們寫一個自動眨眼和人物跟隨滑鼠拖拽
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using live2d;
using live2d.framework;
public class live2dModel : MonoBehaviour {
public TextAsset modelFile;
private Live2DModelUnity live2DModel;
private Matrix4x4 live2DCanvasPos;
public Texture2D[] textures;
private L2DMotionManager L2DMotionManager;
public TextAsset[] motionFiles;
private Live2DMotion[] motions;
private MotionQueueManager motionQueueManager;
public int motionIndex;
//自動眨眼
private EyeBlinkMotion eyeBlinkMotion;
//滑鼠拖拽引起動作變化
private L2DTargetPoint drag;
// Use this for initialization
void Start () {
//初始化
Live2D.init();
//讀取模型
//TextAsset modelFile = Resources.Load<TextAsset>("Epsilon/runtime/Epsilon.moc");
live2DModel = Live2DModelUnity.loadModel(modelFile.bytes);
//與貼圖建立聯系
//Live2DModelUnity.loadModel(modelFile.bytes);
for (int i = 0; i < textures.Length; i++)
{
live2DModel.setTexture(i, textures[i]);
}
//指定顯示位置與尺寸(實用正交矩陣與相關API顯示影像,再由游戲物體的位置和攝像機的size調整到合適的位置)
float modelWidth = live2DModel.getCanvasWidth();
live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50, 50);
//播放動作
//實體化動作物件
motions = new Live2DMotion[motionFiles.Length];
for (int i = 0; i < motions.Length; i++)
{
motions[i] = Live2DMotion.loadMotion(motionFiles[i].bytes);
}
//設定某一個影片的屬性
//重復播放不淡入
motions[0].setLoopFadeIn(false);
//設定淡入淡出時間,引數單位為毫秒
motions[0].setFadeOut(1000);
motions[0].setFadeIn(1000);
//影片是否回圈播放
//motions[0].setLoop(true);
//motionQueueManager = new MotionQueueManager();
//motionQueueManager.startMotion(motions[0]);
//動作的優先級使用
L2DMotionManager = new L2DMotionManager();
//眨眼
eyeBlinkMotion = new EyeBlinkMotion();
//滑鼠拖拽
drag = new L2DTargetPoint();
}
// Update is called once per frame
void Update () {
//模型位置(矩陣形式的區域坐標轉換成世界坐標)
live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);
//if (Input.GetKeyDown(KeyCode.M))
//{
// motionIndex++;
// if (motionIndex>=motions.Length)
// {
// motionIndex = 0;
// }
// motionQueueManager.startMotion(motions[motionIndex]);
//}
//motionQueueManager.updateParam(live2DModel);
//模型跟隨滑鼠轉向與看向
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", drag.getX());
live2DModel.setParamFloat("PARAM_EYE_BALL_Y", drag.getY());
}
//眨眼
eyeBlinkMotion.setParam(live2DModel);
//更新模型定點等
live2DModel.update();
}
private void OnRenderObject()
{
live2DModel.draw();
}
private void StartMotion(int motionIndex, int priority)
{
if (L2DMotionManager.getCurrentPriority() >= priority)
{
return;
}
L2DMotionManager.startMotion(motions[motionIndex]);
}
}
在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”, drag.getX());
live2DModel.setParamFloat(“PARAM_EYE_BALL_Y”, drag.getY());
}
我們這里面的第一個引數是在live2d軟體里的
Parameter
我們右鍵點擊角度X
選中Edit paramter
就會出現一個視窗
我們用到的就是這個ID
這個是模型ID是創建原模型的模型師規定的

這五個分別是
1.角度X
2.角度Y
3.體の回轉(滑稽)
4.目玉X
5.目玉Y
而且我們的角度旋轉的最大值是±30
而我們的身體卻只有±10
然后ctrl s
點擊unity運行
我們的老婆就會跟著滑鼠點擊的方向看了
而且如果你想讓她眼里只有你的時候
我們只需要把
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”, drag.getX());
live2DModel.setParamFloat(“PARAM_EYE_BALL_Y”, drag.getY());
}
這個里面的后兩個的第二個引數改成負的
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”, -drag.getX());
live2DModel.setParamFloat(“PARAM_EYE_BALL_Y”, -drag.getY());
}
這樣我們ctrl s點擊unity運行
超可愛!!!
好今天我們就做到
喜歡的可以一鍵三連
求求你!這對我真的很重要!
那么下周見啦
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/230362.html
標籤:其他
