I’ve been working on this little project for 2 weeks now, and I’ve run in to 2 questions that are really blocking me.
- How do I get the timeline to accept new animations at runtime?
- How do I get the timeline to update when I create new tracks
I’m attempting to record animation from runtime directly into the Unity timeline. I have this prefab that I instantiate (the movement of this prefab is what will be recorded into animation clips in timeline tracks, and in it’s Start() function, I have it create a new track with animation clip in the timeline.
Using the GameObjectRecorder, I’m able to record animation directly into these clips. But when I stop the recording and scrub back in the timeline (during runtime), it won’t play back this new animation. If I have the prefab sitting in the scene before runtime, and it’s assigned to an animation track, and I attach a premade .anim Asset to that track, it will work fine in runtime, I can scrub back and forth over the animation. But not when the animation is created and added DURING runtime.
Is it impossible for the timeline to accept new clips and play them back in runtime?
Here is my code that creates the animation tracks, and records the animation into them:
public bool record = false;
public AnimationClip clip;
private GameObjectRecorder m_Recorder;
private GameObject animNodeTransform;
private SceneTransport sceneTransport;
private PlayableDirector scenePlayableDirectorComponent;
void Start()
{
sceneTransport = GameObject.Find("Timeline").GetComponent<SceneTransport>();
animNodeTransform = gameObject.transform.Find("AnimNodeTransform").gameObject;
m_Recorder = new GameObjectRecorder(gameObject);
scenePlayableDirectorComponent = GameObject.Find("Timeline").GetComponent<PlayableDirector>();
m_Recorder.BindComponent<Transform>(gameObject, false);
m_Recorder.BindComponent<Transform>(animNodeTransform, false);
//This is to create an Animation Clip
clip = new AnimationClip
{
name = gameObject.name + " clip"
};
//instantiate playable for animationClip
AnimationClipPlayable playable = AnimationClipPlayable.Create(scenePlayableDirectorComponent.playableGraph, clip);
//get timelineAsset from playableDirector
TimelineAsset timelineAsset = (TimelineAsset)scenePlayableDirectorComponent.playableAsset;
//create new animationTrack on timeline
var newTrack = timelineAsset.CreateTrack<AnimationTrack>(null, gameObject.name);
//bind object to which the animation shall be assigned to the created animationTrack
scenePlayableDirectorComponent.SetGenericBinding(newTrack, gameObject);
//create a timelineClip for the animationClip on the AnimationTrack
var timelineClip = newTrack.CreateClip(clip);
timelineClip.displayName = gameObject.name + " clip";
}
void Update()
{
if (sceneTransport.playing && sceneTransport.recording)
{
record = true;
}
else
{
record = false;
}
}
void LateUpdate()
{
if (record)
{
m_Recorder.TakeSnapshot(Time.deltaTime);
}
else if (m_Recorder.isRecording)
{
m_Recorder.SaveToClip(clip);
m_Recorder.ResetRecording();
}
}
Also, a smaller problem, is that when I instantiate my gameObjects, they create tracks in the timeline, but I don’t see them until I mess around with the editor in some way, like create a track group, then a bunch of animation tracks will just appear. Or if I close the Timeline window and open it again. How do I get the editor to update when I create the tracks?
Thanks to this thread over here for writing most of this code ![]()