AnimationEventInfo is a read-only, non-allocating struct that provides information about an animation event when it fires.
AnimationEventInfo is a ref struct that serves as a read-only view of animation event data when an event is fired during animation playback. Unlike AnimationEvent, AnimationEventInfo is designed for reporting event information in callbacks. It cannot be used for authoring or be stored in fields.
An animation event callback can accept either an AnimationEvent or an AnimationEventInfo parameter. Use AnimationEventInfo to avoid allocating the AnimationEvent object itself when an event fires.
The parameter can access floats, ints, strings, and object references configured on the animation event.
Note: The AnimationEventInfo struct itself doesn't allocate but accessing string properties (functionName and stringParameter) causes string allocation when retrieving from native code. Accessing non-string properties (such as time, floatParameter, intParameter, objectReferenceParameter, isFiredByLegacy, isFiredByAnimator, animatorStateInfo, and animatorClipInfo) causes no allocations.
// AnimationEventInfo example // Non-allocating event callback that reads event parameters
using UnityEngine;
public class Example : MonoBehaviour { // This callback receives AnimationEventInfo instead of AnimationEvent // which avoids allocations when the event fires public void OnAnimationEvent(AnimationEventInfo eventInfo) { Debug.Log($"Event '{eventInfo.functionName}' fired at time {eventInfo.time}"); Debug.Log($"Parameters: int={eventInfo.intParameter}, float={eventInfo.floatParameter}"); Debug.Log($"String parameter: {eventInfo.stringParameter}");
if (eventInfo.objectReferenceParameter != null) { Debug.Log($"Object reference: {eventInfo.objectReferenceParameter.name}"); } } }
A more detailed example shows how to access animator state information:
// Accessing animator state information from AnimationEventInfo using UnityEngine;
public class Example : MonoBehaviour { public void OnAnimatorEvent(AnimationEventInfo eventInfo) { // Check the event source if (eventInfo.isFiredByAnimator) { // Access animator state information AnimatorStateInfo stateInfo = eventInfo.animatorStateInfo; Debug.Log($"State: {stateInfo.fullPathHash}, Time: {stateInfo.normalizedTime}");
// Access clip information AnimatorClipInfo clipInfo = eventInfo.animatorClipInfo; if (clipInfo.clip != null) { Debug.Log($"Clip: {clipInfo.clip.name}, Weight: {clipInfo.weight}"); } } else if (eventInfo.isFiredByLegacy) { // Access legacy animation state AnimationState animState = eventInfo.animationState; if (animState != null) { Debug.Log($"Animation: {animState.name}, Time: {animState.time}"); } } } }
| Property | Description |
|---|---|
| animationState | The animation state that fired this event (Read Only). |
| animatorClipInfo | The animator clip info related to this event (Read Only). |
| animatorStateInfo | The animator state info related to this event (Read Only). |
| floatParameter | Float parameter that is stored in the event (Read Only). |
| functionName | The name of the called function (Read Only). |
| intParameter | Integer parameter that is stored in the event (Read Only). |
| isFiredByAnimator | Returns true if this animation event was fired by an Animator component (Read Only). |
| isFiredByLegacy | Returns true if this animation event was fired by an Animation component (Read Only). |
| objectReferenceParameter | Object reference parameter that is stored in the event (Read Only). |
| stringParameter | String parameter that is stored in the event (Read Only). |
| time | The time at which the event is being fired (Read Only). |