与二次元老婆邂逅的游戏的创建过程(二)

今天我们让我们的老婆做一些简单地动作
废话少说开始吧!!!
首先我们先打开我们的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运行
超可爱!!!
好今天我们就做到
喜欢的可以一键三连
求求你!这对我真的很重要!
那么下周见啦

  • 15
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值