2025-10-13 09:22:18 +02:00
|
|
|
using System.Collections.Generic;
|
2025-10-14 15:53:58 +02:00
|
|
|
using Core;
|
2025-10-10 10:07:08 +02:00
|
|
|
using UnityEngine;
|
2025-10-10 15:51:49 +02:00
|
|
|
using UnityEngine.AddressableAssets;
|
2025-10-13 09:22:18 +02:00
|
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
2025-10-10 15:51:49 +02:00
|
|
|
using UnityEngine.Playables;
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
using UnityEngine.UI;
|
2025-10-10 10:07:08 +02:00
|
|
|
|
2025-10-13 09:22:18 +02:00
|
|
|
namespace Cinematics
|
2025-10-10 10:07:08 +02:00
|
|
|
{
|
2025-10-10 15:51:49 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Handles loading, playing and unloading cinematics
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class CinematicsManager : MonoBehaviour
|
2025-10-13 12:41:04 +02:00
|
|
|
{
|
|
|
|
|
public event System.Action OnCinematicStarted;
|
|
|
|
|
public event System.Action OnCinematicStopped;
|
2025-10-10 15:51:49 +02:00
|
|
|
private static CinematicsManager _instance;
|
|
|
|
|
private static bool _isQuitting;
|
2025-10-13 14:25:11 +02:00
|
|
|
private Image _cinematicSprites;
|
|
|
|
|
private bool _isCinematicPlaying = false;
|
|
|
|
|
public bool IsCinematicPlaying => _isCinematicPlaying;
|
2025-10-10 15:51:49 +02:00
|
|
|
|
2025-10-13 09:22:18 +02:00
|
|
|
// Dictionary to track addressable handles by PlayableDirector
|
|
|
|
|
private Dictionary<PlayableDirector, AsyncOperationHandle<PlayableAsset>> _addressableHandles
|
|
|
|
|
= new Dictionary<PlayableDirector, AsyncOperationHandle<PlayableAsset>>();
|
|
|
|
|
|
2025-10-10 15:51:49 +02:00
|
|
|
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;
|
2025-10-10 10:07:08 +02:00
|
|
|
|
2025-10-10 15:51:49 +02:00
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
2025-10-13 09:22:18 +02:00
|
|
|
// Subscribe to application quit event to ensure cleanup
|
|
|
|
|
Application.quitting += OnApplicationQuit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
// Unsubscribe from application quit event
|
|
|
|
|
Application.quitting -= OnApplicationQuit;
|
|
|
|
|
|
|
|
|
|
// Clean up any remaining addressable handles when disabled
|
|
|
|
|
ReleaseAllHandles();
|
|
|
|
|
}
|
2025-10-10 15:51:49 +02:00
|
|
|
|
2025-10-13 09:22:18 +02:00
|
|
|
private void OnApplicationQuit()
|
|
|
|
|
{
|
|
|
|
|
ReleaseAllHandles();
|
2025-10-10 15:51:49 +02:00
|
|
|
}
|
2025-10-13 09:22:18 +02:00
|
|
|
|
2025-10-10 15:51:49 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Plays a cinematic from an object reference and returns the PlayableDirector playing the timeline
|
|
|
|
|
/// </summary>
|
|
|
|
|
public PlayableDirector PlayCinematic(PlayableAsset assetToPlay)
|
|
|
|
|
{
|
2025-10-13 14:25:11 +02:00
|
|
|
_cinematicSprites.enabled = true;
|
2025-10-10 15:51:49 +02:00
|
|
|
playableDirector.stopped += OnPlayableDirectorStopped;
|
|
|
|
|
playableDirector.Play(assetToPlay);
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug("Playing cinematic " + assetToPlay.name);
|
2025-10-13 14:25:11 +02:00
|
|
|
_isCinematicPlaying = true;
|
2025-10-13 12:41:04 +02:00
|
|
|
OnCinematicStarted?.Invoke();
|
2025-10-10 15:51:49 +02:00
|
|
|
return playableDirector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnPlayableDirectorStopped(PlayableDirector director)
|
|
|
|
|
{
|
2025-10-13 14:25:11 +02:00
|
|
|
_cinematicSprites.enabled = false;
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug("Cinematic stopped!");
|
2025-10-13 14:25:11 +02:00
|
|
|
_isCinematicPlaying = false;
|
2025-10-13 12:41:04 +02:00
|
|
|
OnCinematicStopped?.Invoke();
|
2025-10-13 09:22:18 +02:00
|
|
|
// Release the addressable handle associated with this director
|
|
|
|
|
ReleaseAddressableHandle(director);
|
2025-10-10 15:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads a playable from an asset path and plays it as a cinematic
|
|
|
|
|
/// </summary>
|
|
|
|
|
public PlayableDirector LoadAndPlayCinematic(string key)
|
|
|
|
|
{
|
2025-10-13 09:22:18 +02:00
|
|
|
// Load the asset via addressables
|
2025-10-10 15:51:49 +02:00
|
|
|
var handle = Addressables.LoadAssetAsync<PlayableAsset>(key);
|
|
|
|
|
var result = handle.WaitForCompletion();
|
2025-10-13 09:22:18 +02:00
|
|
|
|
|
|
|
|
// Store the handle for later release
|
|
|
|
|
_addressableHandles[playableDirector] = handle;
|
|
|
|
|
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug($"[CinematicsManager] Loaded addressable cinematic: {key}");
|
2025-10-13 09:22:18 +02:00
|
|
|
|
2025-10-10 15:51:49 +02:00
|
|
|
return PlayCinematic(result);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 09:22:18 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Skips the currently playing cinematic if one is active
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SkipCurrentCinematic()
|
|
|
|
|
{
|
|
|
|
|
if (playableDirector != null && playableDirector.state == PlayState.Playing)
|
|
|
|
|
{
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug("Skipping current cinematic");
|
2025-10-13 09:22:18 +02:00
|
|
|
playableDirector.Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-10 10:07:08 +02:00
|
|
|
|
2025-10-13 09:22:18 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Releases the addressable handle associated with a specific PlayableDirector
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ReleaseAddressableHandle(PlayableDirector director)
|
|
|
|
|
{
|
|
|
|
|
if (_addressableHandles.TryGetValue(director, out var handle))
|
|
|
|
|
{
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug($"[CinematicsManager] Releasing addressable handle for cinematic");
|
2025-10-13 09:22:18 +02:00
|
|
|
Addressables.Release(handle);
|
|
|
|
|
_addressableHandles.Remove(director);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases all active addressable handles
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ReleaseAllHandles()
|
2025-10-10 15:51:49 +02:00
|
|
|
{
|
2025-10-13 09:22:18 +02:00
|
|
|
foreach (var handle in _addressableHandles.Values)
|
|
|
|
|
{
|
|
|
|
|
if (handle.IsValid())
|
|
|
|
|
{
|
|
|
|
|
Addressables.Release(handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_addressableHandles.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 14:25:11 +02:00
|
|
|
private void Awake()
|
2025-10-13 09:22:18 +02:00
|
|
|
{
|
2025-10-13 14:25:11 +02:00
|
|
|
PlayStartCinematicOnGameLoad();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads a cinematic asynchronously while showing a loading screen, then plays it
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">The addressable key of the cinematic to load</param>
|
|
|
|
|
/// <returns>The PlayableDirector playing the cinematic</returns>
|
|
|
|
|
public async System.Threading.Tasks.Task<PlayableDirector> PlayCinematicWithLoadingScreen(string key)
|
|
|
|
|
{
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug($"[CinematicsManager] Preparing to load cinematic with loading screen: {key}");
|
2025-10-13 09:22:18 +02:00
|
|
|
|
2025-10-13 14:25:11 +02:00
|
|
|
// First, show the loading screen BEFORE creating any async operations
|
|
|
|
|
UI.LoadingScreenController.Instance.ShowLoadingScreen();
|
|
|
|
|
|
|
|
|
|
// Give the loading screen a frame to render
|
|
|
|
|
await System.Threading.Tasks.Task.Yield();
|
|
|
|
|
|
|
|
|
|
// Now create the load handle and track its progress
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug($"[CinematicsManager] Starting cinematic asset load: {key}");
|
2025-10-13 14:25:11 +02:00
|
|
|
AsyncOperationHandle<PlayableAsset> handle = Addressables.LoadAssetAsync<PlayableAsset>(key);
|
|
|
|
|
|
|
|
|
|
// Update the loading screen with the progress provider after the handle is created
|
|
|
|
|
UI.LoadingScreenController.Instance.ShowLoadingScreen(() => handle.PercentComplete);
|
|
|
|
|
|
|
|
|
|
// Wait for the loading to complete
|
|
|
|
|
var result = await handle.Task;
|
|
|
|
|
|
|
|
|
|
// Store the handle for later release
|
|
|
|
|
_addressableHandles[playableDirector] = handle;
|
|
|
|
|
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug($"[CinematicsManager] Cinematic loaded: {key}");
|
2025-10-13 14:25:11 +02:00
|
|
|
|
|
|
|
|
// Hide the loading screen
|
|
|
|
|
UI.LoadingScreenController.Instance.HideLoadingScreen();
|
|
|
|
|
|
|
|
|
|
// Important: Wait for the loading screen to be fully hidden before playing the cinematic
|
|
|
|
|
await UI.LoadingScreenController.Instance.WaitForLoadingScreenToHideAsync();
|
|
|
|
|
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug($"[CinematicsManager] Loading screen hidden, now playing cinematic: {key}");
|
2025-10-13 14:25:11 +02:00
|
|
|
|
|
|
|
|
// Play the cinematic
|
|
|
|
|
return PlayCinematic(result);
|
|
|
|
|
}
|
2025-10-10 17:26:55 +02:00
|
|
|
|
2025-10-13 14:25:11 +02:00
|
|
|
private async void PlayStartCinematicOnGameLoad()
|
|
|
|
|
{
|
2025-10-10 17:26:55 +02:00
|
|
|
if (!SceneManager.GetActiveScene().name.ToLower().Contains("mainmenu"))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-10-13 14:25:11 +02:00
|
|
|
|
|
|
|
|
_instance = this;
|
2025-10-10 17:26:55 +02:00
|
|
|
|
2025-10-10 15:51:49 +02:00
|
|
|
playableDirector = GetComponent<PlayableDirector>();
|
2025-10-13 14:25:11 +02:00
|
|
|
_cinematicSprites = GetComponentInChildren<Image>(true);
|
|
|
|
|
|
|
|
|
|
// Use the new method with loading screen instead of direct load
|
|
|
|
|
await PlayCinematicWithLoadingScreen("IntroSequence");
|
2025-10-10 15:51:49 +02:00
|
|
|
}
|
2025-10-10 10:07:08 +02:00
|
|
|
}
|
|
|
|
|
}
|