SoundManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. public class SoundManager : MonoSingleton<SoundManager>
  6. {
  7. private AudioSource bgmAudioSource = null;
  8. private float bgmVolume = 1;
  9. private GameObject m_SoundEffectObj = null;
  10. private List<AudioSource> soundList = new List<AudioSource>();
  11. private float soundEffectVolume = 1;
  12. public override void InitManager()
  13. {
  14. base.InitManager();
  15. SetBGMVolume(PlayerPrefs.HasKey("BGMVolume") ? PlayerPrefs.GetFloat("BGMVolume") : 1);
  16. SetSoundEffectsVolume(PlayerPrefs.HasKey("SoundEffectVolume") ? PlayerPrefs.GetFloat("SoundEffectVolume") : 1);
  17. }
  18. private void Update()
  19. {
  20. for(int i = 0; i < soundList.Count; i++)
  21. {
  22. if (!soundList[i].isPlaying)
  23. {
  24. GameObject.Destroy(soundList[i]);
  25. soundList.RemoveAt(i);
  26. }
  27. }
  28. }
  29. public void PlayBGM(string path)
  30. {
  31. if (bgmAudioSource == null)
  32. {
  33. GameObject obj = new GameObject("BGM");
  34. bgmAudioSource = obj.AddComponent<AudioSource>();
  35. }
  36. AudioClip clip = Resources.Load<AudioClip>(path);
  37. bgmAudioSource.loop = true;
  38. bgmAudioSource.clip = clip;
  39. bgmAudioSource.volume = bgmVolume;
  40. bgmAudioSource.Play();
  41. }
  42. public void PasueBGM()
  43. {
  44. if (bgmAudioSource == null)
  45. {
  46. return;
  47. }
  48. bgmAudioSource.Pause();
  49. }
  50. public void ContinueBGM()
  51. {
  52. if (bgmAudioSource == null)
  53. {
  54. return;
  55. }
  56. bgmAudioSource.Pause();
  57. }
  58. public void StopBGM()
  59. {
  60. if (bgmAudioSource == null)
  61. {
  62. return;
  63. }
  64. bgmAudioSource.Stop();
  65. }
  66. public float GetBGMVolum()
  67. {
  68. return bgmVolume;
  69. }
  70. public void SetBGMVolume(float value)
  71. {
  72. bgmVolume = value;
  73. PlayerPrefs.SetFloat("BGMVolume", value);
  74. if (bgmAudioSource == null)
  75. {
  76. return;
  77. }
  78. bgmAudioSource.volume = bgmVolume;
  79. }
  80. public AudioSource PlaySoundEffect(string path, bool isLoop)
  81. {
  82. if (m_SoundEffectObj == null)
  83. {
  84. m_SoundEffectObj = new GameObject("SoundEffect");
  85. }
  86. AudioClip clip = Resources.Load<AudioClip>(path);
  87. AudioSource source = m_SoundEffectObj.AddComponent<AudioSource>();
  88. source.clip = clip;
  89. source.volume = soundEffectVolume;
  90. source.loop = isLoop;
  91. source.Play();
  92. soundList.Add(source);
  93. return source;
  94. }
  95. public void StopSoundEffect(AudioSource source)
  96. {
  97. if (soundList.Contains(source))
  98. {
  99. soundList.Remove(source);
  100. source.Stop();
  101. GameObject.Destroy(source);
  102. }
  103. }
  104. public void SetSoundEffectsVolume(float value)
  105. {
  106. soundEffectVolume = value;
  107. PlayerPrefs.SetFloat("SoundEffectVolume", value);
  108. for (int i = 0; i < soundList.Count; i++)
  109. {
  110. soundList[i].volume = value;
  111. }
  112. }
  113. public float GetSoundEffectsVolume()
  114. {
  115. return soundEffectVolume;
  116. }
  117. }