UserData

Updated: 01/30/2020

Summary

UserData is a function that visualizes the user data set in the model’s ArtMesh in Unity.
User data is a function that allows tags to be assigned as desired to the ArtMesh, which can be used to set the ArtMesh for special processing in the SDK.

Click here for more information on user data.

To use user data in Unity, follow these steps.

  1. Parse .userdata3.json
  2. Set UserData for the ArtMesh

1. Parse .userdata3.json

Parse .userdata3.json using CubismUserData3Json.
Click here for more information on CubismUserData3Json.

var userData3Json = CubismUserData3Json.LoadFrom(jsonString);

The .userdata3.json path can also be obtained from .model3.json using CubismModel3Json.
The obtained path is relative to .model3.json.

var userDataPath = modelJson.FileReferences.UserData;

var jsonString = ((TextAsset)Resources.Load<TextAsset>(userDataPath)).text;

Click here for more information on CubismModel3Json.

Get the ArtMesh data from the parsed .userdata3.json.

var drawableBodies = userData3Json.ToBodyArray(CubismUserDataTargetType.ArtMesh);

2. Set UserData for the ArtMesh

Apply the user data obtained from .userdata3.json to the Prefab ArtMesh.

To add user data information to an ArtMesh, use the CubismUserDataTag.

// Get reference to Prefab ArtMesh
var drawables = cubismModel.Drawables;

for (var i = 0; i < drawables.Length; ++i)
{
    var index = -1;
  
    for (var j = 0; j < drawableBodies.Length; ++j)
    {
        if (drawableBodies[j].Id != drawables[i].Id)
        {
            continue;
        }

        index = j;
        break;
    }

    if (index < 0)
    {
        continue;
    }
    

    // If CubismUserDataTag is already attached, use it again
    var tag = drawables[i].gameObject.GetComponent<CubismUserDataTag>();

    if (tag == null)
    {
        tag = drawables[i].gameObject.AddComponent<CubismUserDataTag>();
    }

    // Set user data to CubismUserDataTag
    tag.Initialize(drawableBodies[index]);
}
Please let us know what you think about this article.