Get Events Set in motion3.json

Updated: 11/21/2017

This section describes how to retrieve information about events set in motion3.json from events issued from an automatically generated AnimationClip.

Summary

[UserData] is a function added in 3.1 that allows users to issue events at any point in the motion.
In Unity, events are issued from the AnimationClip that is converted from motion3.json.
Depending on how events are handled, they can be used for various things, such as playing sound in the middle of a motion or changing the display state of a part.

Click here for more information on how to set up events in motion3.json.

The following procedure is used to obtain events issued by motion.

  1. Create a Component to Receive Events
  2. Add Description to CubismMotion3Json
  3. Place Models and Motion in a Scene

Create a Component to Receive Events

Click [Create] in the Project window, then click [C# Script] to create a C# script.
In this example, name it UserDataTest.

Rewrite the content of the UserDataTest you created as follows.

using UnityEngine;

public class UserDataTest : MonoBehaviour
{
	public void UserDataEventListener(string value)
	{
        var currentState = GetComponent<Animator>().GetCurrentAnimatorStateInfo(0);


        Debug.Log("Time : " + (currentState.length / currentState.speed) + "\n" +
            "Value : " + value);
	}
}

Add Description to CubismMotion3Json

Open the CubismMotion3Json class and write the following where the AnimationEvent is created in ToAnimationClip().

                    var animationEvent = new AnimationEvent
                    {
                        time = UserData[i].Time,
                        functionName = "UserDataEventListener",    // Added rows.
                        stringParameter = UserData[i].Value,
                    };

In functionName, set the name of the method that will receive events from the UserDataTest class created in the previous section.

Place Models and Motion in a Scene

Import or re-import motion3.json with events set.

Place the model’s Prefab in the Hierarchy window and attach the UserDataTest component to the root of the Prefab.

Drag and drop the AnimationClip generated from motion3.json into the Prefab placed in the Hierarchy.

This completes the setup.

When a Scene is executed in this state, an event is issued at a specific timing from the playing AnimationClip, and the string set for the event is output in the Console window.

Was this article helpful?
YesNo
Please let us know what you think about this article.