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
In order to get access to CinemachineShot, you will need to do:
var cinemachineShot = myTimelineClip.asset as CinemachineShot;
Here’s why: Timeline is built on the Playablesystem 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.
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.
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…
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
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~
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?
TimelineClip clip = cinemachineTrack.CreateClip<CinemachineShot>();
var shot = clip.asset as CinemachineShot;
shot.VirtualCamera.exposedName = UnityEditor.GUID.Generate().ToString();
playableDirector.SetReferenceValue(shot.VirtualCamera.exposedName, virtualCamera);
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.