AnimationEventUtility
Functions to read the animation events baked into a clip.
Read time 2 minutesLast updated 3 hours ago
Definition
- Type: Class
- Namespace: Unity.Animations
- Assembly: UnityEngine.AnimationCoreModule
public static class AnimationEventUtility
Remarks
The clip data stores the time, the function name, and the parameters of each animation event of the source animation clip. Use AnimationEventUtility.GatherAnimationEvents to read the events that a clip timer crossed during a frame. The timer is the one that you pass to ClipSampler.Sample, so the events match the sampled pose.
The following example uses the , , , , , and namespaces. It gathers the animation events that the playback time of a clip passes between two updates. The range can span several loops of a looping clip.
System.Collections.GenericUnity.AnimationsUnity.CollectionsUnity.EntitiesUnity.IntegerTimeUnityEngineAdditional Resources: GatheredAnimationEvent
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;}
Static Methods
Method | Description |
|---|---|
| GatherAnimationEvents | Appends the animation events that a timer crossed during its last update to a list. |