AvatarExtensions
Extension methods for avatars that give access to the rig definition of an avatar.
Read time 3 minutesLast updated 3 hours ago
Definition
- Type: Class
- Namespace: Unity.Animations
- Assembly: UnityEngine.AnimationCoreModule
public static class AvatarExtensions
Remarks
Call AvatarExtensions.ToRigReference to get a reference to the rig definition of an avatar. The rig definition has a bone for each transform of the avatar. Read the data through BlobObjectReference<T>.Value, also from jobs, and pass it by reference to create a ClipSampler or an AnimationStream for the avatar.
The following example plays an animation clip on the hierarchy of an avatar, with the rig definition of the avatar. 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.
Additional Resources: RigDefinition
``<![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(); } }}
Static Methods
Method | Description |
|---|---|
| ToRigReference | Acquires a reference to the rig definition of an avatar. |