Shegotwet/Assets/Scripts/AttachToGameObject/SoundInfo.cs

128 lines
4.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using UnityEngine;
2025-09-19 19:31:06 +00:00
public enum eSound { s001_ButtonClick, s002_UIClose,
Max
}
2025-09-19 19:31:06 +00:00
public enum eBGM { b001_BGM, b002_MiniGame, b003_MiniGame, b004_MiniGame, b005_Gacha, b006_Album, b007_LuckyGame, 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;
2025-09-13 22:08:25 +00:00
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>();
2025-09-13 22:08:25 +00:00
protected override void Awake()
{
2025-09-13 22:08:25 +00:00
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 = 0.5f)
{
2025-09-11 06:10:39 +00:00
if (SaveMgr.Ins.Get_Option(eOption.Sound))
Play_OneShot(dic_clip[_sound], vol);
}
2025-09-11 06:10:39 +00:00
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)
{
2025-09-19 19:31:06 +00:00
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;
2025-09-19 19:31:06 +00:00
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();
}
}
}