Documentation

Unity Engine

Download Unity

Unity Engine

Create

Creates a RigBuilder with the bones and channels that a collection of Animation Clips animates.
Read time 19 minutesLast updated 3 hours ago

Definition

Create(Transform, AnimationClip[], Allocator)

Creates a RigBuilder with the bones and channels that a collection of Animation Clips animates.
[ExcludeFromBurstCompatTesting("Takes managed Transform and AnimationClips")]public static RigBuilder Create(Transform rootTransform, AnimationClip[] animationClips, Allocator allocator)

Parameters

rootTransform

The transform that becomes the root bone. Character space is the space of its parent.

animationClips

AnimationClip[]
Collection of Animation Clips. If null, the builder is empty.

allocator

Allocator used for RigBuilder. Allocator is a member of the <a href="https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html"&gt;Unity.Collections.Allocator&lt;/a> enumeration.

Returns

Type

Description

RigBuilderA RigBuilder with the bones and channels that the clips animate.

Remarks

Each transform that a clip animates becomes a bone, and so does each of its parents up to
rootTransform
. The identifier of a bone is the StringHash of its path relative to
rootTransform
, so the clips must use paths relative to
rootTransform
. Use RigDefinitionUtility.BindTransforms to find the transform of each bone. Each other property that a clip animates becomes a float, integer, or entity ID channel.
Only Generic clips are supported. If a clip is a Humanoid clip, this method logs a warning once for the clip and ignores the Humanoid curves of the clip.
When the clips animate a transform,
rootTransform
becomes the root bone, at index 0. Character space is the space of the parent of the root bone. If you pass the animated object itself, character space is the space of its parent, and character-space methods such as AnimationStream.GetLocalToCharacterRotation don't return values relative to the character. The root bone is also the transform that places the character in the world, so writing the pose to it moves the character. To make the top bone of the skeleton the root bone instead, use RigBuilder.Create.
The default values of the bones and channels are the values in the hierarchy when you call this method. They include the local transform of
rootTransform
, which is its placement relative to its parent, and any pose or scale that the hierarchy has in the scene. Two copies of a character at different places get rigs with different default values. To use other default values for a bone, such as the pose of the skeleton in the model asset, find the bone with RigBuilder.GetBoneIndex and use RigBuilder.TrySetBoneDefaultTranslation, RigBuilder.TrySetBoneDefaultRotation, and RigBuilder.TrySetBoneDefaultScale.
If several transforms have the same path, the first one in depth-first order becomes the bone. The method logs a warning and skips the others and their children, so that the rig matches the transforms that RigDefinitionUtility.BindTransforms finds.
To create the rig definition, call RigBuilder.ToRigDefinition on the builder. Before that, you can change the builder, for example to add sockets or bones that only your code animates. Dispose the builder when you no longer need it.
Each of the following examples is a
MonoBehaviour
that plays a clip on the hierarchy under its GameObject. It builds a rig definition from the clip once. Each frame, it samples the clip. The first example writes the pose to the transforms with the Burst-compiled function in the second code block. The function isn't part of the API, so copy it into your project. The second example writes the float, integer, and object reference channels to the properties of the components with GenericBindingUtility.
``<![CDATA[using Unity.Burst; using Unity.Collections; using Unity.Mathematics; using UnityEngine;
namespace Unity.Animations.DocumentationExamples { // Writes the pose in a stream to the transform handles that // RigDefinitionUtility.BindTransforms outputs. Entry index of the // handles is the transform of bone index. [BurstCompile] public static class WriteTransformsUtility { // A Burst-compiled static method runs on the main thread, so it // writes the transforms without the cost of scheduling a job. [BurstCompile] public static void WriteTransforms( in AnimationStream stream, in NativeArray<TransformHandle> transforms) { for (int i = 0; i < transforms.Length; ++i) { // The entry of a bone that has no transform is None, and a // destroyed transform is no longer valid. TransformHandle transform = transforms[i]; if (!transform.IsValid()) { continue; }
stream.GetLocalToParentTRS( i, out float3 translation, out quaternion rotation, out float3 scale); transform.SetLocalPositionAndRotation(translation, rotation); transform.localScale = scale; } } } } ]]> ``<![CDATA[using System; using Unity.Collections; using Unity.IntegerTime; using UnityEngine; using UnityEngine.Animations;
namespace Unity.Animations.DocumentationExamples { // This example plays a clip on the properties of the components under the GameObject of this component: float // properties such as the intensity of a light, discrete integer properties, and object references such as the // sprite of a sprite renderer. Assign a clip that animates components under this GameObject, with paths relative // to this GameObject. To also write the transforms, refer to SampleClipToTransformsExample. public class SampleClipToComponentsExample : MonoBehaviour { public AnimationClip animationClip;
BlobObjectReference<RigDefinition> m_RigDefinition; ClipSampler m_Sampler; BoundChannels<float> m_Floats; BoundChannels<int> m_Ints; BoundChannels<EntityId> m_ObjectReferences; float m_Time;
// The properties of one kind, the index of the rig channel of each property, and the values to write. struct BoundChannels<T> : IDisposable where T : unmanaged { public NativeArray<BoundProperty> Properties; public NativeArray<int> ChannelIndices; public NativeArray<T> Values;
public BoundChannels(NativeArray<BoundProperty> properties) { Properties = properties; ChannelIndices = new NativeArray<int>(properties.Length, Allocator.Persistent); Values = new NativeArray<T>(properties.Length, Allocator.Persistent); }
public void Dispose() { // Unbind the properties to release the objects that they reference. if (Properties.IsCreated) { GenericBindingUtility.UnbindProperties(Properties); Properties.Dispose(); } if (ChannelIndices.IsCreated) ChannelIndices.Dispose(); if (Values.IsCreated) Values.Dispose(); } }
void OnEnable() { if (animationClip == null) return;
// Build a rig with a channel for each property that the clip animates: a float channel for a float // property, an int channel for a discrete integer property, and an entity id channel for an object // reference. The default value of a channel is the current value of its property. using RigBuilder rigBuilder = RigBuilder.Create(transform, new[] { animationClip }, Allocator.Temp); m_RigDefinition = rigBuilder.ToRigDefinition(); m_Sampler = animationClip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent);
// Bind each property that the clip animates once. BindProperties sorts the properties by kind, and the // properties of each kind have the order of the bindings of that kind. using NativeArray<GenericBinding> bindings = new NativeArray<GenericBinding>( GenericBindingUtility.GetCurveBindings(animationClip), Allocator.Temp); GenericBindingUtility.BindProperties( gameObject, bindings, out NativeArray<BoundProperty> floatProperties, out NativeArray<BoundProperty> intProperties, out NativeArray<BoundProperty> objectReferenceProperties, Allocator.Persistent); m_Floats = new BoundChannels<float>(floatProperties); m_Ints = new BoundChannels<int>(intProperties); m_ObjectReferences = new BoundChannels<EntityId>(objectReferenceProperties);
// Find the rig channel of each binding. // The channel index is the index to read in the stream, or -1 if the rig has no such channel. ref BindingSet rigBindings = ref m_RigDefinition.Value.Bindings; int floatCount = 0; int intCount = 0; int objectReferenceCount = 0; foreach (GenericBinding binding in bindings) { if (binding.isDiscrete) { m_Ints.ChannelIndices[intCount++] = BindingUtility.FindBindingIndex(ref rigBindings.IntBindings, binding); } else if (binding.isObjectReference) { m_ObjectReferences.ChannelIndices[objectReferenceCount++] = BindingUtility.FindBindingIndex(ref rigBindings.EntityIdBindings, binding); } else { m_Floats.ChannelIndices[floatCount++] = BindingUtility.FindBindingIndex(ref rigBindings.FloatBindings, binding); } }
// Start from the current values, so that a property without a channel keeps its value. GenericBindingUtility.GetValues(m_Floats.Properties, m_Floats.Values); GenericBindingUtility.GetValues(m_Ints.Properties, m_Ints.Values); GenericBindingUtility.GetValues(m_ObjectReferences.Properties, m_ObjectReferences.Values); m_Time = 0f; }
void OnDisable() { m_Floats.Dispose(); m_Ints.Dispose(); m_ObjectReferences.Dispose();
// Dispose the sampler before the rig definition that it uses. if (m_Sampler.IsCreated) m_Sampler.Dispose(); m_RigDefinition.Dispose(); }
void Update() { m_Time += Time.deltaTime; ApplyClipValues(m_Time); }
// Samples the clip at a time and writes the values to the properties of the components. public void ApplyClipValues(float time) { // The sampler isn't created when the component has no clip. if (!m_Sampler.IsCreated) return;
AnimationStream stream = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); m_Sampler.Sample(ref stream, new DiscreteTime(time));
// Gather the value of each property from its channel, then write the values of each kind in one call. for (int i = 0; i < m_Floats.Values.Length; ++i) { int channelIndex = m_Floats.ChannelIndices[i]; if (channelIndex >= 0) m_Floats.Values[i] = stream.GetFloat(channelIndex); } GenericBindingUtility.SetValues(m_Floats.Properties, m_Floats.Values);
for (int i = 0; i < m_Ints.Values.Length; ++i) { int channelIndex = m_Ints.ChannelIndices[i]; if (channelIndex >= 0) m_Ints.Values[i] = stream.GetInt(channelIndex); } GenericBindingUtility.SetValues(m_Ints.Properties, m_Ints.Values);
// An object reference channel holds the EntityId of the object. for (int i = 0; i < m_ObjectReferences.Values.Length; ++i) { int channelIndex = m_ObjectReferences.ChannelIndices[i]; if (channelIndex >= 0) m_ObjectReferences.Values[i] = stream.GetEntityId(channelIndex); } GenericBindingUtility.SetValues(m_ObjectReferences.Properties, m_ObjectReferences.Values);
// Dispose the stream explicitly, because a using declaration can't be passed by reference. stream.Dispose(); } } } ]]>

Examples

using Unity.Collections;using Unity.IntegerTime;using UnityEngine;namespace Unity.Animations.DocumentationExamples{ // This example plays a clip on the hierarchy under the GameObject of this component. Assign a clip that animates // transforms under this GameObject, with paths relative to this GameObject. public class SampleClipToTransformsExample : MonoBehaviour { public AnimationClip animationClip; BlobObjectReference<RigDefinition> m_RigDefinition; NativeArray<TransformHandle> m_BoneTransforms; ClipSampler m_Sampler; float m_Time; void OnEnable() { if (animationClip == null) return; // Build a rig with a bone for each transform that the clip animates, and for each parent of those // transforms up to the transform of this component. using RigBuilder rigBuilder = RigBuilder.Create(transform, new[] { animationClip }, Allocator.Temp); m_RigDefinition = rigBuilder.ToRigDefinition(); // Find the transform of each bone. The array has one entry for each bone, indexed by bone index. The // entry of a bone that has no transform in the hierarchy is invalid. RigDefinitionUtility.BindTransforms( ref m_RigDefinition.Value, transform, Allocator.Persistent, out m_BoneTransforms); // Create the sampler once, and keep it between frames to sample faster. m_Sampler = animationClip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); m_Time = 0f; } void OnDisable() { // Dispose the sampler before the rig definition that it uses. if (m_Sampler.IsCreated) m_Sampler.Dispose(); if (m_BoneTransforms.IsCreated) m_BoneTransforms.Dispose(); m_RigDefinition.Dispose(); } void Update() { m_Time += Time.deltaTime; ApplyClipPose(m_Time); } // Samples the clip at a time and writes the pose to the transforms. public void ApplyClipPose(float time) { // The sampler isn't created when the component has no clip. if (!m_Sampler.IsCreated) return; // The stream starts with the default values of the rig, which are the local transforms of the hierarchy // when the rig definition was created. The sample overwrites only the channels that the clip animates. AnimationStream stream = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); // When the clip has Loop Time enabled, the time repeats. Otherwise, the pose stays at the end of the clip. m_Sampler.Sample(ref stream, new DiscreteTime(time)); // Write the pose to the transforms with a Burst-compiled function. The function isn't part of the API, // so copy it into your project. WriteTransformsUtility.WriteTransforms(stream, m_BoneTransforms); // Dispose the stream explicitly, because a using declaration can't be passed by reference. stream.Dispose(); } }}

Create(Transform, AnimationClip[], Transform, Allocator)

Creates a RigBuilder from a list of Animation Clips, with the root motion from a transform of the hierarchy.
[ExcludeFromBurstCompatTesting("Takes managed Transform and AnimationClips")]public static RigBuilder Create(Transform rootTransform, AnimationClip[] animationClips, Transform rootMotionTransform, Allocator allocator)

Parameters

rootTransform

The transform that becomes the root bone. Character space is the space of its parent.

animationClips

AnimationClip[]
Collection of Animation Clips. If null, the builder is empty.

rootMotionTransform

The transform whose motion gives the root motion. It must be
rootTransform
or one of its children. If null, the root motion bone is the root bone, as with RigBuilder.Create.

allocator

Allocator used for RigBuilder. Allocator is a member of the <a href="https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html"&gt;Unity.Collections.Allocator&lt;/a> enumeration.

Returns

Type

Description

RigBuilderReturns a RigBuilder with channels extracted from the Animation Clips.

Remarks

The method collects the bones and channels as RigBuilder.Create does, and sets RigBuilder.RootMotionBoneId to the bone of
rootMotionTransform
. The rig has a bone for
rootMotionTransform
and each of its parents up to
rootTransform
, even if no clip animates them.
Use this method when the transform that carries the motion of the character isn't
rootTransform
, for example a hips bone under a root that clips don't animate. ClipSampler.SampleRootMotion then returns the motion of this transform in character space when a clip has no root motion curves, and ClipSampler.SampleExtractingRootMotion removes the root motion from this transform in the pose.

Create(Transform, Transform, AnimationClip[], Allocator)

Creates a RigBuilder with the bones and channels that a collection of Animation Clips animates, with the top bone of the skeleton as the root bone.
[ExcludeFromBurstCompatTesting("Takes managed Transform and AnimationClips")]public static RigBuilder Create(Transform rootTransform, Transform skeletonRootTransform, AnimationClip[] animationClips, Allocator allocator)

Parameters

rootTransform

The animated object. The clips use paths relative to it.

skeletonRootTransform

The transform that becomes the root bone. It must be
rootTransform
or one of its children.

animationClips

AnimationClip[]
Collection of Animation Clips. If null, the builder is empty.

allocator

Allocator used for RigBuilder. Allocator is a member of the <a href="https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html"&gt;Unity.Collections.Allocator&lt;/a> enumeration.

Returns

Type

Description

RigBuilderA RigBuilder with the bones and channels that the clips animate.

Remarks

The method collects the bones and channels as RigBuilder.Create does, but only
skeletonRootTransform
and its children become bones, and
skeletonRootTransform
becomes the root bone. The method ignores the curves of the clips that animate other transforms, such as
rootTransform
.
Use this method when the clips animate a skeleton under the animated object, such as a model with an armature. Character space is then the space of the parent of
skeletonRootTransform
. The rig has no bone for the animated object, so writing the pose to the transforms doesn't move the character.
The identifier of each bone is still the StringHash of its path relative to
rootTransform
, as in the clips. To find the transform of each bone, pass
rootTransform
to RigDefinitionUtility.BindTransforms.
The following example is a
MonoBehaviour
that cross-fades two clips on the skeleton under its GameObject. The last code block is the Burst-compiled function that writes the pose to the transforms. The function isn't part of the API, so copy it into your project.
``<![CDATA[using Unity.Burst; using Unity.Collections; using Unity.Mathematics; using UnityEngine;
namespace Unity.Animations.DocumentationExamples { // Writes the pose in a stream to the transform handles that // RigDefinitionUtility.BindTransforms outputs. Entry index of the // handles is the transform of bone index. [BurstCompile] public static class WriteTransformsUtility { // A Burst-compiled static method runs on the main thread, so it // writes the transforms without the cost of scheduling a job. [BurstCompile] public static void WriteTransforms( in AnimationStream stream, in NativeArray<TransformHandle> transforms) { for (int i = 0; i < transforms.Length; ++i) { // The entry of a bone that has no transform is None, and a // destroyed transform is no longer valid. TransformHandle transform = transforms[i]; if (!transform.IsValid()) { continue; }
stream.GetLocalToParentTRS( i, out float3 translation, out quaternion rotation, out float3 scale); transform.SetLocalPositionAndRotation(translation, rotation); transform.localScale = scale; } } } } ]]>

Examples

using Unity.Collections;using Unity.IntegerTime;using UnityEngine;namespace Unity.Animations.DocumentationExamples{ // This example cross-fades from one clip to another with an easing curve. A linear fade changes the pose at a // constant rate, which can look abrupt where it starts and ends. SmoothStep starts and ends slowly. Assign two // clips that animate the transforms under the GameObject of this component, with paths relative to this // GameObject. Assign the skeleton root, the top bone of the skeleton, a direct child of this GameObject. Set when // the transition starts and how long it lasts, in seconds. public class EaseCrossFadeExample : MonoBehaviour { public AnimationClip fromClip; public AnimationClip toClip; public Transform skeletonRootTransform; public float transitionStartTime; public float transitionDuration = 0.3f; BlobObjectReference<RigDefinition> m_RigDefinition; NativeArray<TransformHandle> m_BoneTransforms; ClipSampler m_FromSampler; ClipSampler m_ToSampler; float m_Time; void OnEnable() { if (fromClip == null || toClip == null || skeletonRootTransform == null) return; // Build one rig from both clips, so that both samplers write to streams of the same rig. // The reference keeps the rig definition alive until you dispose it. using RigBuilder rigBuilder = RigBuilder.Create( transform, skeletonRootTransform, new[] { fromClip, toClip }, Allocator.Temp); m_RigDefinition = rigBuilder.ToRigDefinition(); RigDefinitionUtility.BindTransforms( ref m_RigDefinition.Value, transform, Allocator.Persistent, out m_BoneTransforms); m_FromSampler = fromClip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); m_ToSampler = toClip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); m_Time = 0f; } void OnDisable() { // Dispose the samplers before the rig definition that they use. if (m_FromSampler.IsCreated) m_FromSampler.Dispose(); if (m_ToSampler.IsCreated) m_ToSampler.Dispose(); if (m_BoneTransforms.IsCreated) m_BoneTransforms.Dispose(); m_RigDefinition.Dispose(); } void Update() { m_Time += Time.deltaTime; ApplyPose(m_Time); } // Samples both clips at a time, blends them with the easing curve, and writes the pose to the transforms. public void ApplyPose(float time) { if (!m_FromSampler.IsCreated) return; AnimationStream fromPose = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); AnimationStream toPose = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); AnimationStream output = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); m_FromSampler.Sample(ref fromPose, new DiscreteTime(time)); m_ToSampler.Sample(ref toPose, new DiscreteTime(time)); // Evaluate clamps its input to the range 0 to 1, so the fade holds the first pose before the transition // and the second pose after it. A transition without a duration switches at its start time. float progress = transitionDuration > 0f ? (time - transitionStartTime) / transitionDuration : (time >= transitionStartTime ? 1f : 0f); float weight = Interpolator.Evaluate(Interpolator.FunctionType.SmoothStep, progress); // A weight of 0 gives fromPose, and a weight of 1 gives toPose. output.Blend(fromPose, toPose, weight); // Write the pose to the transforms with a Burst-compiled function. The function isn't part of the API, // so copy it into your project. WriteTransformsUtility.WriteTransforms(output, m_BoneTransforms); // Dispose the streams explicitly, because a using declaration can't be passed by reference. output.Dispose(); toPose.Dispose(); fromPose.Dispose(); } }}

Create(Transform, Transform, AnimationClip[], Transform, Allocator)

Creates a RigBuilder with the bones and channels that a collection of Animation Clips animates, with the top bone of the skeleton as the root bone and the root motion from a transform of the skeleton.
[ExcludeFromBurstCompatTesting("Takes managed Transform and AnimationClips")]public static RigBuilder Create(Transform rootTransform, Transform skeletonRootTransform, AnimationClip[] animationClips, Transform rootMotionTransform, Allocator allocator)

Parameters

rootTransform

The animated object. The clips use paths relative to it.

skeletonRootTransform

The transform that becomes the root bone. It must be
rootTransform
or one of its children.

animationClips

AnimationClip[]
Collection of Animation Clips. If null, the builder is empty.

rootMotionTransform

The transform whose motion gives the root motion. It must be
skeletonRootTransform
or one of its children. If null, the root motion bone is the root bone.

allocator

Allocator used for RigBuilder. Allocator is a member of the <a href="https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html"&gt;Unity.Collections.Allocator&lt;/a> enumeration.

Returns

Type

Description

RigBuilderA RigBuilder with the bones and channels that the clips animate.

Remarks

The method collects the bones and channels as RigBuilder.Create does, and sets the root motion bone as RigBuilder.Create does.

Create(Avatar, Allocator)

Create RigBuilder from an Avatar asset.
[ExcludeFromBurstCompatTesting("Takes managed Avatar")]public static RigBuilder Create(Avatar avatar, Allocator allocator)

Parameters

avatar

Avatar asset

allocator

Allocator used for RigBuilder. Allocator is a member of the <a href="https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html"&gt;Unity.Collections.Allocator&lt;/a> enumeration.

Returns

Type

Description

RigBuilderReturns a RigBuilder with channels extracted from the Avatar.

Remarks

Use this method to change the rig of an Avatar, for example to add sockets. To use the rig of an Avatar as it is, read the rig definition that the Avatar stores with AvatarExtensions.ToRigReference instead of building a new one.
Only Generic Avatars are supported. If
avatar
is a Humanoid Avatar, this method logs a warning once for the Avatar and ignores the Humanoid mapping.
If the Avatar is a Generic Avatar with a Root node, set in the Rig tab of the Model Import Settings, the method sets that bone as the root motion bone of the builder, in RigBuilder.RootMotionBoneId.
The following example is a
MonoBehaviour
that plays a clip on the hierarchy under its GameObject with the rig definition that an Avatar stores. The last code block is the Burst-compiled function that writes the pose to the transforms. The function isn't part of the API, so copy it into your project.
``<![CDATA[using Unity.Burst; using Unity.Collections; using Unity.Mathematics; using UnityEngine;
namespace Unity.Animations.DocumentationExamples { // Writes the pose in a stream to the transform handles that // RigDefinitionUtility.BindTransforms outputs. Entry index of the // handles is the transform of bone index. [BurstCompile] public static class WriteTransformsUtility { // A Burst-compiled static method runs on the main thread, so it // writes the transforms without the cost of scheduling a job. [BurstCompile] public static void WriteTransforms( in AnimationStream stream, in NativeArray<TransformHandle> transforms) { for (int i = 0; i < transforms.Length; ++i) { // The entry of a bone that has no transform is None, and a // destroyed transform is no longer valid. TransformHandle transform = transforms[i]; if (!transform.IsValid()) { continue; }
stream.GetLocalToParentTRS( i, out float3 translation, out quaternion rotation, out float3 scale); transform.SetLocalPositionAndRotation(translation, rotation); transform.localScale = scale; } } } } ]]>

Examples

using Unity.Collections;using Unity.IntegerTime;using UnityEngine;namespace Unity.Animations.DocumentationExamples{ // This example plays a clip on the hierarchy under the GameObject of this component, with the rig definition that // an Avatar stores. Assign the Avatar of this hierarchy, and a clip that animates transforms under this GameObject. public class SampleClipWithAvatarExample : MonoBehaviour { public Avatar avatar; public AnimationClip animationClip; BlobObjectReference<RigDefinition> m_RigReference; NativeArray<TransformHandle> m_BoneTransforms; ClipSampler m_Sampler; float m_Time; void OnEnable() { if (avatar == null || animationClip == null) return; // An Avatar stores a rig definition with a bone for each transform of the Avatar. The reference keeps the // rig definition alive until you dispose it, even if the Avatar is destroyed. m_RigReference = avatar.ToRigReference(); if (!m_RigReference.IsValid) { m_RigReference.Dispose(); m_RigReference = default; return; } // Find the transform of each bone. The identifier of a bone of an Avatar is the StringHash of its path // relative to the root of the Avatar, so pass the transform that the Avatar was created from. RigDefinitionUtility.BindTransforms( ref m_RigReference.Value, transform, Allocator.Persistent, out m_BoneTransforms); // Create the sampler once, and keep it between frames to sample faster. m_Sampler = animationClip.ToClipSampler(ref m_RigReference.Value, Allocator.Persistent); m_Time = 0f; } void OnDisable() { // Dispose the sampler before the reference that keeps the rig definition alive. if (m_Sampler.IsCreated) m_Sampler.Dispose(); if (m_BoneTransforms.IsCreated) m_BoneTransforms.Dispose(); m_RigReference.Dispose(); } void Update() { m_Time += Time.deltaTime; ApplyClipPose(m_Time); } // Samples the clip at a time and writes the pose to the transforms. public void ApplyClipPose(float time) { // The sampler isn't created when the component has no Avatar or no clip. if (!m_Sampler.IsCreated) return; // The stream starts with the default values of the rig, which are the local transforms of the hierarchy // when the Avatar was created. The sample overwrites only the channels that the clip animates. AnimationStream stream = AnimationStream.Create(ref m_RigReference.Value, Allocator.Temp); // When the clip has Loop Time enabled, the time repeats. Otherwise, the pose stays at the end of the clip. m_Sampler.Sample(ref stream, new DiscreteTime(time)); // Write the pose to the transforms with a Burst-compiled function. The function isn't part of the API, // so copy it into your project. WriteTransformsUtility.WriteTransforms(stream, m_BoneTransforms); // Dispose the stream explicitly, because a using declaration can't be passed by reference. stream.Dispose(); } }}