SoundCtrl.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. namespace Sound
  3. {
  4. [RequireComponent(typeof(AudioSource))]
  5. public class SoundCtrl : MonoBehaviour
  6. {
  7. public const string CodeBind = "动态绑定";
  8. public const string HallBgmPlayer = "大厅背景音乐播放器";
  9. public const string HallEnvPlayer = "大厅环境音效播放器";
  10. public const string BattleBgmPlayer = "战斗背景音乐播放器";
  11. public const string BattleEnvPlayer = "战斗环境音效播放器";
  12. [Dropdown(CodeBind, HallBgmPlayer, HallEnvPlayer, BattleBgmPlayer, BattleEnvPlayer)]
  13. public string Tag;
  14. private AudioSource _audioSource;
  15. private void Start()
  16. {
  17. _audioSource = gameObject.GetComponent<AudioSource>();
  18. SoundCore.Instance.OnPlaySoundEvent += OnPlaySound;
  19. SoundCore.Instance.OnMuteChange += OnMuteChange;
  20. }
  21. private void OnMuteChange(bool mute)
  22. {
  23. _audioSource.volume = mute ? 0f : 1f;
  24. }
  25. private void OnPlaySound(AudioClip clip, string tag, bool play, bool loop)
  26. {
  27. if (Tag == tag)
  28. {
  29. _audioSource.clip = clip;
  30. _audioSource.loop = loop;
  31. if (play)
  32. {
  33. _audioSource.Play();
  34. }
  35. else
  36. {
  37. _audioSource.Stop();
  38. }
  39. }
  40. }
  41. private void OnDestroy()
  42. {
  43. SoundCore.Instance.OnPlaySoundEvent -= OnPlaySound;
  44. SoundCore.Instance.OnMuteChange -= OnMuteChange;
  45. }
  46. }
  47. }