Search Unity

Instanciated timeline losing connections in animation track

Discussion in 'Timeline' started by Diesign, Aug 2, 2018.

  1. Diesign

    Diesign

    Joined:
    May 15, 2016
    Posts:
    16
    I have an animated vcam in a timeline track which is instantiated at runtime. When it is instantiated the vcam loses connection to the track. Is it even possible to reconnect it using the API or is there a better way to instantiate a timeline that has a vcam in an animator track?
     
  2. gekidoslayer

    gekidoslayer

    Unity Technologies

    Joined:
    Sep 27, 2016
    Posts:
    134
    easiest way to handle this is to have the vcams as a child of the gameobject with the timeline on it and instantiate the whole thing as a prefab. As long as the timeline and referenced gameobjects are all in the same prefab, they will retain their references...
     
  3. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    If you still need to do runtime binding of tracks to animators, you can use PlayableDirector.SetGenericBinding(track, animator). You can find the track by using TimelineAsset.GetOutputTracks() and searching them by name.
     
  4. Diesign

    Diesign

    Joined:
    May 15, 2016
    Posts:
    16
    Thanks for responding so quickly! 



    You were both super helpful. @mikew_unity you are right - making the vcam a child of the timeline gameobject totally clears up the animator binding. 



    Turns out the real question I should have been asking was how to bind the cinemachine brain to the cinemachine.timeline. Luckily I spent a good bit of yesterday reading up on the api docs you referenced yesterday @seant_unity and I finally got something working that binds everything up at runtime:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5. using UnityEngine.Timeline;
    6. using Cinemachine;
    7.  
    8. public class DynamicBind : MonoBehaviour
    9. {
    10.  
    11.     public TimelineAsset TimlineAsset;
    12.     public string TrackName;
    13.     public CinemachineBrain cineCam;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         cineCam = GameObject.FindGameObjectWithTag("PictureCamera").GetComponent<CinemachineBrain>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.  
    24.  
    25.         var director = this.GetComponent<PlayableDirector>();
    26.         director.playableAsset = TimlineAsset;
    27.  
    28.         foreach (var playableAssetOutput in director.playableAsset.outputs)
    29.         {
    30.             if (playableAssetOutput.streamName == TrackName)
    31.             {
    32.                 director.SetGenericBinding(playableAssetOutput.sourceObject, cineCam);
    33.             }
    34.  
    35.         }
    36.         director.Play();
    37.  
    38.     }
    39.  
    40. }
    41.  

    What's weird is now the timeline just won't play but it seems to think it is playing. Even Play on Awake doesn't work while the script is activated for some reason. I am not getting errors logged in the console and when I check out the timeline and try to manually play it you can see the some non standard state of the play button is active for some reason.

    Here is a video showing how everything plays fine without the script but then activating the script totally prevents anything from playing. 


    https://www.dropbox.com/s/jwqhl2jk1zt5a7s/bug?.mov?dl=0



    Am I just missing something obvious here? I have spent an embarrassing amount of time googling and trying different things and I feel like I am close but I am honestly not sure if I am even on the right track haha.
     
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    That will work except if the director is already playing. If you change the bindings on a director that's playing you need to call director.RebuildPlayableGraph() - but only call that if the bindings change, doing it every frame can be very costly.
     
  6. Diesign

    Diesign

    Joined:
    May 15, 2016
    Posts:
    16
    Thank you so very much @seant_unity !!!! Everything is working just fine now. Words cannot express the gratitude I am feeling right now.

    You are deeply appreciated.


    If anyone else is having a similar problem I just executed everything at start.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5. using UnityEngine.Timeline;
    6. using Cinemachine;
    7.  
    8. public class DynamicBind : MonoBehaviour
    9. {
    10.  
    11.     public TimelineAsset TimlineAsset;
    12.     public string TrackName;
    13.     public CinemachineBrain cineCam;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         cineCam = GameObject.FindGameObjectWithTag("PictureCamera").GetComponent<CinemachineBrain>();
    19.  
    20.         var director = this.GetComponent<PlayableDirector>();
    21.         director.playableAsset = TimlineAsset;
    22.  
    23.         foreach (var playableAssetOutput in director.playableAsset.outputs)
    24.         {
    25.             if (playableAssetOutput.streamName == TrackName)
    26.             {
    27.                 director.SetGenericBinding(playableAssetOutput.sourceObject, cineCam);
    28.             }
    29.  
    30.         }
    31.         director.Play();
    32.  
    33.     }
    34.  
    35. }
    36.  
     
    senkal_ and seant_unity like this.
  7. Diesign

    Diesign

    Joined:
    May 15, 2016
    Posts:
    16
    @mikew_unity sorry for reviving a dead thread but I have been thinking deeply about how to integrate timelines into architecture recently.

    So you generally keep a vcam embedded in each prefab with it's associated timeline? Is there a best practice that you follow for setting the Cinemachine Brain?
     
  8. Meheraj7

    Meheraj7

    Joined:
    Nov 5, 2015
    Posts:
    81
    In my case I cannot instantiate the gameplay camera with the cutscene so i am not able to make a transition from the cutscene vcams to my gameplay vcam because the gameplay vcam's reference doesnt exist when its instantiated. is there anyway to re assign my gameplay vcam to the cinemachine track in timeline?
     
  9. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    PlayableDirector.SetGenericBinding(track, vcam) is the script call you need to bind a camera at runtime.
     
  10. Meheraj7

    Meheraj7

    Joined:
    Nov 5, 2015
    Posts:
    81
    is it the same for even cameras that are in the cinemachinetrack? I am asking cuz i cant see those cameras in the playabledirector's bindings.