# AnimationClip

> Provides an asset that assigns animation curves to animatable properties.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.AnimationModule
* **Inherits from:** [Motion](/engine/6000.5/script-reference/unityengine/motion.md)

```csharp
public sealed class AnimationClip : Motion
```

## Remarks

The [Animation](/engine/6000.5/script-reference/unityengine/animation.md) component and the [Animator](/engine/6000.5/script-reference/unityengine/animator.md) component use the [AnimationClip](/engine/6000.5/script-reference/unityengine/animationclip.md) asset to animate [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md) and [Component](/engine/6000.5/script-reference/unityengine/component.md) properties.

**Supported Animatable Types**

When you create an [AnimationClip](/engine/6000.5/script-reference/unityengine/animationclip.md) asset with an [Animation](/engine/6000.5/script-reference/unityengine/animation.md) component, you must set `legacy` to true. Use the [Animation](/engine/6000.5/script-reference/unityengine/animation.md) component to animate the following public or serialized properties for a [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md) or [Component](/engine/6000.5/script-reference/unityengine/component.md):

* float values
* boolean values (casted to float)
* integer values (casted to float)
* discrete integer values (using the attribute [DiscreteEvaluationAttribute](/engine/6000.5/script-reference/unityengine/animations/discreteevaluationattribute.md) and reinterpreted to float)

For `legacy` clips, use the [AnimationClip.SetCurve](/engine/6000.5/script-reference/unityengine/animationclip/setcurve.md) method to assign new curves in the Editor and at runtime.

When you create an [AnimationClip](/engine/6000.5/script-reference/unityengine/animationclip.md) asset for an [Animator](/engine/6000.5/script-reference/unityengine/animator.md) component, you can animate the following public or serialized properties:

* float values
* boolean values (cast as float)
* integer values (cast as float)
* discrete integer values (use the [DiscreteEvaluationAttribute](/engine/6000.5/script-reference/unityengine/animations/discreteevaluationattribute.md) attribute reinterpreted as a float)
* [Object](/engine/6000.5/script-reference/unityengine/object.md) reference values (saved as an [ObjectReferenceKeyframe](/engine/6000.5/script-reference/unityeditor/objectreferencekeyframe.md))

**Curve creation**

In the Editor, it is recommended that you use the [AnimationUtility.SetEditorCurve](/engine/6000.5/script-reference/unityeditor/animationutility/seteditorcurve.md) and [AnimationUtility.SetEditorCurves](/engine/6000.5/script-reference/unityeditor/animationutility/seteditorcurves.md) methods to assign one or many [AnimationCurve](/engine/6000.5/script-reference/unityengine/animationcurve.md) objects for each float property and each boolean, integer, or discrete integer reinterpreted as a float.

In the Editor and at runtime, you can use the [AnimationClip.SetCurve](/engine/6000.5/script-reference/unityengine/animationclip/setcurve.md) method to assign an [AnimationCurve](/engine/6000.5/script-reference/unityengine/animationcurve.md) to an animation clip. You can also modify an animation clip at runtime but this modification is only recognized by the [Animation](/engine/6000.5/script-reference/unityengine/animation.md) component. The [Animator](/engine/6000.5/script-reference/unityengine/animator.md) component requires that clips are compiled to an optimized representation and this compilation process is not available at runtime.

Use the [AnimationUtility.SetObjectReferenceCurve](/engine/6000.5/script-reference/unityeditor/animationutility/setobjectreferencecurve.md) and [AnimationUtility.SetObjectReferenceCurves](/engine/6000.5/script-reference/unityeditor/animationutility/setobjectreferencecurves.md) methods to create and assign new [ObjectReferenceKeyframe](/engine/6000.5/script-reference/unityeditor/objectreferencekeyframe.md) arrays of [Object](/engine/6000.5/script-reference/unityengine/object.md) reference properties. **Note:** This is only supported by the [Animator](/engine/6000.5/script-reference/unityengine/animator.md) component.

**Curve query**

Use the [AnimationUtility.GetEditorCurve](/engine/6000.5/script-reference/unityeditor/animationutility/geteditorcurve.md) method to retrieve an [AnimationCurve](/engine/6000.5/script-reference/unityengine/animationcurve.md) for a float property.

Use the [AnimationUtility.GetObjectReferenceCurve](/engine/6000.5/script-reference/unityeditor/animationutility/getobjectreferencecurve.md) method to retrieve an [ObjectReferenceKeyframe](/engine/6000.5/script-reference/unityeditor/objectreferencekeyframe.md) array for an [Object](/engine/6000.5/script-reference/unityengine/object.md) reference property. **Note:** This is only supported by the [Animator](/engine/6000.5/script-reference/unityengine/animator.md) component.

**Animation Events management**

In the editor, use the [AnimationUtility.SetAnimationEvents](/engine/6000.5/script-reference/unityeditor/animationutility/setanimationevents.md) method to set or replace the [AnimationEvent](/engine/6000.5/script-reference/unityengine/animationevent.md) array for the [AnimationClip](/engine/6000.5/script-reference/unityengine/animationclip.md). Use the [AnimationUtility.GetAnimationEvents](/engine/6000.5/script-reference/unityeditor/animationutility/getanimationevents.md) method to retrieve the [AnimationEvent](/engine/6000.5/script-reference/unityengine/animationevent.md) array from the [AnimationClip](/engine/6000.5/script-reference/unityengine/animationclip.md).

Additional Resources: [Animation](/engine/6000.5/script-reference/unityengine/animation.md) [Animator](/engine/6000.5/script-reference/unityengine/animator.md) [AnimationCurve](/engine/6000.5/script-reference/unityengine/animationcurve.md) [ObjectReferenceKeyframe](/engine/6000.5/script-reference/unityeditor/objectreferencekeyframe.md) [AnimationEvent](/engine/6000.5/script-reference/unityengine/animationevent.md)

## Examples

```csharp
// Create a new MonoBehaviour in your project and paste in the following code.

using UnityEngine;

// The example creates a new MonoBehaviour that is used alongside a Camera component.
// At runtime, the component will create a procedural clip to animate the Camera field of view
// property and automatically play that clip on an Animation component.
[RequireComponent(typeof(Camera))]
public class ProceduralFieldOfViewAnimation : MonoBehaviour
{
    void OnEnable()
    {
        AnimationClip clip = new AnimationClip();
        AnimationCurve curve = AnimationCurve.Linear(0.0f, 60.0f, 10.0f, 90.0f);
        clip.SetCurve("", typeof(Camera), "field of view", curve);
        clip.legacy = true;

        if (!TryGetComponent<Animation>(out var animation))
            animation = gameObject.AddComponent<Animation>();

        animation.AddClip(clip, "animfov");
        animation.Play("animfov");
    }
}
```

```csharp
using UnityEditor;
using UnityEngine;

static class AnimationClipWithAnimationCurvesExample
{
    // This example creates an AnimationClip with a single frame that captures the pose of a GameObject hierarchy.
    // The clip is saved as an asset in the project.
    [MenuItem("Example/Create Animation Clip Pose From GameObject")]
    static void CreateAnimationClipPoseFromGameObject()
    {
        var selectedGameObject = Selection.activeGameObject;
        if (selectedGameObject == null)
        {
            Debug.LogError("Please select a GameObject to create a clip for.");
            return;
        }

        AnimationClip clip = new AnimationClip();

        var transforms = selectedGameObject.GetComponentsInChildren<Transform>();
        var numberOfCurves = transforms.Length * 10; // 3 for position, 4 for rotation, 3 for scale

        var bindings = new EditorCurveBinding[numberOfCurves];
        var curves = new AnimationCurve[numberOfCurves];

        for (int i = 0; i < transforms.Length; ++i)
        {
            var startIndex = i * 10;

            var transform = transforms[i];
            var path = AnimationUtility.CalculateTransformPath(transform, selectedGameObject.transform);

            var index = startIndex;
            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.x");
            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.y");
            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.z");

            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalRotation.x");
            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalRotation.y");
            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalRotation.z");
            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalRotation.w");

            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalScale.x");
            bindings[index++] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalScale.y");
            bindings[index] = EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalScale.z");

            transform.GetLocalPositionAndRotation(out var localPosition, out var localRotation);
            var localScale = transform.localScale;

            index = startIndex;
            curves[index++] = AnimationCurve.Constant(0f, 1f, localPosition.x);
            curves[index++] = AnimationCurve.Constant(0f, 1f, localPosition.y);
            curves[index++] = AnimationCurve.Constant(0f, 1f, localPosition.z);

            curves[index++] = AnimationCurve.Constant(0f, 1f, localRotation.x);
            curves[index++] = AnimationCurve.Constant(0f, 1f, localRotation.y);
            curves[index++] = AnimationCurve.Constant(0f, 1f, localRotation.z);
            curves[index++] = AnimationCurve.Constant(0f, 1f, localRotation.w);

            curves[index++] = AnimationCurve.Constant(0f, 1f, localScale.x);
            curves[index++] = AnimationCurve.Constant(0f, 1f, localScale.y);
            curves[index] = AnimationCurve.Constant(0f, 1f, localScale.z);
        }

        AnimationUtility.SetEditorCurves(clip, bindings, curves);

        AssetDatabase.CreateAsset(clip, AssetDatabase.GenerateUniqueAssetPath($"Assets/{selectedGameObject.name}-Pose.anim"));
    }
}
```

```csharp
using UnityEditor;
using UnityEngine;

static class AnimationClipWithObjectReferenceKeyframesExample
{
    // This example creates an AnimationClip of a sequence of sprites selected
    // in the project view. The clip is saved as an asset in the project.
    [MenuItem("Example/Create Flip Book Animation Clip From Sprites")]
    static void CreateFlipBookAnimationClipFromSprites()
    {
        var selectedSprites = Selection.GetFiltered<Sprite>(SelectionMode.Unfiltered);
        if (selectedSprites == null || selectedSprites.Length == 0)
        {
            Debug.LogError("Please select sprites in the project view to create a clip for.");
            return;
        }

        AnimationClip clip = new AnimationClip();

        var spriteCurve = new ObjectReferenceKeyframe[selectedSprites.Length];
        for (int i = 0; i < selectedSprites.Length; ++i)
        {
            var sprite = selectedSprites[i];
            spriteCurve[i] = new ObjectReferenceKeyframe
            {
                time = i/clip.frameRate,
                value = sprite
            };
        }

        var spriteBinding = EditorCurveBinding.PPtrCurve("", typeof(SpriteRenderer), "m_Sprite");
        AnimationUtility.SetObjectReferenceCurve(clip, spriteBinding, spriteCurve);

        AssetDatabase.CreateAsset(clip, AssetDatabase.GenerateUniqueAssetPath($"Assets/FlipBookClip.anim"));
    }
}
```

## Constructors

| Constructor                                                                          | Description                   |
| ------------------------------------------------------------------------------------ | ----------------------------- |
| [AnimationClip()](/engine/6000.5/script-reference/unityengine/animationclip/ctor.md) | Creates a new animation clip. |

## Properties

| Property                                                                                                        | Description                                                                                                    |
| --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| [empty](/engine/6000.5/script-reference/unityengine/animationclip/empty.md)                                     | Returns true if the animation clip has no curves and no events.                                                |
| [events](/engine/6000.5/script-reference/unityengine/animationclip/events.md)                                   | Animation Events for this animation clip.                                                                      |
| [frameRate](/engine/6000.5/script-reference/unityengine/animationclip/framerate.md)                             | Frame rate at which keyframes are sampled. (Read Only)                                                         |
| [hasGenericRootTransform](/engine/6000.5/script-reference/unityengine/animationclip/hasgenericroottransform.md) | Returns true if the Animation has animation on the root transform.                                             |
| [hasMotionCurves](/engine/6000.5/script-reference/unityengine/animationclip/hasmotioncurves.md)                 | Returns true if the AnimationClip has root motion curves.                                                      |
| [hasMotionFloatCurves](/engine/6000.5/script-reference/unityengine/animationclip/hasmotionfloatcurves.md)       | Returns true if the AnimationClip has editor curves for its root motion.                                       |
| [hasRootCurves](/engine/6000.5/script-reference/unityengine/animationclip/hasrootcurves.md)                     | Returns true if the AnimationClip has root Curves.                                                             |
| [humanMotion](/engine/6000.5/script-reference/unityengine/animationclip/humanmotion.md)                         | Returns true if the animation contains curve that drives a humanoid rig.                                       |
| [legacy](/engine/6000.5/script-reference/unityengine/animationclip/legacy.md)                                   | Set to true if the AnimationClip will be used with the Legacy Animation component ( instead of the Animator ). |
| [length](/engine/6000.5/script-reference/unityengine/animationclip/length.md)                                   | Animation length in seconds. (Read Only)                                                                       |
| [localBounds](/engine/6000.5/script-reference/unityengine/animationclip/localbounds.md)                         | AABB of this Animation Clip in local space of Animation component that it is attached too.                     |
| [wrapMode](/engine/6000.5/script-reference/unityengine/animationclip/wrapmode.md)                               | Sets the default wrap mode used in the animation state.                                                        |

## Methods

| Method                                                                                                                | Description                                                       |
| --------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| [AddEvent](/engine/6000.5/script-reference/unityengine/animationclip/addevent.md)                                     | Adds an animation event to the clip.                              |
| [ClearCurves](/engine/6000.5/script-reference/unityengine/animationclip/clearcurves.md)                               | Clears all curves from the clip.                                  |
| [EnsureQuaternionContinuity](/engine/6000.5/script-reference/unityengine/animationclip/ensurequaternioncontinuity.md) | Realigns quaternion keys to ensure shortest interpolation paths.  |
| [SampleAnimation](/engine/6000.5/script-reference/unityengine/animationclip/sampleanimation.md)                       | Samples an animation at a given time for any animated properties. |
| [SetCurve](/engine/6000.5/script-reference/unityengine/animationclip/setcurve.md)                                     | Assigns the curve to animate a specific property.                 |

## Inheritance

**Inherited Members:**

* [Object.GetEntityId()](/engine/6000.5/script-reference/unityengine/object/getentityid.md)
* [Object.GetHashCode()](/engine/6000.5/script-reference/unityengine/object/gethashcode.md)
* [Object.InstantiateAsync\<T>(T)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Transform, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, Vector3, Quaternion, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Vector3, Quaternion, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Vector3, Quaternion, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>, InstantiateParameters, CancellationToken)](/engine/6000.5/script-reference/unityengine/object/instantiateasync.md)
* [Object.Instantiate(Object, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-vector3-quaternion\))
* [Object.Instantiate(Object, Vector3, Quaternion, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-vector3-quaternion-transform\))
* [Object.Instantiate(Object)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object\))
* [Object.Instantiate(Object, Scene)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-scene\))
* [Object.Instantiate\<T>(T, InstantiateParameters)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion, InstantiateParameters)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate(Object, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-transform\))
* [Object.Instantiate(Object, Transform, bool)](/engine/6000.5/script-reference/unityengine/object/instantiate.md#instantiate\(object-transform-bool\))
* [Object.Instantiate\<T>(T)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Transform)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Transform, bool)](/engine/6000.5/script-reference/unityengine/object/instantiate.md)
* [Object.Destroy(Object, float)](/engine/6000.5/script-reference/unityengine/object/destroy.md)
* [Object.DestroyImmediate(Object, bool)](/engine/6000.5/script-reference/unityengine/object/destroyimmediate.md)
* [Object.FindObjectsByType(Type)](/engine/6000.5/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytype\(type\))
* [Object.FindObjectsByType(Type, FindObjectsInactive)](/engine/6000.5/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytype\(type-findobjectsinactive\))
* [Object.DontDestroyOnLoad(Object)](/engine/6000.5/script-reference/unityengine/object/dontdestroyonload.md)
* [Object.FindAnyObjectByType\<T>()](/engine/6000.5/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytypet\(\))
* [Object.FindAnyObjectByType\<T>(FindObjectsInactive)](/engine/6000.5/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytypet\(findobjectsinactive\))
* [Object.FindObjectsByType\<T>()](/engine/6000.5/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytypet\(\))
* [Object.FindObjectsByType\<T>(FindObjectsInactive)](/engine/6000.5/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytypet\(findobjectsinactive\))
* [Object.FindAnyObjectByType(Type)](/engine/6000.5/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytype\(type\))
* [Object.FindAnyObjectByType(Type, FindObjectsInactive)](/engine/6000.5/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytype\(type-findobjectsinactive\))
* [Object.ToString()](/engine/6000.5/script-reference/unityengine/object/tostring.md)
* [Object.name](/engine/6000.5/script-reference/unityengine/object/name.md)
* [Object.hideFlags](/engine/6000.5/script-reference/unityengine/object/hideflags.md)

### Operators

| Operator                                                                           | Description                                                             |
| ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| [operator ==](/engine/6000.5/script-reference/unityengine/object/op-equality.md)   | Compares two object references to see if they refer to the same object. |
| [bool](/engine/6000.5/script-reference/unityengine/object/op-implicit.md)          | Determines whether the object exists.                                   |
| [operator !=](/engine/6000.5/script-reference/unityengine/object/op-inequality.md) | Compares if two objects refer to a different object.                    |
