using System.Collections; using System.Collections.Generic; using UnityEngine; public class SoundManager : Singleton { public List listEffect; const string recordBGM = "SoundOnBGM"; public bool MUTE_BGM = false; const string recordEff = "SoundOnEffect"; public bool MUTE_EFF = false; const string recordVOC = "SoundOnVoice"; public bool MUTE_VOC = false; public AssetBundleManager assetBundle = new AssetBundleManager(); public void Init() { DontDestroyOnLoad(this); OnInitialized(); } private void OnInitialized() { if (PlayerPrefs.HasKey(recordBGM)) MUTE_BGM = bool.Parse(PlayerPrefs.GetString(recordBGM)); if (PlayerPrefs.HasKey(recordEff)) MUTE_EFF = bool.Parse(PlayerPrefs.GetString(recordEff)); if (PlayerPrefs.HasKey(recordVOC)) MUTE_VOC = bool.Parse(PlayerPrefs.GetString(recordVOC)); } string strKey; void Play(AudioSource source, AudioClip clip) { source.clip = clip; source.Play(); } GameObject prefab = null; AudioSource SouceCreate() { if (prefab == null) { prefab = Instantiate(new GameObject(), transform); prefab.AddComponent(); prefab.name = $"AudioSource"; } return Instantiate(prefab, transform).GetComponent(); } AudioSource SourceGet() { if (listEffect == null) listEffect = new List(); for (int i = 0; i < listEffect.Count; i++) { if (!listEffect[i].isPlaying) return listEffect[i]; } AudioSource source = SouceCreate(); source.name = $"AudioSource {listEffect.Count}"; listEffect.Add(source); return source; } AudioClip ClipGet(string nameClip) { return assetBundle.GetAudioClip(nameClip); } AudioSource tmp; public AudioSource EffectPlay(string nameClip, bool LOOP = false, float duration = 0, float pitch = 0) { tmp = SourceGet(); Play(tmp, ClipGet(nameClip)); tmp.loop = LOOP; tmp.mute = MUTE_EFF; if (duration != 0) StartCoroutine(PlayDuration(tmp, duration)); return tmp; } public AudioSource EffectPlay(AudioClip clip, bool LOOP = false, float duration = 0, float pitch = 1) { tmp = SourceGet(); tmp.pitch = pitch; Play(tmp, clip); tmp.loop = LOOP; tmp.mute = MUTE_EFF; if (duration != 0) StartCoroutine(PlayDuration(tmp, duration)); return tmp; } IEnumerator PlayDuration(AudioSource audioSource, float duration) { yield return new WaitForSeconds(duration); audioSource.Stop(); } public AudioSource VoicePlay(string nameClip, bool LOOP = false) { tmp = SourceGet(); Play(tmp, ClipGet(nameClip)); tmp.loop = LOOP; tmp.mute = MUTE_VOC; return tmp; } public void MuteSetting(string nameRecord, bool isOn) { //Debug.Log($"{name} : {nameRecord}, {isOn}"); PlayerPrefs.SetString(nameRecord, $"{isOn}"); PlayerPrefs.Save(); OnInitialized(); } Dictionary> dicSoudPlayer = new Dictionary>(); public SoundPlayer EffectPlay(GameObject prefab, float pitch = 1f) { if (MUTE_EFF) return null; List list = null; SoundPlayer player = null; if (dicSoudPlayer.ContainsKey(prefab)) { list = dicSoudPlayer[prefab]; player = list.Find(a => !a.audioSource.isPlaying); if (player != null) { listPlay.Add(player); player.Play(pitch); return player; } } else { list = new List(); dicSoudPlayer.Add(prefab, list); } player = Instantiate(prefab, transform).GetComponent(); if (player != null) { list.Add(player); listPlay.Add(player); player.Play(pitch); } return player; } List listPlay = new List(); public bool LEVELING = true; public float volumeStep = 0.97f; void Leveling() { if (LEVELING && 0 < listPlay.Count) { float rate = 1; for (int i = 0; i < listPlay.Count; i++) { //rate = 1 - (rateStep * i); rate = Mathf.Pow(volumeStep, i); if (listPlay[i].audioSource.isPlaying) listPlay[i].Volume(rate < 0 ? 0 : rate); else if (!listPlay[i].audioSource.isPlaying) listPlay.Remove(listPlay[i--]); } } } private void Update() { Leveling(); } Dictionary dicBGMPlayer = new Dictionary(); public SoundPlayer bgm = null; System.Action OnStop; public SoundPlayer BGMPlay(GameObject prefab, bool PLAY, System.Action OnStop = null) { if (bgm != null && PLAY) { if (this.OnStop != null) this.OnStop(); this.OnStop = OnStop; BGMStop(); } this.OnStop = OnStop; SoundPlayer sound = null; if (dicBGMPlayer.ContainsKey(prefab)) { sound = dicBGMPlayer[prefab]; } else { sound = Instantiate(prefab, transform).GetComponent(); dicBGMPlayer.Add(prefab, sound); } if (sound != null) { sound.Play_Fade(PLAY); sound.audioSource.mute = MUTE_BGM; } bgm = PLAY ? sound : null; return bgm; } public void BGMStop() { if (bgm != null) { bgm.Play_Fade(false); bgm = null; } } public void BGMPause(bool PAUSE) { if(bgm != null) { if (PAUSE) bgm.audioSource.Pause(); else bgm.audioSource.Play(); } } public void BGMVolume(float rate = 1, System.Action OnEnd = null) { if (bgm != null) bgm.Volume(rate, OnEnd); } Dictionary dicSP = new Dictionary(); public SoundPlayer PrefabGet(string name) { SoundPlayer sp = null; if (dicSP.ContainsKey(name)) sp = dicSP[name]; else { GameObject obj = Resources.Load($"{name}") as GameObject; if (obj != null) { sp = obj.GetComponent(); dicSP.Add(name, sp); } } return sp; } }