Working progress bar from boot into main menu
This commit is contained in:
@@ -14,9 +14,16 @@ namespace Bootstrap
|
||||
[SerializeField] private string mainMenuSceneName = "MainMenu";
|
||||
[SerializeField] private float minDelayAfterBoot = 0.5f; // Small delay after boot to ensure smooth transition
|
||||
[SerializeField] private bool debugMode = false;
|
||||
|
||||
|
||||
// Progress distribution between bootstrap and scene loading
|
||||
[SerializeField, Range(0.1f, 0.9f)] private float bootProgressWeight = 0.5f; // Default 50/50 split
|
||||
|
||||
private enum LoadingPhase { Bootstrap, SceneLoading }
|
||||
private LoadingPhase _currentPhase = LoadingPhase.Bootstrap;
|
||||
|
||||
private bool _bootComplete = false;
|
||||
private bool _hasStartedLoading = false;
|
||||
private float _sceneLoadingProgress = 0f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -29,12 +36,12 @@ namespace Bootstrap
|
||||
return;
|
||||
}
|
||||
|
||||
// Show the loading screen immediately
|
||||
// Show the loading screen immediately with our combined progress provider
|
||||
LoadingScreenController.Instance.ShowLoadingScreen(
|
||||
// Use the CustomBoot progress as the progress provider
|
||||
progressProvider: () => CustomBoot.CurrentProgress,
|
||||
// When loading screen is fully hidden, load main menu
|
||||
onComplete: () => LoadMainMenu()
|
||||
progressProvider: GetCombinedProgress,
|
||||
onComplete: () => {
|
||||
Debug.Log("[BootSceneController] Loading screen fully hidden, boot sequence completed");
|
||||
}
|
||||
);
|
||||
|
||||
// Start the boot process if not already initialized
|
||||
@@ -73,17 +80,38 @@ namespace Bootstrap
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Progress provider that combines bootstrap and scene loading progress
|
||||
/// </summary>
|
||||
private float GetCombinedProgress()
|
||||
{
|
||||
switch (_currentPhase)
|
||||
{
|
||||
case LoadingPhase.Bootstrap:
|
||||
// Scale bootstrap progress from 0 to bootProgressWeight
|
||||
return CustomBoot.CurrentProgress * bootProgressWeight;
|
||||
|
||||
case LoadingPhase.SceneLoading:
|
||||
// Scale scene loading progress from bootProgressWeight to 1.0
|
||||
return bootProgressWeight + (_sceneLoadingProgress * (1f - bootProgressWeight));
|
||||
|
||||
default:
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnBootProgressChanged(float progress)
|
||||
{
|
||||
if (debugMode)
|
||||
{
|
||||
Debug.Log($"[BootSceneController] Boot progress: {progress:P0}");
|
||||
Debug.Log($"[BootSceneController] Bootstrap progress: {progress:P0}, Combined: {GetCombinedProgress():P0}");
|
||||
}
|
||||
}
|
||||
|
||||
private void LogDebugInfo()
|
||||
{
|
||||
Debug.Log($"[BootSceneController] Debug - Boot Progress: {CustomBoot.CurrentProgress:P0}, Boot Complete: {_bootComplete}, Loading Started: {_hasStartedLoading}");
|
||||
Debug.Log($"[BootSceneController] Debug - Phase: {_currentPhase}, Bootstrap: {CustomBoot.CurrentProgress:P0}, " +
|
||||
$"Scene: {_sceneLoadingProgress:P0}, Combined: {GetCombinedProgress():P0}, Boot Complete: {_bootComplete}");
|
||||
}
|
||||
|
||||
private void OnBootCompleted()
|
||||
@@ -94,34 +122,56 @@ namespace Bootstrap
|
||||
Debug.Log("[BootSceneController] Boot process completed");
|
||||
_bootComplete = true;
|
||||
|
||||
// After a small delay, tell the loading screen we're done
|
||||
// After a small delay, start loading the main menu
|
||||
// This prevents jerky transitions if boot happens very quickly
|
||||
Invoke(nameof(CompleteLoadingScreen), minDelayAfterBoot);
|
||||
Invoke(nameof(StartLoadingMainMenu), minDelayAfterBoot);
|
||||
}
|
||||
|
||||
private void CompleteLoadingScreen()
|
||||
private void StartLoadingMainMenu()
|
||||
{
|
||||
Debug.Log("[BootSceneController] Hiding loading screen");
|
||||
|
||||
// Tell loading screen that loading is complete
|
||||
if (LoadingScreenController.Instance != null)
|
||||
{
|
||||
LoadingScreenController.Instance.HideLoadingScreen();
|
||||
}
|
||||
}
|
||||
|
||||
private async void LoadMainMenu()
|
||||
{
|
||||
// Prevent multiple scene loads
|
||||
if (_hasStartedLoading)
|
||||
return;
|
||||
|
||||
_hasStartedLoading = true;
|
||||
_currentPhase = LoadingPhase.SceneLoading;
|
||||
LoadMainMenu();
|
||||
}
|
||||
|
||||
private async void LoadMainMenu()
|
||||
{
|
||||
Debug.Log($"[BootSceneController] Loading main menu scene: {mainMenuSceneName}");
|
||||
|
||||
// Load the main menu scene
|
||||
var progress = new Progress<float>(p => Debug.Log($"Loading main menu: {p * 100:F0}%"));
|
||||
await SceneManagerService.Instance.SwitchSceneAsync(mainMenuSceneName, progress);
|
||||
try
|
||||
{
|
||||
// Initialize scene loading progress to 0 to ensure proper remapping
|
||||
_sceneLoadingProgress = 0f;
|
||||
|
||||
// Create a progress object that remaps scene loading progress (0-1) to our second phase range
|
||||
var progress = new Progress<float>(p => {
|
||||
// Store the raw scene loading progress (0-1)
|
||||
_sceneLoadingProgress = p;
|
||||
|
||||
if (debugMode)
|
||||
{
|
||||
Debug.Log($"[BootSceneController] Scene loading raw: {p:P0}, Combined: {GetCombinedProgress():P0}");
|
||||
}
|
||||
});
|
||||
|
||||
// Load the scene but don't auto-hide loading screen (we're managing that ourselves)
|
||||
await SceneManagerService.Instance.SwitchSceneAsync(mainMenuSceneName, progress, autoHideLoadingScreen: false);
|
||||
|
||||
// Ensure progress is complete
|
||||
_sceneLoadingProgress = 1f;
|
||||
|
||||
// Scene is fully loaded, we can now hide the loading screen
|
||||
LoadingScreenController.Instance.HideLoadingScreen();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[BootSceneController] Error loading main menu: {e.Message}");
|
||||
// Still try to hide the loading screen even if there was an error
|
||||
LoadingScreenController.Instance.HideLoadingScreen();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user