Can't attach animation after creating one through script in Unity

29 views Asked by At

I'm supposed to create an animation for a specific object in Unity using C#. Therefore, the object has a component with the following parameters:

enter image description here

The animation clips were successfully generated and then I manually created the animation controllers in Unity using those animation clips. However, when I attach that animation controller to the object and go to Window > Animation, there is no animation attached:

enter image description here

This is the entire code:

public class CreateAnimation : MonoBehaviour
{
    public double height;
    public int activityNumber;
    public Transform transform;
    private int[] activityIndexes;

    private void Start()
    {
        int[] activityI = { 101, 33, 18, 10 };
        int[] activityII = { 24, 24, 24, 111, 111, 111, 43, 43, 43, 119, 119, 119, 51, 51, 51 };
        int[] activityIII = { 94, 94, 94, 26, 26, 26, 118, 118, 118, 50, 50, 50, 102, 102, 102, 34, 34, 34, 94, 94, 94, 26, 26, 26 };

        activityIndexes = activityI;

        var _animation = gameObject.GetComponent<Animation>();

        Dictionary<string, List<float>> positionData = CSVReader.ReadFile(1, height);

        if (activityNumber == 2)
        {
            positionData = CSVReader.ReadFile(2, height);
            activityIndexes = activityII;
        }
        if (activityNumber == 3)
        {
            positionData = CSVReader.ReadFile(3, height);
            activityIndexes = activityIII;
        }

        if (activityNumber != 3)
        {
            var clip = CreateAnimationClip("activity" + activityNumber, positionData, false, false);
            //_animation.clip = clip;
            _animation.AddClip(clip, clip.name);
            AssetDatabase.CreateAsset(clip, "Assets/Samples/" + clip.name + ".anim");
            _animation.Play(clip.name);
        }
        else
        {
            var clip = CreateAnimationClip("activity" + activityNumber + "-Part1", positionData, true, true);
            //_animation.clip = clip;
            _animation.AddClip(clip, clip.name);
            AssetDatabase.CreateAsset(clip, "Assets/Samples/" + clip.name + ".anim");
            _animation.Play(clip.name);

            clip = CreateAnimationClip("activity" + activityNumber + "-Part2", positionData, true, false);
            //_animation.clip = clip;
            _animation.AddClip(clip, clip.name);
            AssetDatabase.CreateAsset(clip, "Assets/Samples/" + clip.name + ".anim");
            _animation.Play(clip.name);
        }
    }

    private AnimationClip CreateAnimationClip(string clipName, Dictionary<string, List<float>> positionData, bool isActivityThree, bool isRestPosition)
    {
        var clip = new AnimationClip();
        clip.name = clipName;
        clip.legacy = true;
        int index = 0;

        foreach (var data in positionData)
        {
            var localAxis = "m_LocalPosition.x";
            var split = data.Key.Split('#');

            var propertyName = split[0];
            var propertyAxis = split[1];

            bool isRotation = false;
            if (propertyName == "jLeftKnee" || propertyName == "jRightKnee")
            {
                isRotation = true;
            }
            AnimationCurve curve = CreateAnimationCurve(data.Value, isRotation);

            Transform property = transform.Find(propertyName);
            if (property != null || propertyName == "RightHand" || propertyName == "LeftHand")
            {
                clip.SetCurve(transform.GetChild(activityIndexes[index]).name, typeof(Transform), localAxis, curve);
                index += 1;
            }
        }

        return clip;
    }

    private AnimationCurve CreateAnimationCurve(List<float> values, bool isRotation)
    {
        AnimationCurve curve = new AnimationCurve();

        for (int i = 0; i < values.Count; i++)
        {
            float timeInSeconds = i * 0.017f;
            curve.AddKey(timeInSeconds, values[i]);
        }
        return curve;
    }
}
0

There are 0 answers