Search Unity

Timeline + Cinemachine : create Shots by script

Discussion in 'Timeline' started by HugMat, Jul 10, 2017.

  1. HugMat

    HugMat

    Joined:
    Mar 30, 2017
    Posts:
    16
    Hi,
    I'm using Timeline and Cinemachine and I want to create new CinemachineShots in a CinemachineTrack via scripting, but I have 2 issues :
    - I don't know how to add a CinemachineShot in the track via script : the only way I found to do so is creating a default clip in the Cinemachine Track, but then I can only access it as a TimelineClip
    - When I have my Cinemachine Shot, I want to set its VirtualCamera but since it is an ExposedReference I don't know how. I guess i have to use the CreatePlayable method but I don't know how

    Thanks a lot
     
  2. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    177
    In order to get access to CinemachineShot, you will need to do:
    Code (CSharp):
    1. var cinemachineShot = myTimelineClip.asset as CinemachineShot;
    Here's why: Timeline is built on the Playable system to play animation, audio and custom scripts, which means that each TimelineClip contains a reference to a PlayableAsset, which contains the data need by the playable to operate at runtime. A TimelineClip is generic and is only concerned with timing (duration, start, end, etc.) and blending. If you want to get access to data specific only to a certain type of clip (like the Virtual Camera property of a CinemachineShot), you will need to through the TimelineClip's asset.

    To set the Virtual Camera:
    Code (CSharp):
    1. myCinemachineShot.VirtualCamera.exposedName = UnityEditor.GUID.Generate().ToString();
    2. myPlayableDirector.SetReferenceValue(myCinemachineShot.VirtualCamera.exposedName, myVirtualCamera);
    Here's why: Since CinemachineShot is an asset, it cannot contain any reference to an object in a scene. So you need to ask the PlayableDirector to remember that VirtualCamera (an exposed reference) refers a given object in the scene. You don't have to do it inside the CreatePlayable method.

    Hope this helps!

    [EDIT] I forgot to initialize the exposed reference name.
     
    Last edited: Jul 19, 2017
    friuns3, joelsinbarba and _slash_ like this.
  3. HugMat

    HugMat

    Joined:
    Mar 30, 2017
    Posts:
    16
    Yeah, it helps a lot !
    I'm having another issue though : when I add a second CinemaShot on my track, and I want to set its VirtualCamera, it modifies also the VirtualCamera for the first Shot.
    I've tried to change the exposedname of the second shot but the problem remains...
     
  4. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    177
    I forgot one important step :confused:

    Code (CSharp):
    1. myCinemachineShot.VirtualCamera.exposedName = UnityEditor.GUID.Generate().ToString();
    It's not obvious that you need to initialize exposedName. Sorry about that.;)
     
  5. HugMat

    HugMat

    Joined:
    Mar 30, 2017
    Posts:
    16
    It works, thanks a lot.
    I just have one last question : is it possible to "update" the CinemachineTrack in Run mode.
    Indeed, the camera linked to my CinemachineTrack does not use the CinemachineShots I create in Run, like it doesn't detect them
     
  6. leoleoroy

    leoleoroy

    Joined:
    Jul 21, 2017
    Posts:
    5
    Hello HugMat

    Could you tell me how to creating a default clip in the Cinemachine Track, but then I can only access it as a TimelineClip?
    I try a lot of time, still don't how to access.
    Thank you~
     
  7. leoleoroy

    leoleoroy

    Joined:
    Jul 21, 2017
    Posts:
    5
    Hello julienb:)

    I try to use your method.
    But "var cinemachineShot = myTimelineClip.asset as CinemachineShot;"
    I just have "CinemachineClearShot" can be choose >_<
     
  8. HugMat

    HugMat

    Joined:
    Mar 30, 2017
    Posts:
    16
    Hello leoleroy,

    are you using Cinemachine.Timeline ? You have to to chose CinemachineShot
     
  9. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
  10. 1257961942

    1257961942

    Joined:
    Jul 22, 2017
    Posts:
    5
    Is there a API in unityengeion to get a guid?
     
  11. totlis

    totlis

    Joined:
    Feb 6, 2018
    Posts:
    1
    hi, can i ask how you do it? Like adding virtual Cinemachine cameras using c#. It will be a big help :)
     
  12. ILightMaster

    ILightMaster

    Joined:
    Jun 18, 2018
    Posts:
    3
    exposed reference + asset bundle = lol
     
    SKoptev likes this.
  13. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    it's me or api look very strange here
     
  14. kottmann

    kottmann

    Joined:
    Mar 6, 2018
    Posts:
    6
    I created a script (with the code in this thread) which adds new CinemachineShots into timeline and connects them to a CinemachineVirtualCamera during runtime. After quitting run mode, the clip in the timeline is persisted but not the referenced VirtualCamera. Is there a way to persist this reference in CinemachineShot to the VirtualCamera?

    Code (CSharp):
    1. TimelineClip clip = cinemachineTrack.CreateClip<CinemachineShot>();
    2. var shot = clip.asset as CinemachineShot;
    3. shot.VirtualCamera.exposedName = UnityEditor.GUID.Generate().ToString();        
    4. playableDirector.SetReferenceValue(shot.VirtualCamera.exposedName, virtualCamera);
     
  15. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    The problem is the playable director, which is what saves the binding, is part of the scene. The scene gets reverted when going out of playmode. There are some tools out there to save playmode scene state, one of which exists in the Cinemachine package.

    Because Timeline is an asset, changes to it persist during playmode.