94 lines
2.8 KiB
C#
94 lines
2.8 KiB
C#
using Bootstrap;
|
|
using System;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.Playables;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
namespace CinematicsM
|
|
{
|
|
/// <summary>
|
|
/// Handles loading, playing and unloading cinematics
|
|
/// </summary>
|
|
public class CinematicsManager : MonoBehaviour
|
|
{
|
|
private static CinematicsManager _instance;
|
|
private static bool _isQuitting;
|
|
private Image cinematicSprites;
|
|
public PlayableAsset cinematicToPlay;
|
|
|
|
public static CinematicsManager Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null && Application.isPlaying && !_isQuitting)
|
|
{
|
|
_instance = FindAnyObjectByType<CinematicsManager>();
|
|
if (_instance == null)
|
|
{
|
|
var go = new GameObject("CinematicsManager");
|
|
_instance = go.AddComponent<CinematicsManager>();
|
|
// DontDestroyOnLoad(go);
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
public PlayableDirector playableDirector;
|
|
|
|
private void OnEnable()
|
|
{
|
|
|
|
}
|
|
/// <summary>
|
|
/// Plays a cinematic from an object reference and returns the PlayableDirector playing the timeline
|
|
/// </summary>
|
|
public PlayableDirector PlayCinematic(PlayableAsset assetToPlay)
|
|
{
|
|
cinematicSprites.enabled = true;
|
|
playableDirector.stopped += OnPlayableDirectorStopped;
|
|
playableDirector.Play(assetToPlay);
|
|
Debug.Log("Playing cinematic " + assetToPlay.name);
|
|
return playableDirector;
|
|
}
|
|
|
|
void OnPlayableDirectorStopped(PlayableDirector director)
|
|
{
|
|
cinematicSprites.enabled = false;
|
|
Debug.Log("Cinematic stopped!");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads a playable from an asset path and plays it as a cinematic
|
|
/// </summary>
|
|
public PlayableDirector LoadAndPlayCinematic(string key)
|
|
{
|
|
var handle = Addressables.LoadAssetAsync<PlayableAsset>(key);
|
|
var result = handle.WaitForCompletion();
|
|
return PlayCinematic(result);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
private void Awake()
|
|
{
|
|
_instance = this;
|
|
|
|
if (!SceneManager.GetActiveScene().name.ToLower().Contains("mainmenu"))
|
|
{
|
|
return;
|
|
}
|
|
|
|
playableDirector = GetComponent<PlayableDirector>();
|
|
cinematicSprites = GetComponentInChildren<Image>(true);
|
|
LoadAndPlayCinematic("IntroSequence");
|
|
}
|
|
}
|
|
}
|