I have checked the live2D Manual at "Cubism SDK > platform > Unity", and I haven't found a clear way to change a model's expressions or animations through code.
What I'm trying to do is a function I can call, for example "ShowHappyCharacter();" and "ShowSadCharacter();" that I can use to change the expressions during gameplay.
0 ·
Comments
I think change of model's expressions is a code only to SampleApp1 project of Live2D Unity SDK.
It has change of model's expressions in LAppModel.cs .
[SampleApp1/Assets/Scripts/sample/LAppModel.cs]
public void Init(String modelJson) { ・ ・ // Expression .json Load if (modelSetting.GetExpressionNum() != 0) { var len = modelSetting.GetExpressionNum(); for (int i = 0; i < len; i++) { loadExpression(modelSetting.GetExpressionName(i), modelHomeDir + modelSetting.GetExpressionFile(i)); } } ・ ・ } public void Update() { ・ ・ // Expression parameter update if (expressionManager != null) expressionManager.updateParam(live2DModel); ・ ・ // parameter update live2DModel.update(); }Good luck !Naotaro, I think I have not explained my situation properly, but I am thankful for your response. What I want to do is to have the live2d model's emotion to change during gameplay, so I think that a function that uses "Input" during the "Update()" is required, but I don't know which.
For example, when I press "A", the emotion changes to "Confident Smile", and when I press "B", it changes to "Laughing".
Finding a function that changes the expression during "Update()" depending on a variable would also solve my issue.
You can switch facial expression by changing /sample/Simple/simpleModel.cs of Live2D SDK like below:
using UnityEngine; using live2d; using live2d.framework; // [ExecuteInEditMode] public class SimpleModel : MonoBehaviour { public TextAsset mocFile ; public Texture2D[] textureFiles ; private Live2DModelUnity live2DModel; private Matrix4x4 live2DCanvasPos; // expression public TextAsset expJsonA, expJsonB; //Please Drag & Drop .exp.json to expJsonA and expJsonB in the Inspector of the Unity Editor. AMotion expressionMotionA, expressionMotionB; MotionQueueManager expressionMgr; void Start () { if (live2DModel != null) return; Live2D.init(); 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); // load .exp.json expressionMotionA = L2DExpressionMotion.loadJson(expJsonA.bytes); expressionMotionB = L2DExpressionMotion.loadJson(expJsonB.bytes); expressionMgr = new MotionQueueManager(); } void Update() { if (live2DModel == null) return; live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos); if (!Application.isPlaying) { live2DModel.update(); return; } // Change Expression. if (Input.GetKeyDown(KeyCode.A)) {// pressed "A" key. expressionMgr.startMotion(expressionMotionA); } else if (Input.GetKeyDown(KeyCode.B)) {// pressed "B" key. expressionMgr.startMotion(expressionMotionB); } // update change of expression to Live2D model. expressionMgr.updateParam(live2DModel); live2DModel.update(); } void OnRenderObject() { if (live2DModel == null) return; live2DModel.draw(); } }Add L2DExpressionMotion.cs to the project because L2DExpressionMotion is used to load exp.json.
L2DExpressionMotion is in /framework folder of Live2D SDK.
I hope this will answer your question.
Thank you very much naotaro for your help and y_a_s_ for the code!