using UnityEngine; using UnityEngine.SceneManagement; public static class BgmRuntimeBootstrap { [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] private static void Bootstrap() { var mgr = BgmPlaybackManager.EnsureInstance(); mgr.EnsurePlaying(); // Ensure UI binder exists if (Object.FindAnyObjectByType() == null) { var go = new GameObject("BGM_UI_Binder"); go.AddComponent(); Object.DontDestroyOnLoad(go); } RemoveBgmParticlesInLoadedScenes(); SceneManager.sceneLoaded -= OnSceneLoaded; SceneManager.sceneLoaded += OnSceneLoaded; } private static void OnSceneLoaded(Scene scene, LoadSceneMode mode) { RemoveBgmParticlesInLoadedScenes(); } private static void RemoveBgmParticlesInLoadedScenes() { var emitters = Object.FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None); for (int i = 0; i < emitters.Length; i++) { var e = emitters[i]; if (e == null) continue; if (!e.gameObject.scene.IsValid() || !e.gameObject.scene.isLoaded) continue; Object.Destroy(e.gameObject); } var all = Resources.FindObjectsOfTypeAll(); for (int i = 0; i < all.Length; i++) { var t = all[i]; if (t == null) continue; if (!t.gameObject.scene.IsValid() || !t.gameObject.scene.isLoaded) continue; if (t.name.Equals("BgmParticles", System.StringComparison.OrdinalIgnoreCase)) Object.Destroy(t.gameObject); } } }