Created
July 22, 2021 01:09
-
-
Save mminer/71b2c64d813587b9056a601e0c22ae70 to your computer and use it in GitHub Desktop.
Unity component to bind the main camera's Cinemachine brain to a timeline's Cinemachine tracks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Cinemachine; | |
using UnityEngine; | |
using UnityEngine.Playables; | |
using UnityEngine.Timeline; | |
[ExecuteInEditMode] | |
public class CinemachineBrainTimelineBinder : MonoBehaviour | |
{ | |
void Start() | |
{ | |
var mainCamera = Camera.main; | |
if (mainCamera == null) | |
{ | |
Debug.LogError("No main camera available to get Cinemachine brain from."); | |
return; | |
} | |
var brain = mainCamera.GetComponent<CinemachineBrain>(); | |
var director = GetComponent<PlayableDirector>(); | |
var timeline = director.playableAsset as TimelineAsset; | |
if (timeline == null) | |
{ | |
return; | |
} | |
var cinemachineTracks = timeline | |
.GetOutputTracks() | |
.Select(track => track as CinemachineTrack) | |
.Where(track => track != null); | |
foreach (var track in cinemachineTracks) | |
{ | |
director.SetGenericBinding(track, brain); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment