1 (edited by immortalfray 2022-09-30 14:20:49)

Topic: Implementing Cinemachine for storymode.

I’m using this section to document my progress on implementing Cinemachine into UFE in order to replace the panels used in storymode with an actual animated cutscene.  I’m mainly doing this to showcase my knowledge in working with another programmers code base and also because it’s a core component in my own fighting game project.

2 (edited by immortalfray 2022-10-08 15:06:42)

Re: Implementing Cinemachine for storymode.

https://youtu.be/mlZ9CVO4MOA

https://media.giphy.com/media/aYw0feS3XkO92pX6Wq/giphy.gif
Here is the current progress of my Cinemachine implementation.  Its pretty rough and this test cutscene doesn't have much going on but so far Im not getting any errors or interruption during gameplay now that the main camera also has a Cinemachine brain component attached to it.  Im going to refine the code and then I will share everything here and maybe write up a tutorial for anyone who wants to use this or if Mistermind wants to include it in a future update.

3 (edited by immortalfray 2022-10-11 09:31:38)

Re: Implementing Cinemachine for storymode.

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


https://media.giphy.com/media/aYw0feS3XkO92pX6Wq/giphy.gif


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:
https://i.imgur.com/vvDtcfH.jpg


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:
https://i.imgur.com/JasBuT2.jpg

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.
https://i.imgur.com/tIkqWBg.jpg


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.

Re: Implementing Cinemachine for storymode.

I wrote an article last year going over how to construct a track in Cinemachine.  This should help anyone get started with creating cutscenes in their project.

Creating cutscenes in unity with Cinemachine and Timeline