Creating a Project Using the Simple

This is an explanation about the process from creating the Unity project to make Live2D model be displayed.

Refer to this page about the tutorial video using the SDK sample.

◆Things to prepare
  • Unity No need to be Unity Pro.
  • Live2D library  (It is "Live2DUnity.dll" in the "lib" folder of downloaded Live2D SDK)
  • The resource of Live2D
You originally need to prepare the resource of Live2D according to the each model to be created.
We are going to explain it with the resource of a sample project "Haru".

simple/Assets/Resources/
  • haru.moc.bytes
  • haru.1024/texture_00.png
  • haru.1024/texture_01.png
  • haru.1024/texture_02.png



◆Create and prepare for the project 

Create a new project, and make a folder in the project view.

Select "Create" in the project view, and create four folders in "Folder", and rename them as "Live2D", "Resources", "Scene", 
and "Scripts".




Import Live2D SDK and the resource

Drag and drop "Live2DUnity.dll" in the Live2D SDK's "lib" folder into the "Live2D" folder.





Drag and drop "haru.1024" folder with the Texture and "haru.moc.bytes" into the "Resources" folder.





Select the "Scripts" folder and right-click it to select "C# Script" - "Create".
Rename it as you with. We rename it as "main" as an example.




The project view is as below.







◆Create a game object displaying a Live2D model


From the menu bar, select "GameObject" - "Create Empty" to create a blank game object.





◆Make settings of a script

You can make settings of a script to display Live2D.
Refer to the each document about the detailed settings.


Drag and drop "main" to "GameObject".




Open "main" to add as follows;

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using live2d;
  5.  
  6. public class main : MonoBehaviour {
  7.         public TextAsset mocFile;
  8.         public Texture2D[] textures;
  9.        
  10.         private Live2DModelUnity live2DModel;
  11.        
  12.        
  13.         // Use this for initialization
  14.         void Start () {
  15.                
  16.         }
  17.                
  18.         // Update is called once per frame
  19.         void Update () {
  20.             
  21.         }
  22. }



Select the GameObject added "main" and drag and drop "haru.moc" in the moc
that is in the script of Inspector tab, and drag and drop files from "texture_00"
in "texture_02" in "Textures".
Finally, set the Position of "GameObject" as X:-7, Y:8, and Z:0.





Open "main" again, and initialize the Live2D model with the Start() function.

  1.         void Start () {
  2.                
  3.                 Live2D.init ();
  4.                
  5.                 live2DModel = Live2DModelUnity.loadModel(mocFile.bytes);
  6.                
  7.                 for(int i = 0; i<textures.Length; i++)
  8.                 {
  9.                         live2DModel.setTexture(i, textures[i]);
  10.                 }
  11.         }
  12.        



Create the OnRenderObject() function and add the command to make settings of the position and the size of the model to display.
In this sample, we made simple settings and the Live2D model is drawn in the right lower with "GameObject" as a pivot.
Then add commands to update the model and start drawing.

  1.         void OnRenderObject()
  2.         {
  3.                 Matrix4x4 m1=Matrix4x4.Ortho(
  4.                         -200.0f, 200.0f,
  5.                          200.0f,-200.0f,
  6.                         -0.5f,0.5f);
  7.                 Matrix4x4 m2 = transform.localToWorldMatrix;
  8.                 Matrix4x4 m3 = m2*m1;
  9.                
  10.                 live2DModel.setMatrix(m3);
  11.                 live2DModel.update();
  12.                 live2DModel.draw();
  13.         }

※:
With the Unity, Update() is prepared as a basic function to invoke each frame, however you better to update Live2D and invoke
from OnRenderObject() to invoke in the best timing. 




The codes will finally become as below;

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using live2d;
  5.  
  6. public class main : MonoBehaviour {
  7.         public TextAsset mocFile;
  8.         public Texture2D[] textures;
  9.  
  10.         private Live2DModelUnity live2DModel;
  11.  
  12.  
  13.         // Use this for initialization
  14.         void Start () {
  15.  
  16.                 Live2D.init ();
  17.  
  18.                 live2DModel = Live2DModelUnity.loadModel(mocFile.bytes);
  19.  
  20.                 for(int i = 0; i<textures.Length; i++)
  21.                 {
  22.                         live2DModel.setTexture(i, textures[i]);
  23.                 }
  24.         }
  25.  
  26.         // Update is called once per frame
  27.         void Update () {
  28.  
  29.         }
  30.  
  31.  
  32.         void OnRenderObject()
  33.         {
  34.                 Matrix4x4 m1=Matrix4x4.Ortho(
  35.                         -200.0f, 200.0f,
  36.                          200.0f,-200.0f,
  37.                         -0.5f,0.5f);
  38.                 Matrix4x4 m2 = transform.localToWorldMatrix;
  39.                 Matrix4x4 m3 = m2*m1;
  40.  
  41.                 live2DModel.setMatrix(m3);
  42.  
  43.                 live2DModel.update();
  44.                 live2DModel.draw();
  45.         }
  46. }

The setting is completed if you click the play button to display the model!


Comments