Load models at runtime

[Last updated: 01/30/2020]

This section explains how to initialize and display the model from a script.

Summary

In 2.1, Init,loadModel,setTexture were used to initialize the model, load the model, and set the texture.
3.0 or later versions, LoadAtPath,ToModel can be used to initialize the model, load the model, set the texture, as well as update and draw the model.
The first step is to create a script to load the model from the path.
The loaded model is then used ToModel() to display the model.
This time, the script is written to read from StreamingAssets.

Detail

Until 2.1, after initializing Live2D with Live2D.Init(), the model was loaded with Live2DUnity.loadModel.

The texture was then set as follows, and the model was updated and drawn with ALive2DModel.update() and ALive2DModel.draw().  

In 3.0 and later versions, the process has been completely redesigned. After loading the model using CusbimModel3Json.LoadAtPath, call .ToModel () to display the model.
Prefab is also created and displayed, and drawing is updated without writing .update() or .draw() update drawing from the script.

 

Now we would like to actually initialize and display the model using Cubism 3 SDK for Unity.

First, create a script to load the model from a path.
Create a new C# script and name it “InitModel.cs”.

Write InitModel.cs as follows.

Right-click in the Hierarchy window and click “Create Empty” to create an empty game object.
Then, attach InitModel.cs to the Game Object you created.

Next, add the model to the Assets folder for loading.
In this case, we are loading from StreamingAssets, so select the Assets folder, right-click on the Project window, and create a new folder.
Rename the new folder “StreamingAssets”.
For more information on StreamingAssets, see Unity - Manual: Streaming Assets.

Add the model to StreamingAssets as shown in the image.

Finally, press the play button in Unity, and the model is displayed.
The model can now be initialized and displayed from a script.

There is also an actual example using ToModel().
Please find the link below and please take a look at it as well.

[Live2D GitHub]CubismModel3Json.cs
https://github.com/Live2D/CubismUnityComponents/blob/develop/Assets/Live2D/Cubism/Editor/Importers/CubismModel3JsonImporter.cs
Here, the model's GameObject is generated by ToModel on the hook of an import event in AssetImporter.

[Live2D GitHub]CubismViewer.cs
https://github.com/Live2D/CubismViewer/blob/develop/Assets/Live2D/Cubism/Viewer/CubismViewer.cs
Here, the model file is read from an absolute path, and then the model is generated with ToModel.

 

About the Original Workflow Method

[Added on 01/31/2019]
To make the prefab created by .ToModel() the Original Workflow method, pass true as an argument to CubismModel3Json.ToModel().

© 2010 - 2022 Live2D Inc.