Semi-working intro cinematic with loading screen

This commit is contained in:
Michal Pikulski
2025-10-13 14:25:11 +02:00
parent 65e14c07d2
commit aefff3d050
6 changed files with 181 additions and 50 deletions

View File

@@ -15,11 +15,11 @@ namespace Cinematics
{
public event System.Action OnCinematicStarted;
public event System.Action OnCinematicStopped;
private static CinematicsManager _instance;
private static bool _isQuitting;
private Image cinematicSprites;
public PlayableAsset cinematicToPlay;
private Image _cinematicSprites;
private bool _isCinematicPlaying = false;
public bool IsCinematicPlaying => _isCinematicPlaying;
// Dictionary to track addressable handles by PlayableDirector
private Dictionary<PlayableDirector, AsyncOperationHandle<PlayableAsset>> _addressableHandles
@@ -69,20 +69,21 @@ namespace Cinematics
/// </summary>
public PlayableDirector PlayCinematic(PlayableAsset assetToPlay)
{
cinematicSprites.enabled = true;
_cinematicSprites.enabled = true;
playableDirector.stopped += OnPlayableDirectorStopped;
playableDirector.Play(assetToPlay);
Debug.Log("Playing cinematic " + assetToPlay.name);
_isCinematicPlaying = true;
OnCinematicStarted?.Invoke();
return playableDirector;
}
void OnPlayableDirectorStopped(PlayableDirector director)
{
cinematicSprites.enabled = false;
_cinematicSprites.enabled = false;
Debug.Log("Cinematic stopped!");
_isCinematicPlaying = false;
OnCinematicStopped?.Invoke();
// Release the addressable handle associated with this director
ReleaseAddressableHandle(director);
}
@@ -115,14 +116,6 @@ namespace Cinematics
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
@@ -152,7 +145,54 @@ namespace Cinematics
_addressableHandles.Clear();
}
private void Start()
private void Awake()
{
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)
{
Debug.Log($"[CinematicsManager] Preparing to load cinematic with loading screen: {key}");
// 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
Debug.Log($"[CinematicsManager] Starting cinematic asset load: {key}");
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;
Debug.Log($"[CinematicsManager] Cinematic loaded: {key}");
// 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();
Debug.Log($"[CinematicsManager] Loading screen hidden, now playing cinematic: {key}");
// Play the cinematic
return PlayCinematic(result);
}
private async void PlayStartCinematicOnGameLoad()
{
if (!SceneManager.GetActiveScene().name.ToLower().Contains("mainmenu"))
{
@@ -161,14 +201,11 @@ namespace Cinematics
_instance = this;
if (!SceneManager.GetActiveScene().name.ToLower().Contains("mainmenu"))
{
return;
}
playableDirector = GetComponent<PlayableDirector>();
cinematicSprites = GetComponentInChildren<Image>(true);
LoadAndPlayCinematic("IntroSequence");
_cinematicSprites = GetComponentInChildren<Image>(true);
// Use the new method with loading screen instead of direct load
await PlayCinematicWithLoadingScreen("IntroSequence");
}
}
}