Files
test/Assets/PerfectWorld/Scripts/Sound/WorldMusicController.cs
T
vuong dinh hoang f8512de72e deal damage overtime
2026-05-12 16:56:22 +07:00

62 lines
2.0 KiB
C#

using Cysharp.Threading.Tasks;
using UnityEngine;
namespace BrewMonster.Scripts
{
/// <summary>
/// Lives on the persistent "Music" GameObject alongside AudioManager.
/// Waits for the host player to be ready (mirroring TemporaryBackgroundMusic),
/// then loads BGM + ambience clips via Addressables and plays them through AudioManager.
/// </summary>
public class WorldMusicController : MonoBehaviour
{
public static WorldMusicController Instance;
[SerializeField] private WorldMusicDatabaseSO _worldMusicDB;
void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(this);
}
}
/// <summary>
/// Called from LoginScreenUI when the player selects a character.
/// Kicks off the async setup without blocking the caller.
/// </summary>
public void InitForWorld(int worldTag)
{
AudioManager.Instance.StopBGM(1f);
SetupAudioSources(worldTag).Forget();
}
private async UniTask SetupAudioSources(int worldTag)
{
var hostPlayer = CECGameRun.Instance?.GetHostPlayer();
while (hostPlayer == null)
{
await UniTask.DelayFrame(1);
hostPlayer = CECGameRun.Instance?.GetHostPlayer();
}
var entry = _worldMusicDB?.Lookup(worldTag);
if (entry == null)
{
Debug.LogWarning($"[WorldMusicController] No music entry found for worldTag {worldTag}");
return;
}
var bgmClip = await AddressableManager.Instance.LoadAudioClipAsync(entry.bgmPath.ToLower());
var ambienceClip = await AddressableManager.Instance.LoadAudioClipAsync(entry.ambiencePath.ToLower());
AudioManager.Instance.PlayWorldMusic(bgmClip, ambienceClip, 2f);
}
}
}