Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

If you have any questions, reports, suggestions, or requests about Live2D, please send them to this community.
※We cannot guarantee statements or answers from Live2D staff. Thank you for your understanding in advance.
 
Live2D Cubism
Cubism Products and Downloads
Cubism product manuals and tutorials
Cubism Editor Manual    Cubism Editor Tutorial    Cubism SDK Manual    Cubism SDK Tutorial
"[NOTICE]" About the support for Mac models
Cubism Editor is not supported with Apple M1 processors.
Please refer to System Requirements for details.
We will not respond to any related inquiries with this message.
Please note that Cubism SDK is not supported with Apple M1 processors and macOS 11.0 Big Sur.
Live2D user has created a tutorial video(unoffilcial) in English. SEE ALSO.
Thank you, brian_tsui!
[Live2D Tutorial 2020] Introduction to Tools and Technical Concepts

How do I change the model's expressions during gameplay?

edited February 2016 in Help
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.

Comments

  • Hi, CBeam .

    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 !
  • :'( I really wish I could help you with this CBeam, but I know nothing about Unity. I hope naotaro answered your question. He/She is awesome. Good luck!!
  • edited December 2015
    Ah, it's ok Wai, thank you though.

    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.

    image
  • edited December 2015
    My Acquaintance is creating a simple script . Please wait a little .
  • Hi, CBeam.

    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.
  • edited December 2015
    I have followed your instructions and used expression 6 and 8. It works!

    Thank you very much naotaro for your help and y_a_s_ for the code!

    image
Sign In or Register to comment.