Finalize by subscribing to the new boot init order

This commit is contained in:
Michal Pikulski
2025-10-16 14:32:07 +02:00
parent 1891aaa85d
commit c7906a9968
6 changed files with 175 additions and 52 deletions

View File

@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using UI;
using UnityEngine;
using UnityEngine.SceneManagement;
using Bootstrap;
namespace Core
{
@@ -48,30 +49,17 @@ namespace Core
private readonly Dictionary<string, AsyncOperation> _activeUnloads = new();
private const string BootstrapSceneName = "BootstrapScene";
void Start()
{
_loadingScreen = LoadingScreenController.Instance;
// Set up loading screen event handlers
SetupLoadingScreenEvents();
}
void Awake()
{
_instance = this;
// DontDestroyOnLoad(gameObject);
#if UNITY_EDITOR
// In Editor, set CurrentGameplayScene to the currently open scene at play start
if (Application.isPlaying)
{
var activeScene = SceneManager.GetActiveScene();
if (activeScene.IsValid())
{
CurrentGameplayScene = activeScene.name;
}
}
#endif
// Initialize current scene tracking immediately in Awake
InitializeCurrentSceneTracking();
// Register for post-boot initialization
BootCompletionService.RegisterInitAction(InitializePostBoot);
// Ensure BootstrapScene is loaded at startup
var bootstrap = SceneManager.GetSceneByName(BootstrapSceneName);
if (!bootstrap.isLoaded)
@@ -79,6 +67,53 @@ namespace Core
SceneManager.LoadScene(BootstrapSceneName, LoadSceneMode.Additive);
}
}
/// <summary>
/// Initialize current scene tracking immediately in Awake
/// This ensures scene management works correctly regardless of boot timing
/// </summary>
private void InitializeCurrentSceneTracking()
{
// Get the active scene and use it as the current gameplay scene
Scene activeScene = SceneManager.GetActiveScene();
if (activeScene.IsValid())
{
// If this is the MainMenu or another gameplay scene, track it
if (activeScene.name != BootstrapSceneName)
{
CurrentGameplayScene = activeScene.name;
Logging.Debug($"[SceneManagerService] Initialized with current scene: {CurrentGameplayScene}");
}
// Otherwise default to MainMenu
else
{
CurrentGameplayScene = "MainMenu";
Logging.Debug($"[SceneManagerService] Initialized with default scene: {CurrentGameplayScene}");
}
}
else
{
CurrentGameplayScene = "MainMenu";
Logging.Debug($"[SceneManagerService] No valid active scene, defaulting to: {CurrentGameplayScene}");
}
}
void Start()
{
// LoadingScreen setup moved to InitializePostBoot
}
private void InitializePostBoot()
{
// Set up loading screen reference and events after boot is complete
_loadingScreen = LoadingScreenController.Instance;
// Set up loading screen event handlers if available
SetupLoadingScreenEvents();
Logging.Debug($"[SceneManagerService] Post-boot initialization complete, current scene is: {CurrentGameplayScene}");
}
private void SetupLoadingScreenEvents()
{