I am trying to create a script that automatically create animations and assigning them to a timeline.
All works good until i have to assign an animator to my Animation Track i have created.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using UnityEngine.Animations;
public class CreateAnimation : MonoBehaviour {
public PlayableDirector director;
public TimelineAsset timeline;
public AnimationClip clip;
public AnimationCurve curve;
void Start () {
Animation anim = GetComponent<Animation>();
// create a new AnimationClip
clip = new AnimationClip();
clip.legacy = false;
// create a curve to move the GameObject and assign to the clip
Keyframe[] keys;
keys = new Keyframe[3];
keys[0] = new Keyframe(0.0f, 0.0f);
keys[1] = new Keyframe(1.0f, 1.5f);
keys[2] = new Keyframe(2.0f, 0.0f);
curve = new AnimationCurve(keys);
clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
// update the clip to a change the red color
curve = AnimationCurve.Linear(0.0f, 1.0f, 2.0f, 0.0f);
clip.SetCurve("", typeof(Material), "_Color.r", curve);
// now animate the GameObject
/*anim.AddClip(clip, clip.name);
anim.Play(clip.name);*/
AnimationTrack track = timeline.CreateTrack<AnimationTrack>(null, "Cube");
TimelineClip timelineClip = track.CreateClip(clip);
timelineClip.start = 2;
//timeline.CreateTrack<>(null, "TestTrack");
}
I added an image to show what i want to add automatically.
Hope you can help me.