- Home /
Binding animation clip to Timeline and change position/rotation offsets of animation clip by API
Hi, I am new to the timeline API. I need to create an animation track for quite a few of my prefabs on the timeline. Therefore, I am trying to write a script to automate the process. For each of the prefab, I want to write a script to create an animation track and place into the timeline, associate it with the animation clip and then adjust the position and rotation offset accordingly. I have gone into a few problems as below and would appreciate any help please.
1) I manage to create a new track and a default clip. The track is bind with the game object I want. However, it only works when the code is running. Once the code stops running, the originally binding for the track returns back to "None". Sometimes, I will get error about the Playable Graph is null.
2) The default clip is created but the animation clip has not been successfully bind to it.
3) I need to adjust the offset position and rotation of the animation clip and I cannot find the API to get access to the animation clip for each of the prefab and change it.
I would appreciate any help please. Thanks!
Here is the code for your reference:
for (int i = 0; i < myPrefabs.Length; i++) {
anim = myPrefabs[i].GetComponent<Animator>();
if (anim != null)
{
AnimationClipPlayable playable = AnimationClipPlayable.Create
(playableDirector.playableGraph, myClip);
if (playableDirector.playableGraph.IsValid())
{
//Create output for animator to assign to
AnimationPlayableOutput output = AnimationPlayableOutput.Create
(playableDirector.playableGraph, myPrefabs[i].name + " output", anim);
//Set Source Playable to output
output.SetSourcePlayable<AnimationPlayableOutput, AnimationClipPlayable>(playable);
} else
{
Debug.Log("playable Graph is null, director hasn't started playing");
}
}
//get Timeline from playableDirector
TimelineAsset timelineAsset = (TimelineAsset)playableDirector.playableAsset;
//Create New Animation Track on timeline
newTrack = timelineAsset.CreateTrack<AnimationTrack>(null, myPrefabs[i].name + " Track");
playableDirector.SetGenericBinding(newTrack, myPrefabs[i]);
timelineClip = newTrack.CreateDefaultClip();
var animationOutput = (AnimationPlayableOutput)playableDirector.playableGraph.GetOutput(0);
animationOutput.SetTarget(anim);
}
Answer by seant_unity · Aug 05, 2019 at 11:27 AM
Here's an example that should help. It looks like source of the confusion is between the timeline API and the playable API. In the example below it only uses the timeline API to construct everything, and the playable director to do the binding.
// Create the track
var newTrack = timelineAsset.CreateTrack<AnimationTrack>(null, name);
// Create the clip and return the playable asset attached
var animPlayableAsset = (AnimationPlayableAsset) newTrack.CreateClip(myClip).asset;
// Set the clip offsets
animPlayableAsset.position = myPosition;
animPlayableAsset.rotation = myRotation;
// bind the track to the animator
playableDirector.SetGenericBinding(newTrack, anim);
Hi, Thanks so much! You are my savior. I have been struggling on this and tried for almost a week and in vain. Thanks!!
This works! It has successfully created the track, clip and bind correctly. However, it does not appear on the timeline until I play.
I put the create code script as a custom button on the inspector of the timeline. Then, I drag the game Object, the clip and then press the custom button. In the console, I checked and the code has been run. But the created track with bind clip does not appear on the timeline. Once I run the game( hit play), then it appears.
Would you $$anonymous$$d letting me know what I may have missed please? Thanks again.
Hmm...there is one thing to check. In an editor script, after you create the timeline asset and before you create any tracks, you need to save the timeline using AssetDatabase.CreateAsset(). Otherwise the tracks won't actually save to the timeline. $$anonymous$$aybe that's it?
Hi, Thanks again for your great help and reply again! Sorry may be I did not explain clearly. The track and clip are actually created and are already saved on the timeline. The question, is for some reason, I need to hit "play" until I see them appear on the timeline. That was my question. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612205943im_/https://answers.unity.com/themes/thub/images/avi.jpg)