52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
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<BgmUiBinder>() == null)
|
|
{
|
|
var go = new GameObject("BGM_UI_Binder");
|
|
go.AddComponent<BgmUiBinder>();
|
|
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<BgmParticleEmitter>(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<Transform>();
|
|
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);
|
|
}
|
|
}
|
|
}
|