using System; using System.Collections.Generic; using Plugins.CxShine.Singleton; using UnityEngine; namespace Sound { public class SoundCore : ScriptSingleton { private const string LocalStoreMute = "LocalMute"; private readonly Dictionary _sounds = new Dictionary(); private readonly Dictionary _soundRes = new Dictionary(); private bool _isMute; public bool IsMute { get => _isMute; } public delegate void OnPlaySoundDelegate(AudioClip clip, string tag, bool play, bool loop); public event OnPlaySoundDelegate OnPlaySoundEvent; public Action OnMuteChange; public SoundCore() { _isMute = PlayerPrefs.GetInt(LocalStoreMute) != 0; _sounds.Add(SoundType.HallBgmMain, "Sound/battle_bgm"); _sounds.Add(SoundType.BattleBgmMain, "Sound/battle_bgm"); _sounds.Add(SoundType.BattleBgmStart, "Sound/battle_start"); _sounds.Add(SoundType.CockJumpAttack, "Sound/cock_jump_attack"); _sounds.Add(SoundType.SoundClick, "Sound/sound_click"); foreach (var sound in _sounds) { var clip = Resources.Load(sound.Value); _soundRes.Add(sound.Key, clip); } } public void PlaySound(SoundType soundType, string tag, bool play = true, bool loop = false) { if (IsMute) return; _soundRes.TryGetValue(soundType, out var clip); if (clip == null) return; OnPlaySoundEvent?.Invoke(clip, tag, play,loop); } public void Mute(bool mute) { _isMute = mute; PlayerPrefs.SetInt(LocalStoreMute, mute ? 1 : 0); OnMuteChange?.Invoke(_isMute); } } public enum SoundType { HallBgmMain, BattleBgmMain, BattleBgmStart, SoundClick, CockJumpAttack, } }