117 lines
4.4 KiB
C#
117 lines
4.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public enum eSound { s000_GateOpen, s001_LightningImpact, s002_MagicHit, s003_Click, s004_GetBook, s005_BoxOpen, s006_BreakBottle, s007_LaserMissileFire, s008_LaserExplosion, s009_CastSpell, s010_Attack, s011_Potion, s012_CosumeLvUp,
|
|
s013_etfx_explosion_grenade, s014_IceSpike, s015_GoldPowerUp, s016_MagicStepOpen, s017_Transcend, s018_StoneLvUp, s019_Win, s020_WinPetBattle, s021_Fail, s022_Bag, s023_CardMove, s024_Gamble, s025_Win2, s026_PotionMake,
|
|
s027_randomwheel,
|
|
Max
|
|
}
|
|
public enum eBGM { Chapter1, Chapter2, Chapter3, Chapter4, Chapter5, Chapter6, Chapter7, Chapter8, Chapter9, Chapter10, Chapter11, Chapter12, 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;
|
|
|
|
private void Start()
|
|
{
|
|
DontDestroy();
|
|
}
|
|
|
|
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 = 0.5f)
|
|
{
|
|
if (!DSUtil.CheckNull(MyEnumToInt.Ins))
|
|
Play_OneShot(arr_clip[MyEnumToInt.Ins.Get_Int(_sound)], vol);
|
|
}
|
|
public 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.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 = arr_bgm[MyEnumToInt.Ins.Get_Int(_bgm)];
|
|
BGM_AudioSource.loop = true;
|
|
BGM_AudioSource.volume = OptionInfo.Ins.slider_BGM.value;
|
|
BGM_AudioSource.Play();
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
} |