From 0057ae9fa35af17250017c5ad20134aa2dffc473 Mon Sep 17 00:00:00 2001 From: Michal Pikulski Date: Fri, 12 Sep 2025 15:37:26 +0200 Subject: [PATCH] Fix issues with scene loading, introduce an intermediate bootstrap scene --- Assets/Scenes/BootstrapScene.unity | 125 +++++++++++++++++++++ Assets/Scenes/BootstrapScene.unity.meta | 7 ++ Assets/Scripts/Core/SceneManagerService.cs | 34 +++++- Assets/Scripts/UI/MainMenu.cs | 6 +- ProjectSettings/EditorBuildSettings.asset | 3 + 5 files changed, 168 insertions(+), 7 deletions(-) create mode 100644 Assets/Scenes/BootstrapScene.unity create mode 100644 Assets/Scenes/BootstrapScene.unity.meta diff --git a/Assets/Scenes/BootstrapScene.unity b/Assets/Scenes/BootstrapScene.unity new file mode 100644 index 00000000..22fbd800 --- /dev/null +++ b/Assets/Scenes/BootstrapScene.unity @@ -0,0 +1,125 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 10 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 3 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 13 + m_BakeOnSceneLoad: 0 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 0 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 1 + m_PVRFilteringGaussRadiusAO: 1 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 20201, guid: 0000000000000000f000000000000000, type: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 3 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + buildHeightMesh: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1660057539 &9223372036854775807 +SceneRoots: + m_ObjectHideFlags: 0 + m_Roots: [] diff --git a/Assets/Scenes/BootstrapScene.unity.meta b/Assets/Scenes/BootstrapScene.unity.meta new file mode 100644 index 00000000..80c16710 --- /dev/null +++ b/Assets/Scenes/BootstrapScene.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 706f6dec7a10d2542be1a3c21216158b +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Core/SceneManagerService.cs b/Assets/Scripts/Core/SceneManagerService.cs index c8b3ca84..db3af600 100644 --- a/Assets/Scripts/Core/SceneManagerService.cs +++ b/Assets/Scripts/Core/SceneManagerService.cs @@ -42,11 +42,29 @@ public class SceneManagerService : MonoBehaviour private readonly Dictionary _activeLoads = new(); private readonly Dictionary _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 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; } diff --git a/Assets/Scripts/UI/MainMenu.cs b/Assets/Scripts/UI/MainMenu.cs index ba76ef0f..59dc1e02 100644 --- a/Assets/Scripts/UI/MainMenu.cs +++ b/Assets/Scripts/UI/MainMenu.cs @@ -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(p => Debug.Log($"Loading progress: {p * 100:F0}%")); + await SceneManagerService.Instance.SwitchSceneAsync("AppleHillsOverworld", progress); } public void QuitGame() diff --git a/ProjectSettings/EditorBuildSettings.asset b/ProjectSettings/EditorBuildSettings.asset index 5f4803c4..6a75120f 100644 --- a/ProjectSettings/EditorBuildSettings.asset +++ b/ProjectSettings/EditorBuildSettings.asset @@ -20,6 +20,9 @@ EditorBuildSettings: - enabled: 1 path: Assets/Scenes/MiniGames/DivingForPictures.unity guid: 0d57c89693f4e964b8b66e00e39ed1e7 + - enabled: 1 + path: Assets/Scenes/BootstrapScene.unity + guid: 706f6dec7a10d2542be1a3c21216158b m_configObjects: com.unity.addressableassets: {fileID: 11400000, guid: ae6ab785ade6a78439b79df6808becd2, type: 2} com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 2bcd2660ca9b64942af0de543d8d7100, type: 3}