Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

AnimationEventInfo

struct in UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

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}"); } } } }

Properties

Property Description
animationStateThe animation state that fired this event (Read Only).
animatorClipInfoThe animator clip info related to this event (Read Only).
animatorStateInfoThe animator state info related to this event (Read Only).
floatParameterFloat parameter that is stored in the event (Read Only).
functionNameThe name of the called function (Read Only).
intParameterInteger parameter that is stored in the event (Read Only).
isFiredByAnimatorReturns true if this animation event was fired by an Animator component (Read Only).
isFiredByLegacyReturns true if this animation event was fired by an Animation component (Read Only).
objectReferenceParameterObject reference parameter that is stored in the event (Read Only).
stringParameterString parameter that is stored in the event (Read Only).
timeThe time at which the event is being fired (Read Only).