You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
5.9 KiB
253 lines
5.9 KiB
using IVDataFormat;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using UnityEngine.Audio;
|
|
|
|
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 : MonoBehaviour
|
|
{
|
|
#region Const
|
|
private static SoundMgr curMgr = null;
|
|
#endregion Const
|
|
|
|
|
|
private static bool BBgmOn = true;
|
|
public static bool BgmOn
|
|
{
|
|
get
|
|
{
|
|
return BBgmOn;
|
|
}
|
|
set
|
|
{
|
|
if (curMgr != null)
|
|
{
|
|
if (BBgmOn && !value)
|
|
curMgr.StopBgm();
|
|
if (!BBgmOn && value)
|
|
curMgr.PlayBgmSelected();
|
|
}
|
|
BBgmOn = value;
|
|
}
|
|
}
|
|
private static bool BEfcOn = true;
|
|
public static bool EfcOn
|
|
{
|
|
get
|
|
{
|
|
return BEfcOn;
|
|
}
|
|
set
|
|
{
|
|
BEfcOn = value;
|
|
}
|
|
}
|
|
private static bool BVoiceOn = true;
|
|
public static bool VoiceOn
|
|
{
|
|
get
|
|
{
|
|
return BVoiceOn;
|
|
}
|
|
set
|
|
{
|
|
BVoiceOn = value;
|
|
}
|
|
}
|
|
|
|
private AudioSource asPlayer = null;
|
|
private AudioSource[] sfxPlayer = null;
|
|
|
|
private int iLoading = 1;
|
|
private BGM iSelectedBgm = BGM.none;
|
|
|
|
#region ASSFX
|
|
[SerializeField]
|
|
Sound[] sfx;
|
|
#endregion
|
|
|
|
#region Base
|
|
private void Awake()
|
|
{
|
|
if (curMgr != null)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
curMgr = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
asPlayer = GetComponent<AudioSource>();
|
|
sfxPlayer = new AudioSource[3];
|
|
sfxPlayer[0] = asPlayer.transform.Find("sfx").GetComponent<AudioSource>();
|
|
sfxPlayer[1] = asPlayer.transform.Find("sfx (1)").GetComponent<AudioSource>();
|
|
sfxPlayer[2] = asPlayer.transform.Find("sfx (2)").GetComponent<AudioSource>();
|
|
|
|
iLoading--;
|
|
}
|
|
#endregion Base
|
|
|
|
#region Control
|
|
public static void SSetAmxGroupBgm(AudioMixerGroup amxg)
|
|
{
|
|
if (curMgr != null)
|
|
curMgr.SetAmxGroupBgm(amxg);
|
|
}
|
|
|
|
public void SetAmxGroupBgm(AudioMixerGroup amxg)
|
|
{
|
|
//if (ilPlayer != null)
|
|
// ilPlayer.SetMixerGroup(amxg);
|
|
if (asPlayer != null)
|
|
asPlayer.outputAudioMixerGroup = amxg;
|
|
}
|
|
#endregion Control
|
|
|
|
#region Select
|
|
// 선택된 상태의 배경음 로드 & 재생.
|
|
private void PlayBgmSelected()
|
|
{
|
|
if (iSelectedBgm == BGM.none) return;
|
|
Addressables.LoadAssetAsync<AudioClip>(FormatString.StringFormat(AddressableMgr.PathBgmClip, DataHandler.GetBgmPath(iSelectedBgm))).Completed += ALoadBgmComp;
|
|
}
|
|
|
|
public static bool SPlayBgm(BGM bgm)
|
|
{
|
|
if (curMgr != null)
|
|
return curMgr.PlayBgm(bgm);
|
|
return false;
|
|
}
|
|
|
|
// 배경음 로드 요청.
|
|
private bool PlayBgm(BGM bgm)
|
|
{
|
|
if (bgm == iSelectedBgm) return false;
|
|
|
|
iSelectedBgm = bgm;
|
|
if (!BBgmOn)
|
|
{
|
|
GameUIMgr.SOnChangeBgm(LocalizationText.GetText(FormatString.StringFormat("bgm : {0}", iSelectedBgm)));
|
|
return true;
|
|
}
|
|
Addressables.LoadAssetAsync<AudioClip>(FormatString.StringFormat(AddressableMgr.PathBgmClip, DataHandler.GetBgmPath(bgm))).Completed += ALoadBgmComp;
|
|
return true;
|
|
}
|
|
|
|
// 배경음 로드 완료.
|
|
private void ALoadBgmComp(AsyncOperationHandle<AudioClip> 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}", iSelectedBgm)));
|
|
AddressableMgr.SAddUnload();
|
|
}
|
|
|
|
// 배경음 멈춤 및 리소스 해제.
|
|
private void StopBgm()
|
|
{
|
|
asPlayer.Stop();
|
|
|
|
if (asPlayer.clip != null)
|
|
Addressables.Release(asPlayer.clip);
|
|
|
|
asPlayer.clip = null;
|
|
}
|
|
|
|
// 배경음 일시 정지.
|
|
public static void SPauseBgm()
|
|
{
|
|
if (curMgr != null)
|
|
curMgr.asPlayer.Pause();
|
|
GameUIMgr.SPrintTestLog("SPauseBgm");
|
|
}
|
|
|
|
// 배경음 다시 재생.
|
|
public static void SResumeBgm()
|
|
{
|
|
if (curMgr != null)
|
|
curMgr.asPlayer.Play();
|
|
GameUIMgr.SPrintTestLog("SResumeBgm");
|
|
}
|
|
|
|
public static bool SChangeBgm()
|
|
{
|
|
if (curMgr != null)
|
|
return curMgr.ChangeBgm();
|
|
return false;
|
|
}
|
|
|
|
// 배경음 변경.
|
|
private bool ChangeBgm()
|
|
{
|
|
if (iLoading > 0)
|
|
return false;
|
|
iLoading++;
|
|
|
|
BGM next = iSelectedBgm + 1;
|
|
if (!DataHandler.IsAvailBgm(next))
|
|
{
|
|
next = BGM.stage;
|
|
}
|
|
ES3.Save(Global.ES3_Bgm, next);
|
|
|
|
iLoading--;
|
|
return PlayBgm(next);
|
|
}
|
|
#endregion Select
|
|
|
|
#region SFX
|
|
public static void PlaySfx(SoundName name1, SoundName name2 = SoundName.none, SoundName name3 = SoundName.none)
|
|
{
|
|
if (EfcOn)
|
|
{
|
|
curMgr.sfxPlayer[0].clip = curMgr.sfx[(int)name1].clip;
|
|
curMgr.sfxPlayer[0].Play();
|
|
if (name2 != SoundName.none)
|
|
{
|
|
curMgr.sfxPlayer[1].clip = curMgr.sfx[(int)name2].clip;
|
|
curMgr.sfxPlayer[1].Play();
|
|
}
|
|
if (name3 != SoundName.none)
|
|
{
|
|
curMgr.sfxPlayer[2].clip = curMgr.sfx[(int)name3].clip;
|
|
curMgr.sfxPlayer[2].Play();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void ButtonSFXPlay()
|
|
{
|
|
curMgr.sfxPlayer[0].clip = curMgr.sfx[(int)SoundName.BtnPress].clip;
|
|
curMgr.sfxPlayer[0].Play();
|
|
}
|
|
#endregion
|
|
}
|