using IVDataFormat; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.Audio; using Sirenix.Utilities; public enum SoundName { BtnPress, ReinforceSuccess, ReinforceFailed, OneSummon, MultiSummon, SummonBoss, GetGoods, UnlockThing, none } [System.Serializable] public class Sound { public SoundName name; public AudioClip clip; } public class SoundMgr : MonoSingleton { private bool BBgmOn = true; public bool BgmOn { get { return BBgmOn; } set { if (BBgmOn && !value) { StopBgm(); } else if (!BBgmOn && value) { PlayBgmSelected(); } BBgmOn = value; } } public bool EfcOn { get; set; } public bool VoiceOn { get; set; } private AudioSource asPlayer = null; private AudioSource[] sfxPlayer = null; private string curBgmPath = ""; #region ASSFX [SerializeField] Sound[] sfx; #endregion #region Base void Start() { asPlayer = GetComponent(); sfxPlayer = new AudioSource[3]; sfxPlayer[0] = asPlayer.transform.Find("sfx").GetComponent(); sfxPlayer[1] = asPlayer.transform.Find("sfx (1)").GetComponent(); sfxPlayer[2] = asPlayer.transform.Find("sfx (2)").GetComponent(); } #endregion Base #region Control public void SetAmxGroupBgm(AudioMixerGroup amxg) { if (asPlayer != null) asPlayer.outputAudioMixerGroup = amxg; } #endregion Control #region Select // 선택된 상태의 배경음 로드 & 재생. private void PlayBgmSelected() { if (curBgmPath.IsNullOrWhitespace()) return; Addressables.LoadAssetAsync(curBgmPath).Completed += ALoadBgmComp; } // 배경음 로드 요청. public bool PlayBgm(BGM bgm) { return PlayBgm(FormatString.StringFormat(AddressableMgr.PathBgmClip, DataHandler.GetBgmPath(bgm))); } public bool PlayBgm(string path) { if (path == curBgmPath) return false; curBgmPath = path; if (!BBgmOn) { GameUIMgr.SOnChangeBgm(LocalizationText.GetText(FormatString.StringFormat("bgm : {0}", curBgmPath))); return true; } Addressables.LoadAssetAsync(path).Completed += ALoadBgmComp; return true; } // 배경음 로드 완료. private void ALoadBgmComp(AsyncOperationHandle obj) { asPlayer.Stop(); if (asPlayer.clip != null) Addressables.Release(asPlayer.clip); asPlayer.clip = obj.Result; asPlayer.Play(); GameUIMgr.SOnChangeBgm(LocalizationText.GetText(FormatString.StringFormat("bgm : {0}", curBgmPath))); AddressableMgr.SAddUnload(); } // 배경음 멈춤 및 리소스 해제. private void StopBgm() { asPlayer.Stop(); if (asPlayer.clip != null) Addressables.Release(asPlayer.clip); asPlayer.clip = null; } // 배경음 일시 정지. public void SPauseBgm() { asPlayer.Pause(); GameUIMgr.SPrintTestLog("SPauseBgm"); } // 배경음 다시 재생. public void SResumeBgm() { asPlayer.Play(); GameUIMgr.SPrintTestLog("SResumeBgm"); } #endregion Select #region SFX public void PlaySfx(SoundName name1, SoundName name2 = SoundName.none, SoundName name3 = SoundName.none) { if (!EfcOn) return; sfxPlayer[0].clip = sfx[(int)name1].clip; sfxPlayer[0].Play(); if (name2 != SoundName.none) { sfxPlayer[1].clip = sfx[(int)name2].clip; sfxPlayer[1].Play(); } if (name3 != SoundName.none) { sfxPlayer[2].clip = sfx[(int)name3].clip; sfxPlayer[2].Play(); } } public void PlaySfx(AudioClip clip, bool playSurely = true) { if (!EfcOn) return; for(int i = 0; i < sfxPlayer.Length; i++) { if (!sfxPlayer[i].isPlaying) { sfxPlayer[i].clip = clip; sfxPlayer[i].Play(); return; } } if(playSurely) { // if all sfxPlayer is playing, play on sfxPlayer[0]. sfxPlayer[0].clip = clip; sfxPlayer[0].Play(); } } public void ButtonSFXPlay() { sfxPlayer[0].clip = sfx[(int)SoundName.BtnPress].clip; sfxPlayer[0].Play(); } #endregion }