How Do I Change the Animator Component Tied to a Timeline Animation Track During Runtime?

The idea is to make it so that in the editor, I can use a Timeline Asset to preview sequences without having to worry about what specific Animator components will be used during runtime. Depending on what’s happening in game, it won’t always be the same model(s) going through the animations. That’s why I want to use C# to swap things in and out as appropriate.

Thing is… How do I do that?

I know that creating a custom PlayableBehaviour is part of it, but not much else. I’d appreciate help with this.

You have to rebind the Animator at runtime.

public static void BindObject(this PlayableDirector playableDirector, string trackName, GameObject toBind)
{
    var optTracks = OptTracks(playableDirector);
    if (optTracks.None()) return;
    foreach (var outputTrack in optTracks.Single())
    {
        // IndusLogger.Debug($"[UnityExtensionUtils.BindObject()]","timeline {playableDirector.name} track {outputTrack} requested {trackName}");
        if (outputTrack.name != trackName) continue;

        var type = outputTrack.outputs
            .FirstOptional()
            .Select(b => b.outputTargetType)
            .OrElse(typeof(GameObject));

        Object toBindObject = toBind;
        if (type == typeof(GameObject))
        {
            //We're good
        }
        else if (type == typeof(Animator))
        {
            toBindObject = toBind.GetComponent<Animator>();
        }//TODO anything else if needed
        else if (type == typeof(CinemachineBrain))
        {
            toBindObject = toBind.GetComponent<CinemachineBrain>();
        }

        playableDirector.SetGenericBinding(outputTrack, toBindObject);
    }
}