Add skip functionality to Cinematics Manager

This commit is contained in:
Michal Pikulski
2025-10-13 09:22:18 +02:00
parent f150ad0ce4
commit ab84c97675
11 changed files with 753 additions and 22 deletions

View File

@@ -1,14 +1,12 @@
using Bootstrap;
using System;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.InputSystem;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.Playables;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace CinematicsM
namespace Cinematics
{
/// <summary>
/// Handles loading, playing and unloading cinematics
@@ -20,6 +18,10 @@ namespace CinematicsM
private Image cinematicSprites;
public PlayableAsset cinematicToPlay;
// Dictionary to track addressable handles by PlayableDirector
private Dictionary<PlayableDirector, AsyncOperationHandle<PlayableAsset>> _addressableHandles
= new Dictionary<PlayableDirector, AsyncOperationHandle<PlayableAsset>>();
public static CinematicsManager Instance
{
get
@@ -41,8 +43,24 @@ namespace CinematicsM
private void OnEnable()
{
// 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();
}
private void OnApplicationQuit()
{
ReleaseAllHandles();
}
/// <summary>
/// Plays a cinematic from an object reference and returns the PlayableDirector playing the timeline
/// </summary>
@@ -59,6 +77,9 @@ namespace CinematicsM
{
cinematicSprites.enabled = false;
Debug.Log("Cinematic stopped!");
// Release the addressable handle associated with this director
ReleaseAddressableHandle(director);
}
/// <summary>
@@ -66,18 +87,73 @@ namespace CinematicsM
/// </summary>
public PlayableDirector LoadAndPlayCinematic(string key)
{
// Load the asset via addressables
var handle = Addressables.LoadAssetAsync<PlayableAsset>(key);
var result = handle.WaitForCompletion();
// Store the handle for later release
_addressableHandles[playableDirector] = handle;
Debug.Log($"[CinematicsManager] Loaded addressable cinematic: {key}");
return PlayCinematic(result);
}
// Update is called once per frame
void Update()
{
}
private void Awake()
/// <summary>
/// Skips the currently playing cinematic if one is active
/// </summary>
public void SkipCurrentCinematic()
{
if (playableDirector != null && playableDirector.state == PlayState.Playing)
{
Debug.Log("Skipping current cinematic");
playableDirector.Stop();
}
}
/// <summary>
/// Checks if a cinematic is currently playing
/// </summary>
public bool IsCinematicPlaying()
{
return playableDirector != null && playableDirector.state == PlayState.Playing;
}
/// <summary>
/// Releases the addressable handle associated with a specific PlayableDirector
/// </summary>
private void ReleaseAddressableHandle(PlayableDirector director)
{
if (_addressableHandles.TryGetValue(director, out var handle))
{
Debug.Log($"[CinematicsManager] Releasing addressable handle for cinematic");
Addressables.Release(handle);
_addressableHandles.Remove(director);
}
}
/// <summary>
/// Releases all active addressable handles
/// </summary>
private void ReleaseAllHandles()
{
foreach (var handle in _addressableHandles.Values)
{
if (handle.IsValid())
{
Addressables.Release(handle);
}
}
_addressableHandles.Clear();
}
private void Start()
{
if (!SceneManager.GetActiveScene().name.ToLower().Contains("mainmenu"))
{
return;
}
_instance = this;
playableDirector = GetComponent<PlayableDirector>();
cinematicSprites = GetComponentInChildren<Image>(true);