SoundManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using ET;
  2. using ET.Client;
  3. using ET.EventType;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine;
  6. [Event(SceneType.Client)]
  7. public class PlayBGMHandler : BEvent<PlayBgm>
  8. {
  9. protected override async ETTask OnEvent(PlayBgm args)
  10. {
  11. if (args.Play)
  12. {
  13. SoundManager.Instance.PlayBgm(args.Bgm).Coroutine();
  14. }
  15. else
  16. {
  17. SoundManager.Instance.StopBgm();
  18. }
  19. await ETTask.CompletedTask;
  20. }
  21. }
  22. public class SoundManager : Singleton<SoundManager>, ISingletonAwake
  23. {
  24. private AudioSource mMusicBgAudioSource = null;
  25. private AudioSource mSoundAudioSource = null;
  26. public bool BGMute
  27. {
  28. get { return mMusicBgAudioSource.mute; }
  29. set { mMusicBgAudioSource.mute = value; }
  30. }
  31. public float BGVolume
  32. {
  33. get { return mMusicBgAudioSource.volume; }
  34. set { mMusicBgAudioSource.volume = value; }
  35. }
  36. public bool MusicMute
  37. {
  38. get { return mSoundAudioSource.mute; }
  39. set { mSoundAudioSource.mute = value; }
  40. }
  41. public float MusicVolume
  42. {
  43. get { return mSoundAudioSource.volume; }
  44. set { mSoundAudioSource.volume = value; }
  45. }
  46. public void Awake()
  47. {
  48. Log.Debug("初始化SoundManager管理");
  49. var globalObj = GameObject.Find("Global");
  50. GameObject bgMusicObj = new GameObject("BGMusicObj");
  51. bgMusicObj.transform.parent = globalObj.transform;
  52. mMusicBgAudioSource = bgMusicObj.AddComponent<AudioSource>();
  53. mMusicBgAudioSource.loop = true;
  54. mMusicBgAudioSource.volume = 1f;
  55. mMusicBgAudioSource.playOnAwake = false;
  56. var mute = GameSetting.Instance.GetBool(GameSetting.MusicSets.Mute_BG, GameSetting.BGMusicMute);
  57. mMusicBgAudioSource.mute = mute;
  58. GameObject soundMusicObj = new GameObject("SoundMusicObj");
  59. soundMusicObj.transform.parent = globalObj.transform;
  60. mSoundAudioSource = soundMusicObj.AddComponent<AudioSource>();
  61. mSoundAudioSource.loop = false;
  62. mSoundAudioSource.volume = 1f;
  63. mSoundAudioSource.playOnAwake = false;
  64. mute = GameSetting.Instance.GetBool(GameSetting.MusicSets.Mute_Music, GameSetting.MusicMute);
  65. mSoundAudioSource.mute = mute;
  66. }
  67. public async ETTask PlaySound(string name,bool isLoop = false)
  68. {
  69. if (isLoop)
  70. {
  71. await PlayBgm(name);
  72. }
  73. else
  74. {
  75. await PlayUISound(name);
  76. }
  77. }
  78. public async ETTask PlayBgm(string name)
  79. {
  80. if (BGMute) return;
  81. Log.Debug($"play bgm:{name}");
  82. var ac = await GameObjectPool.Instance.AcquireSound(name);
  83. mMusicBgAudioSource.clip = ac;
  84. mMusicBgAudioSource.loop = true;
  85. mMusicBgAudioSource.Play();
  86. }
  87. public void StopBgm()
  88. {
  89. Log.Debug("stop bgm");
  90. mMusicBgAudioSource.Stop();
  91. }
  92. public async ETTask PlayUISound(string name, float duration = -1f)
  93. {
  94. if (MusicMute) return;
  95. var ac = await GameObjectPool.Instance.AcquireSound(name);
  96. mSoundAudioSource.loop = false;
  97. mSoundAudioSource.PlayOneShot(ac);
  98. }
  99. }