How to Implement Cinemachine into Story Mode
*This tutorial assumes you understand how to actually create cutscenes using timeling and cinemachine so I wont talk about how to construct a track. If you'd like a tutorial on how to create a cutscene with cinemachine you can check out this article i wrote here:
Creating cutscenes in unity with Cinemachine and Timeline
1. Import Cinemachine and Timeline from the package manager. (They usually come together)
2. Select the main camera then go to Component > Cinemachine > CinemachineBrain.
This component MUST be attached to the main camera in order for Cinemachine to play cutscenes
3. Create a CinemachineCutscene object
-Create an empty gameobject and attach this script: (make sure the name of your script matches the class name or you'll get errors. Then save it in the appropriate scripts folder)
using UnityEngine;
using System.Collections;
using UnityEngine.Playables;
using UFE3D;
public class StoryModeCinemachine : StoryModeScreen
{
#region public instance properties
//the cutscene object that has a PlayableDirector component
public PlayableDirector cutscene;
public bool skippable = true;
public bool stopPreviousSoundEffectsOnLoad = false;
public float delayBeforeGoingToNextScreen;
public float minDelayBeforeSkipping = 0.1f;
#endregion
#region public override methods
public override void OnShow()
{
base.OnShow();
//we cast the value of cutscene.duration as a float into
delayBeforeGoingToNextScreen = (float)cutscene.duration;
UFE.StopMusic();
if (this.stopPreviousSoundEffectsOnLoad)
{
UFE.StopSounds();
}
this.StartCoroutine(this.ShowScreen());
}
public virtual IEnumerator ShowScreen()
{
float startTime = Time.realtimeSinceStartup;
float time = 0f;
while (
time < this.delayBeforeGoingToNextScreen &&
!(skippable && Input.anyKeyDown && time > this.minDelayBeforeSkipping)
)
{
yield return null;
time = Time.realtimeSinceStartup - startTime;
}
this.GoToNextScreen();
}
#endregion
}
Your setup should look something like this:
4. Create the cutscene child object
-Create an empty gameobject child of the CinemachineCutscene object
-With that object selected go to Window > Sequencing > Timeline
-Attach a PlayableDirector component to that cutscene object
-Now attach this script to the cutscene gameobject:
using UnityEngine.Timeline;
using UnityEngine.Playables;
using UnityEngine;
using Cinemachine;
public class GrabCinemachineCamera : MonoBehaviour
{
[SerializeField]
PlayableDirector director;
[SerializeField]
CinemachineBrain cam;
private void OnEnable()
{
director = GetComponent<PlayableDirector>();
cam = Camera.main.GetComponent<CinemachineBrain>();
SetCinemachineBrain(director, cam);
}
public void SetCinemachineBrain(PlayableDirector director, CinemachineBrain brain)
{
var timeline = director.playableAsset as TimelineAsset;
if (timeline == null) return;
//iterate on all tracks that have a binding
foreach (var track in timeline.GetOutputTracks())
{
//get the object bound to the track, and change it if it's a CinemachineBrain
var boundBrain = director.GetGenericBinding(track) as CinemachineBrain;
if (boundBrain == null)
director.SetGenericBinding(track, brain);
}
}
}
The cinemachine track needs a Cinemachine Brain component to be injected into its binding at runtime so this script will grab this component from the main camera.
Your child object that holds the actual cutscene should look like this:
5. Make the CinemachineCutscene a UI prefab and put it in the same folder with the other UI prefabs in your project
6. All you have to do now is assign your new CinemachineCutscene prefab into the story mode section of the global config.
-can be used instead of a TextureConversation screen before or after battle
-in this example I have it assigned as the main cutscene that plays at the beginning of story mode but it can be applied anywhere as far as I know.
I've only just begun to test this out so I expect to make edits and changes as needed and I will share them here. If this is too hard to follow just post here and I'll try my best to help you. Custom scripts that you write for your specific project may or may not affect the implementation of Cinemachine.