live2D是一个很强大的2D动画组件。我们可以使用AS3脚本对它进行热更新。
live2D在Unity中的使用请看这里:
总得来说,我们可以先去live2D官网下载它的Unity SDK,然后即可在Unity中使用。我们这里使用的是live2d 2.1版。
我们的目标是把 **Live2D_SDK_Unity_2.1.02_1_jp\sample\Demo** 这个unity示例工程改造成as3热更新版本。
首先您要先创建一个空白的Unity工程。然后将下载好的Live2D SDK目录中,上面提到的**Live2D_SDK_Unity_2.1.02_1_jp\sample\Demo**下的所有文件拖拽到Unity项目里。
然后使用ActionScript3热更新脚本系统将Live2D 的API导出给AS3脚本备用。如果您不了解这个热更新脚本,请看这里的链接和之前的系列教程。
<!--Configure DLLs to export-->
<buildassemblys>
<assembly value="D:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\System.dll"></assembly>
<assembly value="D:\Program Files\Unity\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll"></assembly>
<assembly value="D:\Program Files\Unity\Editor\Data\UnityExtensions\Unity\GUISystem\UnityEngine.UI.dll"></assembly>
<assembly value="F:\UnityAS3\Live2DDemo\Library\ScriptAssemblies\Assembly-CSharp.dll"></assembly>
<assembly value="F:\UnityAS3\Live2DDemo\Assets\Live2D\Live2DUnity.dll"></assembly>
</buildassemblys>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Live2DUtil
{
public class Live2DBehaviour : MonoBehaviour
{
// Use this for initialization
public virtual void Start()
{
}
// Update is called once per frame
public virtual void Update()
{
}
public virtual void OnRenderObject()
{
}
}
}
package
{
/**
* ...
* @author ...
*/
public class Live2DDemo
{
public function Live2DDemo()
{
}
}
}
import live2d.EyeBlinkMotion;
import live2d.Live2D;
import live2d.Live2DModelUnity;
import live2d.UtSystem;
import live2d.framework.L2DPhysics;
import live2d.framework.L2DTargetPoint;
import live2dutil.Live2DBehaviour;
import unityengine.Camera;
import unityengine.GameObject;
import unityengine.Input;
import unityengine.Matrix4x4;
import unityengine.MonoBehaviour;
import unityengine.Resources;
import unityengine.Screen;
import unityengine.TextAsset;
import unityengine.Texture2D;
var lmodel:GameObject = new GameObject("as3live2dmodel");
class SimpleModel extends Live2DBehaviour
{
var mocFile:TextAsset;
var physicsFile:TextAsset;
var live2DModel:Live2DModelUnity;
var eyeBlink:EyeBlinkMotion = new EyeBlinkMotion();
var dragMgr:L2DTargetPoint = new L2DTargetPoint();
var physics:L2DPhysics;
var live2DCanvasPos:Matrix4x4;
override public function start():void
{
Live2D.init();
load();
}
function load():void
{
mocFile = Resources.load("haru.moc") as TextAsset;
physicsFile = Resources.load("haru.physics.json") as TextAsset;
live2DModel = Live2DModelUnity.loadModel__(mocFile.bytes);
live2DModel.setTexture(0,Resources.load_("haru.1024/texture_00", Texture2D) as Texture2D);
live2DModel.setTexture(1,Resources.load_("haru.1024/texture_01", Texture2D) as Texture2D);
live2DModel.setTexture(2,Resources.load_("haru.1024/texture_02", Texture2D) as Texture2D);
var modelWidth:Number = live2DModel.getCanvasWidth();
live2DCanvasPos = Matrix4x4.ortho(0, modelWidth, modelWidth, 0, -50.0, 50.0);
if (physicsFile != null) physics = L2DPhysics.load(physicsFile.bytes);
trace("loaded");
}
override public function update():void
{
if (live2DModel == null) return;
live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);
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());
var timeSec:Number = Number( UtSystem.getUserTimeMSec()) / 1000.0;
var t:Number = timeSec * 2 * Math.PI;
live2DModel.setParamFloat("PARAM_BREATH", (0.5 + 0.5 * Math.sin(t / 3.0)));
eyeBlink.setParam(live2DModel);
if (physics != null) physics.updateParam(live2DModel);
live2DModel.update();
}
override public function onRenderObject():void
{
if (live2DModel == null)
return;
if (live2DModel.getRenderMode() == Live2D.L2D_RENDER_DRAW_MESH_NOW)
live2DModel.draw();
}
}
lmodel.addComponent(SimpleModel);