Documentation

Unity Engine

Download Unity

Unity Engine

AnimationClipExtensions

Extension methods for animation clips that give access to the clip data used for sampling.
Read time 2 minutesLast updated 3 hours ago

Definition

public static class AnimationClipExtensions

Remarks

Call AnimationClipExtensions.ToClipSampler to sample an animation clip into an AnimationStream. Call AnimationClipExtensions.ToClipReference to get a reference to the clip data of an animation clip. Use the reference, for example, to read the length of the clip or to store the reference in a component. Read the data through BlobObjectReference<T>.Value, also from jobs.
The following example uses the
Unity.Animations
,
Unity.Collections
,
Unity.Entities
,
Unity.IntegerTime
, and
UnityEngine
namespaces. It builds a rig that has one float channel and creates a ClipSampler from an animation clip. It then samples the clip into an animation stream. The example animates the
intensity
field of the
SampleClipExampleTarget
component. To run the example, add a
MonoBehaviour
named
SampleClipExampleTarget
that has a
float
field named
intensity
, or replace the component and the field with the ones that your animation clip animates.
Additional Resources: ClipSampler

Examples

// This example samples the intensity field that a clip animates at a given time and returns the sampled value.public static float SampleIntensity(AnimationClip animationClip, float time){ // Describe the animated field in a rig, so that the animation stream has a slot for it. using RigBuilder rigBuilder = new RigBuilder(Allocator.Temp); // The empty path means that the animated component is on the root object. The last argument is the // default value. rigBuilder.Add(FloatChannel.Create( "", typeof(SampleClipExampleTarget), nameof(SampleClipExampleTarget.intensity), 0f)); using BlobObjectReference<RigDefinition> rigDefinition = rigBuilder.ToRigDefinition(); // Create a sampler for the clip and the rig, from the main thread. // Keep the sampler between samples of the same clip to sample faster. using ClipSampler sampler = animationClip.ToClipSampler(ref rigDefinition.Value, Allocator.Persistent); // Stop if the sampler isn't created, for example when the animation clip is null. if (!sampler.IsCreated) { return 0f; } // Create the animation stream that receives the sampled values. The stream owns its buffer. AnimationStream stream = AnimationStream.Create(ref rigDefinition.Value, Allocator.Temp); // Sample the clip at the given time. The sample overwrites the content of the stream. sampler.Sample(ref stream, new DiscreteTime(time)); float intensity = stream.GetFloat(0); // Dispose the stream explicitly, because a using declaration can't be passed by reference. stream.Dispose(); // For a clip that goes from 0 to 10 over one second, the sample at 0.5 seconds is 5. return intensity;}

Static Methods

Method

Description

ToClipReferenceAcquires a reference to the sampling data of an animation clip.
ToClipSamplerCreates a ClipSampler that samples an animation clip for a rig.