Get User Data Set in ArtMeshes (SDK for Cocos Creator)
Updated: 03/14/2023
This section explains how to get the “UserData” information set in the ArtMesh from the model.
Summary
“UserData” is a feature added in Cubism 3.1 that allows users to add desired metadata to an ArtMesh.
Depending on how the user data is handled, the ArtMesh can be used for a variety of things, such as designating that ArtMesh as a collision detection or applying special shading.
Click here for information on how to set up user data in an ArtMesh.
To retrieve the user data set in the ArtMesh, follow the steps described below.
An example of a concrete description would be as follows.
import { _decorator, Component, Node } from 'cc'; import CubismUserDataTag from '../extensions/Live2DCubismSdkForCocosExtension/static/assets/Framework/UserData/CubismUserDataTag'; const { ccclass, property } = _decorator; @ccclass('UserDataTest') export class UserDataTest extends Component { protected start() { let userDatas = this.getComponentsInChildren(CubismUserDataTag); for (let i = 0; i < userDatas.length; ++i) { let data = userDatas[i]; console.log("id:"+data.node.name + "\n" +"value:"+data.value); } } }
The Prefab of the model with user data is then placed in the Hierarchy.
Attach the UserDataTest component created above to the root of the placed Prefab.
View our tutorial here for more information on importing models.
This completes the setup.
When the Scene is executed in this state, the Console window will output the ID of the ArtMesh to which the user data is set and the string set in the user data.