UI换了很多,spine主视觉停用
This commit is contained in:
@@ -11,10 +11,16 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
{
|
||||
public static BgmPlaybackManager Instance { get; private set; }
|
||||
|
||||
private const string MainSceneName = "Main_main";
|
||||
private const string GameplaySceneName = "gamePlay_gamePlay";
|
||||
private const string BeforeEverythingSceneName = "beforeEverything";
|
||||
|
||||
[Header("Audio")]
|
||||
public AudioSource audioSource;
|
||||
public AudioMixerGroup outputMixerGroup;
|
||||
public AudioClip clip;
|
||||
[Tooltip("Optional Music_Data resource path for the lobby BGM. Example: Data/Music/allAlong")]
|
||||
public string musicDataResourcePath = "Data/Music/allAlong";
|
||||
[Tooltip("Optional playlist. If empty, will load all clips in Resources/BGM.")]
|
||||
public List<AudioClip> playlist = new List<AudioClip>();
|
||||
public int startIndex = 0;
|
||||
@@ -29,6 +35,7 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
|
||||
private bool isLoading = false;
|
||||
private int currentIndex = 0;
|
||||
private Coroutine loadClipRoutine;
|
||||
|
||||
public static BgmPlaybackManager EnsureInstance()
|
||||
{
|
||||
@@ -38,6 +45,12 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
return Instance;
|
||||
}
|
||||
|
||||
public void SetOutputMixerGroup(AudioMixerGroup mixerGroup)
|
||||
{
|
||||
outputMixerGroup = mixerGroup;
|
||||
ApplyOutputMixerGroup();
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
@@ -51,8 +64,7 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
if (audioSource == null)
|
||||
audioSource = gameObject.AddComponent<AudioSource>();
|
||||
|
||||
if (outputMixerGroup != null)
|
||||
audioSource.outputAudioMixerGroup = outputMixerGroup;
|
||||
ApplyOutputMixerGroup();
|
||||
|
||||
audioSource.playOnAwake = false;
|
||||
audioSource.loop = loop;
|
||||
@@ -66,11 +78,14 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
currentIndex = Mathf.Clamp(startIndex, 0, playlist.Count - 1);
|
||||
clip = playlist[currentIndex];
|
||||
}
|
||||
else if (clip == null)
|
||||
{
|
||||
ResolveDefaultClip();
|
||||
}
|
||||
if (clip != null)
|
||||
audioSource.clip = clip;
|
||||
|
||||
if (autoPlay)
|
||||
EnsurePlaying();
|
||||
SyncPlaybackForScene(SceneManager.GetActiveScene());
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
@@ -81,34 +96,83 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
private void OnDisable()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
StopLobbyAudioAndParticles();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
if (Instance == this)
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
if (stopOnGameplayScene && scene.name == gameplaySceneName)
|
||||
SyncPlaybackForScene(scene);
|
||||
}
|
||||
|
||||
private void OnApplicationQuit()
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
}
|
||||
|
||||
private void SyncPlaybackForScene(Scene scene)
|
||||
{
|
||||
if (!ShouldPlayInScene(scene.name))
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure lobby/select scenes resume BGM after returning from gameplay.
|
||||
if (autoPlay)
|
||||
// Allowed scenes should keep the lobby BGM alive.
|
||||
if (audioSource == null || !audioSource.isPlaying)
|
||||
EnsurePlaying();
|
||||
}
|
||||
|
||||
private bool ShouldPlayInScene(string sceneName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sceneName))
|
||||
return false;
|
||||
|
||||
if (string.Equals(sceneName, GameplaySceneName, StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
if (string.Equals(sceneName, MainSceneName, StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
if (string.Equals(sceneName, BeforeEverythingSceneName, StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void EnsurePlaying()
|
||||
{
|
||||
if (!ShouldPlayInScene(SceneManager.GetActiveScene().name))
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
return;
|
||||
}
|
||||
|
||||
if (audioSource == null)
|
||||
audioSource = gameObject.AddComponent<AudioSource>();
|
||||
|
||||
if (outputMixerGroup != null)
|
||||
audioSource.outputAudioMixerGroup = outputMixerGroup;
|
||||
ApplyOutputMixerGroup();
|
||||
|
||||
audioSource.loop = loop;
|
||||
|
||||
if (audioSource.clip == null && clip != null)
|
||||
audioSource.clip = clip;
|
||||
|
||||
if (audioSource.clip == null)
|
||||
{
|
||||
ResolveDefaultClip();
|
||||
if (audioSource.clip == null && clip != null)
|
||||
audioSource.clip = clip;
|
||||
}
|
||||
|
||||
if (audioSource.clip == null)
|
||||
{
|
||||
EnsurePlaylist();
|
||||
@@ -121,7 +185,9 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
|
||||
if (audioSource.clip == null && !isLoading)
|
||||
{
|
||||
StartCoroutine(LoadClipRoutine());
|
||||
if (loadClipRoutine != null)
|
||||
StopCoroutine(loadClipRoutine);
|
||||
loadClipRoutine = StartCoroutine(LoadClipRoutine());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -133,6 +199,11 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
{
|
||||
isLoading = true;
|
||||
|
||||
if (audioSource.clip == null)
|
||||
{
|
||||
ResolveDefaultClip();
|
||||
}
|
||||
|
||||
// Try Resources first
|
||||
if (audioSource.clip == null && !string.IsNullOrEmpty(resourcesPath))
|
||||
{
|
||||
@@ -149,9 +220,9 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
string path = fallbackFilePath;
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
// Default to project root /??/Bansonic OST.mp3
|
||||
// Default to the checked-in lobby track in Assets/BGM.
|
||||
var projectRoot = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
|
||||
path = Path.Combine(projectRoot, "??", "Bansonic OST.mp3");
|
||||
path = Path.Combine(projectRoot, "Assets", "BGM", "Bansonic OST.mp3");
|
||||
}
|
||||
|
||||
if (File.Exists(path))
|
||||
@@ -170,8 +241,19 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
|
||||
isLoading = false;
|
||||
|
||||
if (autoPlay && audioSource.clip != null && !audioSource.isPlaying)
|
||||
if (!ShouldPlayInScene(SceneManager.GetActiveScene().name))
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (audioSource.clip != null && !audioSource.isPlaying)
|
||||
audioSource.Play();
|
||||
|
||||
if (loadClipRoutine != null)
|
||||
{
|
||||
loadClipRoutine = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsurePlaylist()
|
||||
@@ -181,6 +263,12 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
|
||||
if (playlist.Count == 0)
|
||||
{
|
||||
if (ResolveDefaultClip(out AudioClip defaultClip))
|
||||
{
|
||||
playlist.Add(defaultClip);
|
||||
return;
|
||||
}
|
||||
|
||||
var clips = Resources.LoadAll<AudioClip>("BGM");
|
||||
if (clips != null && clips.Length > 0)
|
||||
{
|
||||
@@ -190,8 +278,60 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveDefaultClip()
|
||||
{
|
||||
if (clip != null)
|
||||
{
|
||||
audioSource.clip = clip;
|
||||
return;
|
||||
}
|
||||
|
||||
if (ResolveDefaultClip(out AudioClip resolved))
|
||||
{
|
||||
clip = resolved;
|
||||
audioSource.clip = resolved;
|
||||
}
|
||||
}
|
||||
|
||||
private bool ResolveDefaultClip(out AudioClip resolved)
|
||||
{
|
||||
resolved = null;
|
||||
|
||||
if (!string.IsNullOrEmpty(musicDataResourcePath))
|
||||
{
|
||||
var musicData = Resources.Load<Music_Data>(musicDataResourcePath);
|
||||
if (musicData != null && musicData.Clip != null)
|
||||
{
|
||||
resolved = musicData.Clip;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
var musicDatas = Resources.LoadAll<Music_Data>("Data/Music");
|
||||
if (musicDatas != null)
|
||||
{
|
||||
for (int i = 0; i < musicDatas.Length; i++)
|
||||
{
|
||||
var data = musicDatas[i];
|
||||
if (data != null && data.Clip != null)
|
||||
{
|
||||
resolved = data.Clip;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void PlayIndex(int index)
|
||||
{
|
||||
if (!ShouldPlayInScene(SceneManager.GetActiveScene().name))
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
return;
|
||||
}
|
||||
|
||||
EnsurePlaylist();
|
||||
if (playlist == null || playlist.Count == 0)
|
||||
return;
|
||||
@@ -224,6 +364,11 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
{
|
||||
if (audioSource == null || audioSource.clip == null)
|
||||
return;
|
||||
if (!ShouldPlayInScene(SceneManager.GetActiveScene().name))
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
return;
|
||||
}
|
||||
if (audioSource.isPlaying)
|
||||
audioSource.Pause();
|
||||
else
|
||||
@@ -232,9 +377,27 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
|
||||
private void StopLobbyAudioAndParticles()
|
||||
{
|
||||
if (audioSource != null)
|
||||
audioSource.Stop();
|
||||
if (loadClipRoutine != null)
|
||||
{
|
||||
StopCoroutine(loadClipRoutine);
|
||||
loadClipRoutine = null;
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
|
||||
if (audioSource != null)
|
||||
{
|
||||
audioSource.Stop();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ApplyOutputMixerGroup()
|
||||
{
|
||||
if (audioSource != null)
|
||||
{
|
||||
audioSource.outputAudioMixerGroup = outputMixerGroup;
|
||||
}
|
||||
}
|
||||
|
||||
public float CurrentTime => audioSource != null ? audioSource.time : 0f;
|
||||
|
||||
Reference in New Issue
Block a user