| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- public class SoundManager : MonoSingleton<SoundManager>
- {
- private AudioSource bgmAudioSource = null;
- private float bgmVolume = 1;
- private GameObject m_SoundEffectObj = null;
- private List<AudioSource> soundList = new List<AudioSource>();
- 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<AudioSource>();
- }
- AudioClip clip = Resources.Load<AudioClip>(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<AudioClip>(path);
- AudioSource source = m_SoundEffectObj.AddComponent<AudioSource>();
- 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;
- }
- }
|