SoundCore.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using Plugins.CxShine.Singleton;
  4. using UnityEngine;
  5. namespace Sound
  6. {
  7. public class SoundCore : ScriptSingleton<SoundCore>
  8. {
  9. private const string LocalStoreMute = "LocalMute";
  10. private readonly Dictionary<SoundType, string> _sounds = new Dictionary<SoundType, string>();
  11. private readonly Dictionary<SoundType, AudioClip> _soundRes = new Dictionary<SoundType, AudioClip>();
  12. private bool _isMute;
  13. public bool IsMute
  14. {
  15. get => _isMute;
  16. }
  17. public delegate void OnPlaySoundDelegate(AudioClip clip, string tag, bool play, bool loop);
  18. public event OnPlaySoundDelegate OnPlaySoundEvent;
  19. public Action<bool> OnMuteChange;
  20. public SoundCore()
  21. {
  22. _isMute = PlayerPrefs.GetInt(LocalStoreMute) != 0;
  23. _sounds.Add(SoundType.HallBgmMain, "Sound/battle_bgm");
  24. _sounds.Add(SoundType.BattleBgmMain, "Sound/battle_bgm");
  25. _sounds.Add(SoundType.BattleBgmStart, "Sound/battle_start");
  26. _sounds.Add(SoundType.CockJumpAttack, "Sound/cock_jump_attack");
  27. _sounds.Add(SoundType.SoundClick, "Sound/sound_click");
  28. foreach (var sound in _sounds)
  29. {
  30. var clip = Resources.Load<AudioClip>(sound.Value);
  31. _soundRes.Add(sound.Key, clip);
  32. }
  33. }
  34. public void PlaySound(SoundType soundType, string tag, bool play = true, bool loop = false)
  35. {
  36. if (IsMute)
  37. return;
  38. _soundRes.TryGetValue(soundType, out var clip);
  39. if (clip == null) return;
  40. OnPlaySoundEvent?.Invoke(clip, tag, play,loop);
  41. }
  42. public void Mute(bool mute)
  43. {
  44. _isMute = mute;
  45. PlayerPrefs.SetInt(LocalStoreMute, mute ? 1 : 0);
  46. OnMuteChange?.Invoke(_isMute);
  47. }
  48. }
  49. public enum SoundType
  50. {
  51. HallBgmMain,
  52. BattleBgmMain,
  53. BattleBgmStart,
  54. SoundClick,
  55. CockJumpAttack,
  56. }
  57. }