using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class SoundManager : MonoSingleton { private AudioSource bgmAudioSource = null; private float bgmVolume = 1; private GameObject m_SoundEffectObj = null; private List soundList = new List(); private float soundEffectVolume = 1; public override void InitManager() { base.InitManager(); SetBGMVolume(PlayerPrefs.HasKey("BGMVolume") ? PlayerPrefs.GetFloat("BGMVolume") : 1); SetSoundEffectsVolume(PlayerPrefs.HasKey("SoundEffectVolume") ? PlayerPrefs.GetFloat("SoundEffectVolume") : 1); } private void Update() { for(int i = 0; i < soundList.Count; i++) { if (!soundList[i].isPlaying) { GameObject.Destroy(soundList[i]); soundList.RemoveAt(i); } } } public void PlayBGM(string path) { if (bgmAudioSource == null) { GameObject obj = new GameObject("BGM"); bgmAudioSource = obj.AddComponent(); } AudioClip clip = Resources.Load(path); bgmAudioSource.loop = true; bgmAudioSource.clip = clip; bgmAudioSource.volume = bgmVolume; bgmAudioSource.Play(); } public void PasueBGM() { if (bgmAudioSource == null) { return; } bgmAudioSource.Pause(); } public void ContinueBGM() { if (bgmAudioSource == null) { return; } bgmAudioSource.Pause(); } public void StopBGM() { if (bgmAudioSource == null) { return; } bgmAudioSource.Stop(); } public float GetBGMVolum() { return bgmVolume; } public void SetBGMVolume(float value) { bgmVolume = value; PlayerPrefs.SetFloat("BGMVolume", value); if (bgmAudioSource == null) { return; } bgmAudioSource.volume = bgmVolume; } public AudioSource PlaySoundEffect(string path, bool isLoop) { if (m_SoundEffectObj == null) { m_SoundEffectObj = new GameObject("SoundEffect"); } AudioClip clip = Resources.Load(path); AudioSource source = m_SoundEffectObj.AddComponent(); source.clip = clip; source.volume = soundEffectVolume; source.loop = isLoop; source.Play(); soundList.Add(source); return source; } public void StopSoundEffect(AudioSource source) { if (soundList.Contains(source)) { soundList.Remove(source); source.Stop(); GameObject.Destroy(source); } } public void SetSoundEffectsVolume(float value) { soundEffectVolume = value; PlayerPrefs.SetFloat("SoundEffectVolume", value); for (int i = 0; i < soundList.Count; i++) { soundList[i].volume = value; } } public float GetSoundEffectsVolume() { return soundEffectVolume; } }