Super Knight : Enter the Dungeon
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.
 
 
 
 

269 lines
6.9 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : Singleton<SoundManager>
{
public List<AudioSource> 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<AudioSource>();
prefab.name = $"AudioSource";
}
return Instantiate(prefab, transform).GetComponent<AudioSource>();
}
AudioSource SourceGet()
{
if (listEffect == null)
listEffect = new List<AudioSource>();
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<GameObject, List<SoundPlayer>> dicSoudPlayer = new Dictionary<GameObject, List<SoundPlayer>>();
public SoundPlayer EffectPlay(GameObject prefab, float pitch = 1f)
{
if (MUTE_EFF)
return null;
List<SoundPlayer> 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<SoundPlayer>();
dicSoudPlayer.Add(prefab, list);
}
player = Instantiate(prefab, transform).GetComponent<SoundPlayer>();
if (player != null)
{
list.Add(player);
listPlay.Add(player);
player.Play(pitch);
}
return player;
}
List<SoundPlayer> listPlay = new List<SoundPlayer>();
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<GameObject, SoundPlayer> dicBGMPlayer = new Dictionary<GameObject, SoundPlayer>();
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<SoundPlayer>();
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<string, SoundPlayer> dicSP = new Dictionary<string, SoundPlayer>();
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<SoundPlayer>();
dicSP.Add(name, sp);
}
}
return sp;
}
}