123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Collections.Generic;
- using Plugins.CxShine.Singleton;
- using UnityEngine;
- namespace Sound
- {
- public class SoundCore : ScriptSingleton<SoundCore>
- {
- private const string LocalStoreMute = "LocalMute";
- private readonly Dictionary<SoundType, string> _sounds = new Dictionary<SoundType, string>();
- private readonly Dictionary<SoundType, AudioClip> _soundRes = new Dictionary<SoundType, AudioClip>();
- 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<bool> 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<AudioClip>(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,
- }
- }
|