Home > Mobile >  How to create animation clip via script in Unity
How to create animation clip via script in Unity

Time:05-07

I want to create an AnimationClip in script against a GameObject in Unity. However, I do not know how to do it.

I have attached the following code to a GameObject in Cube and pressed the Play button. However, I got an error output.

using UnityEngine;
using System.Collections;
public class LinearExample : MonoBehaviour 
{
    private float m_start_time = 0.0f;
    private float m_start_value = 0.0f;
    private float m_end_time = 5.0f;
    private float m_end_value = 10.0f;
    
    public GameObject cubeObject;
    void Start ()
    {
        Animation animation = GetComponent<Animation> ();

        cubeObject = GameObject.Find("Cube");

        if (!animation)
        {
            cubeObject.AddComponent<Animation>();
        }
        AnimationClip clip = new AnimationClip();
        AnimationCurve curve = AnimationCurve.Linear(m_start_time, m_start_value, m_end_time, m_end_value);
        clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
        animation.AddClip(clip, "Move");
        animation.Play("Move");
    }
}

This is an error code

MissingComponentException: There is no 'Animation' attached to the "Cube" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "Cube". Or your script needs to check if the component is attached before using it.
UnityEngine.Animation.AddClip (UnityEngine.AnimationClip clip, System.String newName, System.Int32 firstFrame, System.Int32 lastFrame) (at /Users/builduser/buildslave/unity/build/artifacts/MacEditor/modules/Animation/AnimationsBindings.gen.cs:400)
UnityEngine.Animation.AddClip (UnityEngine.AnimationClip clip, System.String newName) (at /Users/builduser/buildslave/unity/build/artifacts/MacEditor/modules/Animation/AnimationsBindings.gen.cs:390)
LinearExample.Start () (at Assets/LinearExample.cs:24)
 

How can I fix this error code?

Also how can I create an AnimationClip without playing Unity? This code has a Start function, but I don't want to create the Animation at the same time as Start, I want to use it as an Editor script.

enter image description here

CodePudding user response:

I found the solution. First you need to know that the animation system without an Animator is legacy. then enable it in the code and then add the clip as follows:

void Start ()
{
    var _animation = GetComponent<Animation>();

    cubeObject = GameObject.Find("Cube");

    if (!_animation) _animation = cubeObject.AddComponent<Animation>();

    var clip = new AnimationClip();
    var curve = AnimationCurve.Linear(m_start_time, m_start_value, m_end_time, m_end_value);
    clip.SetCurve("", typeof(Transform), "localPosition.x", curve);

    clip.name = "Move"; // set name
    clip.legacy = true; // change to legacy
    
    _animation.clip = clip; // set default clip
    _animation.AddClip(clip, clip.name); // add clip to animation component

    AssetDatabase.CreateAsset(clip, "Assets/" clip.name ".anim"); // to create asset
    _animation.Play(); // then play
}
  • Related