Search Unity

How to set the virtual camera in a "CinemachineShot" from code

Discussion in 'Timeline' started by Mischawake, Sep 26, 2017.

  1. Mischawake

    Mischawake

    Joined:
    Dec 13, 2012
    Posts:
    3
    I have a Timeline with a single Cinemachine Track. My game's camera's are instantiated at runtime, so I am trying to set a CinemachineBrain for the track, and the virtual camera for the CinemachineShot clip on that track. I have the following script running on the game object with a PlayableDirector that holds the Timeline I'm using. It works for setting the CinemachineBrain for the track, but I can't figure out how to access the Virtual Camera for the CinemachineShot.

    Code (CSharp):
    1.  
    2.      
    3.         PlayableDirector director = GetComponent<PlayableDirector>();
    4.         foreach (var output in director.playableAsset.outputs)
    5.         {
    6.             if (output.streamName == "Cinemachine Track" )
    7.             {
    8.                 director.SetGenericBinding(output.sourceObject, myCamera.GetComponent<CinemachineBrain>() );
    9.             }
    10.         }
    11.  
    12.  
    I've read this post, https://forum.unity.com/threads/timeline-cinemachine-create-shots-by-script.482480/#post-3141845, on setting shots from script, but the "CinemachineShot" class that's referenced there doesn't appear in any API I've found. I've also seen some posts on using ExposedReferenes but right now I'm not able to access the CinemachineShot clip which I assume contains the ExposedReference for the VirtualCamera?

    Help!
     

    Attached Files:

  2. Mischawake

    Mischawake

    Joined:
    Dec 13, 2012
    Posts:
    3
    After some more experimenting I managed to figure this out myself. My final code is below. One thing that was not obvious was needing to include the namespace Cinemachine.Timeline in addition to Cinemachine and using UnityEngine.Timeline. After finding which output in the PlayableDirector was the CinemachineTrack I had to cast the sourceObject of that output as a CinemachineTrack in order to access it's list of clips through GetClips(). Once I had the clips, I had to cast the clip's asset as a CinemachineShot, in order to access the VirtualCamera's exposed name. Once I had the expose name I was able to go back and use the SetReferenceValue in the PlayableDirector.

    The official documentation for this pretty sparse as of now, I hope this helps anyone who is dealing with the same issue.

    Code (CSharp):
    1.  
    2.  
    3. void Start () {
    4.        
    5.         GameObject vcgo = new GameObject();
    6.         vc = vcgo.AddComponent<CinemachineVirtualCamera>();
    7.  
    8.         PlayableDirector director = GetComponent<PlayableDirector>();
    9.         foreach (var output in director.playableAsset.outputs)
    10.         {
    11.             Debug.Log(output.sourceObject.GetType() );
    12.             if (output.streamName == "Cinemachine Track" )
    13.             {
    14.                 director.SetGenericBinding(output.sourceObject, Camera.main.GetComponent<CinemachineBrain>() );
    15.            
    16.                 var cinemachineTrack = output.sourceObject as CinemachineTrack;
    17.                 foreach( var clip in cinemachineTrack.GetClips() ){
    18.  
    19.                     var cinemachineShot = clip.asset as CinemachineShot;
    20.                     director.SetReferenceValue(cinemachineShot.VirtualCamera.exposedName, vc);
    21.  
    22.                 }
    23.             }
    24.         }
    25.     }
     
    whiteleon likes this.
  3. redocx

    redocx

    Joined:
    Dec 13, 2012
    Posts:
    23
    nice, it's a good way to do this!!
     
  4. 1257961942

    1257961942

    Joined:
    Jul 22, 2017
    Posts:
    5
    But there is a problem,before you set the value ,the virtualcamera of shot is nothing,when i debug the exposedname,it's zero?