Recording animation into the timeline (and playing it back) at runtime
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 :)
Answer by seant_unity · Jun 01, 2018 at 11:37 AM
You may need to call this on your animation clips - https://docs.unity3d.com/ScriptReference/AnimationUtility.SetGenerateMotionCurves.html to get them to work in Timeline.
Also, if you change the timeline (e.g. add a new track or clip), via script you will need to call PlayableDirector.RebuildGraph() for the changes to take effect.
To get the editor to update, there is no script call yet, but you should be able to select a different object, then select the original playable director as a workaround to get timeline to rebuild the view. That should work unless the timeline window is locked.
Can you please share your code? I'm working on the same issue.
Your answer
Follow this Question
Related Questions
Issue with timeline : gameobject's transform changed 1 Answer
Simple gameobject animation not showing in play mode (no script, Keyframes) 0 Answers
in-between animation between 2 frames 1 Answer
Problem while editing animation clips on timeline 1 Answer
Export Multiple Objects Animations from Maya to Unity 0 Answers