134 lines
4.8 KiB
C#
134 lines
4.8 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public enum eSound {
|
|
s001_ButtonClick, s002_UIClose, s003_SelectAlbum, s004_OpenAlbum,
|
|
s005_MiniGameResult, s006_MiniGameGetHeart, s007_MiniGameGetChatCoint, s008_MiniGameGetGachaCoin, s009_MiniGameUseBomb,
|
|
s010_MiniGameUseUmbrella, s011_MiniGameUseDildo,
|
|
s012_Title, s013_GachaOpen, s014_GachaOpen, s015_LuckyCup, s016_LuckyCup, s017_LuckyGetItem, s018_LuckyGetItem, s019_LuckyGetItem,
|
|
s020_Noraml_Hit, s021_Noraml_Hit, s022_Noraml_Hit, s023_Noraml_Hit, s024_Noraml_Hit, s025_Noraml_Hit,
|
|
s026_Impact_Hit, s027_Impact_Hit, s028_Impact_Hit, s029_Impact_Hit,
|
|
Max
|
|
}
|
|
public enum eBGM { b001_BGM, b002_MiniGame, b003_MiniGame, b004_MiniGame, b005_Gacha, b006_Album, b007_LuckyGame, b008_Title, Max }
|
|
|
|
public class SoundInfo : MonoBehaviourSingletonTemplate<SoundInfo>
|
|
{
|
|
float minDistance = 4f;
|
|
float maxDistance = 30f;
|
|
|
|
public AudioClip[] arr_clip, arr_bgm;
|
|
List<AudioSource> list_audiosource = new List<AudioSource>();
|
|
AudioSource BGM_AudioSource;
|
|
eBGM CurBGM = eBGM.Max;
|
|
float minVolume = 0.02f;
|
|
bool EnablePlay = true;
|
|
|
|
Dictionary<eSound, AudioClip> dic_clip = new Dictionary<eSound, AudioClip>();
|
|
Dictionary<eBGM, AudioClip> dic_bgmclip = new Dictionary<eBGM, AudioClip>();
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
|
|
DontDestroy();
|
|
|
|
for (eSound i = 0; i < eSound.Max; i++)
|
|
dic_clip.Add(i, arr_clip[(int)i]);
|
|
for (eBGM i = 0; i < eBGM.Max; i++)
|
|
dic_bgmclip.Add(i, arr_bgm[(int)i]);
|
|
}
|
|
|
|
public float Get_Volume(Vector3 _v, float _maxvol = 1f)
|
|
{
|
|
if (DSUtil.CheckNull(Camera.main)) return 0f;
|
|
|
|
float distance = Vector3.Distance(_v, Camera.main.transform.position);
|
|
var vol = distance < minDistance ? _maxvol : distance > maxDistance ? minVolume : _maxvol - (distance - minDistance) / (maxDistance - minDistance);
|
|
if (vol < minVolume) vol = minVolume;
|
|
//vol *= OptionInfo.Ins.slider_SFX.value;
|
|
return vol;
|
|
}
|
|
|
|
public void Play_OneShot(eSound _sound, float vol = 1f)
|
|
{
|
|
if (SaveMgr.Ins.Get_Option(eOption.Sound))
|
|
Play_OneShot(dic_clip[_sound], vol);
|
|
}
|
|
AudioSource Play_OneShot(AudioClip _clip, float vol = 1f, bool _loop = false)
|
|
{
|
|
AudioSource temp = list_audiosource.Find(f => !f.isPlaying);
|
|
if (temp == null)
|
|
{
|
|
var go = new GameObject(list_audiosource.Count.ToString());
|
|
go.transform.SetParent(transform);
|
|
temp = go.AddComponent<AudioSource>();
|
|
list_audiosource.Add(temp);
|
|
}
|
|
temp.clip = _clip;
|
|
//temp.volume = (OptionInfo.Ins ? OptionInfo.Ins.slider_SFX.value : 1f) * vol;
|
|
temp.volume = vol;
|
|
temp.loop = _loop;
|
|
if (EnablePlay || _loop) temp.Play();
|
|
return temp;
|
|
}
|
|
public void Play_OneShot_byDistance(eSound _sound, Vector3 _v, float _maxvol = 1f)
|
|
{
|
|
Play_OneShot(_sound, Get_Volume(_v, _maxvol));
|
|
}
|
|
public AudioSource Play_OneShot_byDistance(AudioClip _clip, Vector3 _v, float _maxvol = 1f, bool _loop = false)
|
|
{
|
|
return Play_OneShot(_clip, Get_Volume(_v, _maxvol), _loop);
|
|
}
|
|
//public void Play_OneShot_3DSound(eSound _sound, GameObject _source, float vol = 1f)
|
|
//{
|
|
// var temp = _source.GetComponent<AudioSource>();
|
|
// if (temp == null)
|
|
// {
|
|
// temp = _source.AddComponent<AudioSource>();
|
|
// temp.rolloffMode = AudioRolloffMode.Linear;
|
|
// temp.minDistance = 1f;
|
|
// temp.maxDistance = 2f;
|
|
// }
|
|
// temp.clip = arr_clip[MyEnumToInt.Ins.Get_Int(_sound)];
|
|
// temp.volume = OptionInfo.Ins.slider_SFX.value;
|
|
// temp.Play();
|
|
//}
|
|
public void Play_BGM(eBGM _bgm)
|
|
{
|
|
if (CurBGM != _bgm)
|
|
{
|
|
CurBGM = _bgm;
|
|
if (BGM_AudioSource == null)
|
|
{
|
|
var go = new GameObject("BGM");
|
|
go.transform.SetParent(transform);
|
|
BGM_AudioSource = go.AddComponent<AudioSource>();
|
|
}
|
|
|
|
BGM_AudioSource.clip = dic_bgmclip[_bgm];
|
|
BGM_AudioSource.loop = true;
|
|
BGM_AudioSource.volume = 1f;// OptionInfo.Ins.slider_BGM.value;
|
|
if (SaveMgr.Ins.Get_Option(eOption.Bgm))
|
|
RePlay_BGM();
|
|
else
|
|
Stop_BGM();
|
|
}
|
|
}
|
|
|
|
public void Set_BGM_Volume(float _v) { if (BGM_AudioSource != null) BGM_AudioSource.volume = _v; }
|
|
public void Stop_BGM() { if (BGM_AudioSource != null) BGM_AudioSource.Pause(); }
|
|
public void RePlay_BGM() { if (BGM_AudioSource != null) BGM_AudioSource.Play(); }
|
|
public void Active_Sound(bool _active)
|
|
{
|
|
EnablePlay = _active;
|
|
if (EnablePlay)
|
|
{
|
|
RePlay_BGM();
|
|
}
|
|
else
|
|
{
|
|
Stop_BGM();
|
|
}
|
|
}
|
|
} |