Fix issues with scene loading, introduce an intermediate bootstrap scene
This commit is contained in:
@@ -42,11 +42,29 @@ public class SceneManagerService : MonoBehaviour
|
||||
|
||||
private readonly Dictionary<string, AsyncOperation> _activeLoads = new();
|
||||
private readonly Dictionary<string, AsyncOperation> _activeUnloads = new();
|
||||
private const string BootstrapSceneName = "BootstrapScene";
|
||||
|
||||
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
|
||||
// Ensure BootstrapScene is loaded at startup
|
||||
var bootstrap = SceneManager.GetSceneByName(BootstrapSceneName);
|
||||
if (!bootstrap.isLoaded)
|
||||
{
|
||||
SceneManager.LoadScene(BootstrapSceneName, LoadSceneMode.Additive);
|
||||
}
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
@@ -196,7 +214,7 @@ public class SceneManagerService : MonoBehaviour
|
||||
}
|
||||
|
||||
// Tracks the currently loaded gameplay scene (not persistent/bootstrapper)
|
||||
public string CurrentGameplayScene { get; private set; } = "AppleHillsOverworld";
|
||||
public string CurrentGameplayScene { get; private set; } = "MainMenu";
|
||||
|
||||
// Switches from current gameplay scene to a new one
|
||||
public async Task SwitchSceneAsync(string newSceneName, IProgress<float> progress = null)
|
||||
@@ -210,10 +228,8 @@ public class SceneManagerService : MonoBehaviour
|
||||
else
|
||||
DestroyImmediate(astar.gameObject);
|
||||
}
|
||||
// Load new scene
|
||||
await LoadSceneAsync(newSceneName, progress);
|
||||
// Unload previous scene (if not same)
|
||||
if (!string.IsNullOrEmpty(CurrentGameplayScene) && CurrentGameplayScene != newSceneName)
|
||||
// Unload previous gameplay scene (if not BootstrapScene and not same as new)
|
||||
if (!string.IsNullOrEmpty(CurrentGameplayScene) && CurrentGameplayScene != newSceneName && CurrentGameplayScene != BootstrapSceneName)
|
||||
{
|
||||
var prevScene = SceneManager.GetSceneByName(CurrentGameplayScene);
|
||||
if (prevScene.isLoaded)
|
||||
@@ -225,6 +241,14 @@ public class SceneManagerService : MonoBehaviour
|
||||
Debug.LogWarning($"SceneManagerService: Previous scene '{CurrentGameplayScene}' is not loaded, skipping unload.");
|
||||
}
|
||||
}
|
||||
// Ensure BootstrapScene is loaded before loading new scene
|
||||
var bootstrap = SceneManager.GetSceneByName(BootstrapSceneName);
|
||||
if (!bootstrap.isLoaded)
|
||||
{
|
||||
SceneManager.LoadScene(BootstrapSceneName, LoadSceneMode.Additive);
|
||||
}
|
||||
// Load new gameplay scene
|
||||
await LoadSceneAsync(newSceneName, progress);
|
||||
// Update tracker
|
||||
CurrentGameplayScene = newSceneName;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class MainMenu : MonoBehaviour
|
||||
{
|
||||
public void StartGame()
|
||||
public async void StartGame()
|
||||
{
|
||||
// Replace with the actual scene name as set in Build Settings
|
||||
SceneManager.LoadScene("AppleHillsOverworld");
|
||||
var progress = new Progress<float>(p => Debug.Log($"Loading progress: {p * 100:F0}%"));
|
||||
await SceneManagerService.Instance.SwitchSceneAsync("AppleHillsOverworld", progress);
|
||||
}
|
||||
|
||||
public void QuitGame()
|
||||
|
||||
Reference in New Issue
Block a user