超大量的更新 修复很多问题,gameplay特效初步
This commit is contained in:
@@ -80,11 +80,15 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
if (!stopOnGameplayScene)
|
||||
return;
|
||||
|
||||
if (scene.name == gameplaySceneName)
|
||||
if (stopOnGameplayScene && scene.name == gameplaySceneName)
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure lobby/select scenes resume BGM after returning from gameplay.
|
||||
if (autoPlay)
|
||||
EnsurePlaying();
|
||||
}
|
||||
|
||||
public void EnsurePlaying()
|
||||
|
||||
@@ -3,11 +3,14 @@ using UnityEngine.SceneManagement;
|
||||
|
||||
public static class BgmRuntimeBootstrap
|
||||
{
|
||||
private const string RuntimeAudioListenerName = "__RuntimeAudioListener";
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
|
||||
private static void Bootstrap()
|
||||
{
|
||||
var mgr = BgmPlaybackManager.EnsureInstance();
|
||||
mgr.EnsurePlaying();
|
||||
EnsureAudioListener(SceneManager.GetActiveScene());
|
||||
|
||||
// Ensure UI binder exists
|
||||
if (Object.FindAnyObjectByType<BgmUiBinder>() == null)
|
||||
@@ -24,6 +27,7 @@ public static class BgmRuntimeBootstrap
|
||||
|
||||
private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
EnsureAudioListener(scene);
|
||||
RemoveBgmParticlesInLoadedScenes();
|
||||
}
|
||||
|
||||
@@ -48,4 +52,110 @@ public static class BgmRuntimeBootstrap
|
||||
Object.Destroy(t.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private static void EnsureAudioListener(Scene scene)
|
||||
{
|
||||
if (!scene.IsValid() || !scene.isLoaded)
|
||||
return;
|
||||
|
||||
AudioListener[] allListeners = Object.FindObjectsByType<AudioListener>(FindObjectsInactive.Include, FindObjectsSortMode.None);
|
||||
|
||||
AudioListener activeEnabledInScene = null;
|
||||
AudioListener activeInScene = null;
|
||||
AudioListener anyInScene = null;
|
||||
|
||||
for (int i = 0; i < allListeners.Length; i++)
|
||||
{
|
||||
AudioListener listener = allListeners[i];
|
||||
if (listener == null)
|
||||
continue;
|
||||
if (!listener.gameObject.scene.IsValid() || listener.gameObject.scene != scene)
|
||||
continue;
|
||||
|
||||
if (anyInScene == null)
|
||||
anyInScene = listener;
|
||||
|
||||
if (listener.gameObject.activeInHierarchy && activeInScene == null)
|
||||
activeInScene = listener;
|
||||
|
||||
if (listener.gameObject.activeInHierarchy && listener.enabled)
|
||||
{
|
||||
activeEnabledInScene = listener;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (activeEnabledInScene != null)
|
||||
return;
|
||||
|
||||
if (activeInScene != null)
|
||||
{
|
||||
activeInScene.enabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (anyInScene != null)
|
||||
{
|
||||
anyInScene.enabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
Camera cameraInScene = FindBestCameraInScene(scene);
|
||||
if (cameraInScene != null)
|
||||
{
|
||||
AudioListener cameraListener = cameraInScene.GetComponent<AudioListener>();
|
||||
if (cameraListener == null)
|
||||
cameraListener = cameraInScene.gameObject.AddComponent<AudioListener>();
|
||||
cameraListener.enabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject listenerRoot = FindNamedObjectInScene(scene, RuntimeAudioListenerName);
|
||||
if (listenerRoot == null)
|
||||
{
|
||||
listenerRoot = new GameObject(RuntimeAudioListenerName);
|
||||
try { SceneManager.MoveGameObjectToScene(listenerRoot, scene); } catch { }
|
||||
}
|
||||
if (listenerRoot.GetComponent<AudioListener>() == null)
|
||||
listenerRoot.AddComponent<AudioListener>();
|
||||
}
|
||||
|
||||
private static Camera FindBestCameraInScene(Scene scene)
|
||||
{
|
||||
Camera[] cameras = Object.FindObjectsByType<Camera>(FindObjectsInactive.Include, FindObjectsSortMode.None);
|
||||
Camera first = null;
|
||||
for (int i = 0; i < cameras.Length; i++)
|
||||
{
|
||||
Camera cam = cameras[i];
|
||||
if (cam == null || cam.gameObject.scene != scene)
|
||||
continue;
|
||||
|
||||
if (first == null)
|
||||
first = cam;
|
||||
|
||||
if (cam.gameObject.activeInHierarchy && cam.enabled && cam.CompareTag("MainCamera"))
|
||||
return cam;
|
||||
}
|
||||
|
||||
if (first != null)
|
||||
return first;
|
||||
return null;
|
||||
}
|
||||
|
||||
private static GameObject FindNamedObjectInScene(Scene scene, string objectName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(objectName))
|
||||
return null;
|
||||
|
||||
Transform[] all = Object.FindObjectsByType<Transform>(FindObjectsInactive.Include, FindObjectsSortMode.None);
|
||||
for (int i = 0; i < all.Length; i++)
|
||||
{
|
||||
Transform t = all[i];
|
||||
if (t == null || t.gameObject.scene != scene)
|
||||
continue;
|
||||
if (string.Equals(t.name, objectName, System.StringComparison.OrdinalIgnoreCase))
|
||||
return t.gameObject;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,9 @@ public class BgmUiBinder : MonoBehaviour
|
||||
public bool forceHideOnSceneLoad = true;
|
||||
public string lobbySceneName = "UI_UI";
|
||||
public string gameplaySceneName = "gamePlay_gamePlay";
|
||||
[Tooltip("Main menu scene name where spectrum should be hidden.")]
|
||||
public string mainSceneName = "Main_main";
|
||||
public bool hideSpectrumInMainScene = true;
|
||||
public float musicPicFadeDuration = 0.3f;
|
||||
public bool useUnscaledTime = true;
|
||||
|
||||
@@ -663,6 +666,9 @@ public class BgmUiBinder : MonoBehaviour
|
||||
if (viz != null)
|
||||
viz.enabled = true;
|
||||
CleanupSpectrumRoots(false);
|
||||
|
||||
// If this scene should not show spectrum (e.g. gameplay or main menu), hide after ensuring.
|
||||
HideSpectrumInGameplay(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
|
||||
private void EnsureMusicPicCanvas()
|
||||
@@ -867,8 +873,9 @@ public class BgmUiBinder : MonoBehaviour
|
||||
|
||||
private void HideSpectrumInGameplay(string sceneName)
|
||||
{
|
||||
if (sceneName != gameplaySceneName)
|
||||
return;
|
||||
bool shouldHide = sceneName == gameplaySceneName ||
|
||||
(hideSpectrumInMainScene && sceneName == mainSceneName);
|
||||
if (!shouldHide) return;
|
||||
|
||||
var all = Resources.FindObjectsOfTypeAll<RectTransform>();
|
||||
for (int i = 0; i < all.Length; i++)
|
||||
@@ -938,4 +945,3 @@ public class BgmUiBinder : MonoBehaviour
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user