- Home /
Adding animation for object to timeline at runtime
Hello everybody,
I am currently playing around with the new timeline feature in unity. What I am trying to do is to assign an AnimationClip(I recorded at runtime) to an object of the scene AND add it to the timeline. Assigning the AnimationClip to the object through this code
PlayableDirector scenePlayableDirectorComponent;
AnimationClip clip;
Animator animatorOfSelected;
GameObject toAssignAnimationTo;
//get Animator of object to assign animationClip to
if (toAssignAnimationTo.GetComponent<Animator> () != null) {
animatorOfSelected = toAssignAnimationTo.GetComponent<Animator> ();
} else {
animatorOfSelected = toAssignAnimationTo.AddComponent<Animator> ();
}
//instantiate playable for animationClip
AnimationClipPlayable playable = AnimationClipPlayable.Create(scenePlayableDirectorComponent.playableGraph, clip);
//Create output for animator of object to assign animationClip to
AnimationPlayableOutput output = AnimationPlayableOutput.Create(scenePlayableDirectorComponent.playableGraph, toAssignAnimationTo.name + " output", animatorOfSelected);
//setSourcePlayable for output
output.SetSourcePlayable<AnimationPlayableOutput, AnimationClipPlayable> (playable);
works fine.
But I want the animation to appear in the timeline I created in the editor.
My approach is this:
GameObject toAssignAnimationTo;
PlayableDirector scenePlayableDirectorComponent;
//get timelineAsset from playableDirector
TimelineAsset timelineAsset = (TimelineAsset)scenePlayableDirectorComponent.playableAsset;
//create new animationTrack on timeline
var newTrack = timelineAsset.CreateTrack<AnimationTrack> (null, "Animation Track " + toAssignAnimationTo.name);
//bind object to which the animation shell be assigned to the created animationTrack
scenePlayableDirectorComponent.SetGenericBinding (newTrack, toAssignAnimationTo);
//create a timelineClip for the animationClip on the AnimationTrack
var timelineClip = newTrack.CreateClip (clip);
which seems to go into the right direction:
But the animation on "Cube" is not doing anything(the animations for "redCube" and "blueCube" were recorded in the editor, they work fine).
I hope somebody give me some advise, Thanks in advance!
I couldnt add any track so far now, but your codes work. Did you get any solution? I am strugling with these problems.
Thank you for your codes.
Answer by seant_unity · Sep 29, 2017 at 06:26 PM
Hi,
You will need to generate motion curves for the object to use it in Timeline. There is a button to do that in the AnimationClip inspector.
Unfortunately there is no officially supported public API call to do that, but if you are so inclined, you can use reflection to access the static internal method AnimationUtility.SetGenerateMotionCurves (AnimationClip clip, bool value), until a public API is available.
Cheers
Ah okay.. So at the moment there's no 'official' way to record an animation for an object at runtime so that this animation appears on the timeline? Or do I only have the wrong approach?
Edit: I just checked how to use the AnimationUtility.SetGenerate$$anonymous$$otionCurves (AnimationClip clip, bool value) method with reflection, but I'm not sure how exactly to do it, with reflection I only get access to the functions I could call the 'normal functioncall-way' too. Sorry if it is a stupid question but reflection is new to me..
Thanks!