using UnityEngine; namespace Sound { [RequireComponent(typeof(AudioSource))] public class SoundCtrl : MonoBehaviour { public const string CodeBind = "动态绑定"; public const string HallBgmPlayer = "大厅背景音乐播放器"; public const string HallEnvPlayer = "大厅环境音效播放器"; public const string BattleBgmPlayer = "战斗背景音乐播放器"; public const string BattleEnvPlayer = "战斗环境音效播放器"; [Dropdown(CodeBind, HallBgmPlayer, HallEnvPlayer, BattleBgmPlayer, BattleEnvPlayer)] public string Tag; private AudioSource _audioSource; private void Start() { _audioSource = gameObject.GetComponent(); SoundCore.Instance.OnPlaySoundEvent += OnPlaySound; SoundCore.Instance.OnMuteChange += OnMuteChange; } private void OnMuteChange(bool mute) { _audioSource.volume = mute ? 0f : 1f; } private void OnPlaySound(AudioClip clip, string tag, bool play, bool loop) { if (Tag == tag) { _audioSource.clip = clip; _audioSource.loop = loop; if (play) { _audioSource.Play(); } else { _audioSource.Stop(); } } } private void OnDestroy() { SoundCore.Instance.OnPlaySoundEvent -= OnPlaySound; SoundCore.Instance.OnMuteChange -= OnMuteChange; } } }