Skip to content

Instantly share code, notes, and snippets.

@ciro-unity
Last active January 22, 2023 20:57
Show Gist options
  • Save ciro-unity/fa6ea64635c1d473b9dae64ac1853a92 to your computer and use it in GitHub Desktop.
Save ciro-unity/fa6ea64635c1d473b9dae64ac1853a92 to your computer and use it in GitHub Desktop.
A script that provides a simple example of how to dynamically assign the bound object of a track in Unity's Timeline. In this case, an Animator is assigned to an Animation track as soon as the execution starts.
using System.Collections;
using UnityEngine;
using UnityEngine.Playables;
public class TimelineBinder : MonoBehaviour
{
public PlayableDirector playableDirector; //the component which references the Timeline we want to target
public Animator objectToBind; //bound object is an Animator because we are targeting an Animation track
public string trackName;
private void Start()
{
//The "outputs" in this case are the tracks of the PlayableAsset
foreach (var playableAssetOutput in playableDirector.playableAsset.outputs)
{
if (playableAssetOutput.streamName == trackName)
{
playableDirector.SetGenericBinding(playableAssetOutput.sourceObject, objectToBind);
break;
}
}
}
}
@baroquedub
Copy link

might as well add a break; after line 18, saves iterating through all the tracks even after you've got the one you want

@ciro-unity
Copy link
Author

Thanks and good catch, makes sense.

@KongoPL
Copy link

KongoPL commented Oct 9, 2021

@ciro-unity I owe you a good beer for that example, thank you!

@ciro-unity
Copy link
Author

Happy to hear @KongoPL :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment