Documentation

Unity Engine

Download Unity

Unity Engine

GatheredAnimationEvent

An animation event that a clip timer crossed, with the function name, the parameters, and the time of the event.
Read time 3 minutesLast updated 3 hours ago

Definition

public struct GatheredAnimationEvent

Remarks

Pass a
NativeList<GatheredAnimationEvent>
to AnimationEventUtility.GatherAnimationEvents to get the animation events that a timer crossed. The clip data stores the function name and the parameters of each event, so you can read them from jobs and Burst-compiled code. The function name and the string parameter are stored as StringHash values. Compare them to
new StringHash("name")
values that you compute on the main thread. The object reference parameter is an
EntityId
. Use GatheredAnimationEvent.EventIndex to read the complete event from the source animation clip on the main thread.
The following example uses the
System.Collections.Generic
,
Unity.Animations
,
Unity.Collections
,
Unity.Entities
,
Unity.IntegerTime
, and
UnityEngine
namespaces. It gathers the animation events that the playback time of a clip passes between two updates. It then uses the event index of each gathered event to read the function name of the event from the animation clip.
Additional Resources: AnimationEventUtility

Examples

// This example reads the function name of each animation event that the clip time// crosses between two updates.public static List<string> GatherEventFunctionNames( AnimationClip animationClip, float previousTime, float currentTime){ using BlobObjectReference<Clip> clipReference = animationClip.ToClipReference(); // Stop if the animation clip is null or has no clip data. if (!clipReference.IsValid) { return new List<string>(); } AnimationEvent[] clipEvents = animationClip.events; // Gather the events from previousTime (excluded) to currentTime (included), in seconds from the start of the clip. // To step over the end of a looping clip, pass a currentTime that is greater than the length of the clip. // When previousTime is greater than currentTime, the clip plays backward and the events come in descending order. using NativeList<GatheredAnimationEvent> gatheredEvents = new NativeList<GatheredAnimationEvent>(Allocator.Temp); AnimationEventUtility.GatherAnimationEvents( ref clipReference.Value, new DiscreteTime(previousTime), new DiscreteTime(currentTime), gatheredEvents); // The event index of each gathered event is its index in AnimationClip.events. List<string> functionNames = new List<string>(); foreach (GatheredAnimationEvent gatheredEvent in gatheredEvents) { AnimationEvent clipEvent = clipEvents[gatheredEvent.EventIndex]; functionNames.Add(clipEvent.functionName); } // For events at 0.25 and 0.75 seconds in a looping clip of 1 second, a range from 0.6 to 1.3 returns // the 0.75 event, then the 0.25 event of the next loop. return functionNames;}

Fields

Value

Description

EventIndexThe index of the event in the events of the source animation clip.
FloatParameterThe float parameter value that the event stores.
FunctionNameThe hash of the name of the function of the event.
IntParameterThe int parameter value that the event stores.
LoopThe loop count of the clip cycle in which the timer crossed the event, as a TimerState.LoopCount value.
ObjectReferenceParameterThe object reference parameter of the event, or
EntityId.None
if the event has none.
StringParameterThe hash of the string parameter of the event.
TimeThe time of the event on the curves of the clip, in seconds.