AudioManager
Simple Music and Ambiant setter.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DG.Tweening;
using EODE.Wonderland;
using UnityEngine;
namespace MyProject {
public class AudioManager : MonoSingleton<AudioManager> {
protected override bool NeverReplace => true;
class Source {
public string LastPath = "";
public float MaxVolume = 1f;
List<AudioSource> _sources = new List<AudioSource>();
bool _muted = false;
public void Update() {
if (_sources.Count < 2) return;
if (_sources.First().volume < 0.01f) {
Destroy(_sources.First());
_sources.RemoveAt(0);
}
}
public void Add(AudioSource source, float duration) {
if (_sources.Count > 0) {
_sources.Last().DOKill();
if (_sources.Last().volume > 0f) {
_sources.Last().DOFade(0f, duration);
}
}
if (source == null) return;
source.loop = true;
source.spatialize = false;
source.volume = 0f;
if (!_muted) source.DOFade(MaxVolume, duration);
_sources.Add(source);
source.Play();
}
public void Mute(float duration) {
_muted = true;
if (_sources.Count > 0) _sources.Last().DOFade(0f, duration);
}
public void UnMute(float duration) {
_muted = false;
if (_sources.Count > 0) _sources.Last().DOFade(MaxVolume, duration);
}
public void UpdateVolume(float duration) {
if (_sources.Count > 0) _sources.Last().DOFade(MaxVolume, duration);
}
}
List<Source> _srcs = new List<Source>();
protected override void Awake() {
base.Awake();
_srcs.Add(new Source());
_srcs.Add(new Source());
StartCoroutine(SrcsRoutine());
}
IEnumerator SrcsRoutine() {
while (true) {
foreach (var src in _srcs) {
src.Update();
yield return new WaitForSeconds(0.5f);
}
}
}
void Set(int index, AudioClip source, float duration = 2f) {
if (source == null) {
_srcs[index].LastPath = "";
_srcs[index].Add(null, duration);
return;
}
if (source.name != _srcs[index].LastPath) {
_srcs[index].LastPath = source.name;
var audioS = gameObject.AddComponent<AudioSource>();
audioS.clip = source;
_srcs[index].Add(audioS, duration);
}
}
public void SetMusicVolume(float value, float duration = 2f) {
_srcs[0].MaxVolume = value;
_srcs[0].UpdateVolume(duration);
}
public void SetEnvVolume(float value, float duration = 2f) {
_srcs[1].MaxVolume = value;
_srcs[1].UpdateVolume(duration);
}
public void SetMusic(AudioClip source, float duration = 2f) {
Set(0, source, duration);
}
public void SetEnvironment(AudioClip source, float duration = 2f) {
Set(1, source, duration);
}
public void MuteMusic(float duration = 2f) {
_srcs[0].Mute(duration);
}
public void MuteEnvironment(float duration = 2f) {
_srcs[1].Mute(duration);
}
public void UnMuteMusic(float duration = 2f) {
_srcs[0].UnMute(duration);
}
public void UnMuteEnvironment(float duration = 2f) {
_srcs[1].UnMute(duration);
}
public void RemoveMusic(float duration = 2f) {
_srcs[0].Add(null, duration);
}
public void RemoveEnvironment(float duration = 2f) {
_srcs[1].Add(null, duration);
}
public void SetGlobalSound(AudioClip source) {
var audioS = gameObject.AddComponent<AudioSource>();
audioS.clip = source;
audioS.loop = false;
audioS.spatialize = false;
audioS.volume = 1f;
audioS.Play();
RemoveSound(audioS).WrapErrors();
}
public async System.Threading.Tasks.Task RemoveSound(AudioSource audioS) {
await new WaitWhile(() => audioS.isPlaying);
audioS.SafeDestroy();
}
public async System.Threading.Tasks.Task SetGlobalSound(AudioClip source, float volume, float pitch = 1f) {
var audioS = gameObject.AddComponent<AudioSource>();
audioS.clip = source;
audioS.loop = false;
audioS.spatialize = false;
audioS.volume = volume;
audioS.pitch = pitch;
audioS.Play();
await new WaitWhile(() => audioS.isPlaying);
audioS.SafeDestroy();
}
public AudioSource SetGlobalSoundGet(AudioClip source, float volume = 1f, float pitch = 1f) {
var audioS = gameObject.AddComponent<AudioSource>();
audioS.loop = false;
audioS.spatialize = false;
audioS.volume = volume;
audioS.pitch = pitch;
SetGlobalSoundGet2(audioS, source).WrapErrors();
return audioS;
}
async System.Threading.Tasks.Task SetGlobalSoundGet2(AudioSource audioS, AudioClip source) {
audioS.clip = source;
audioS.Play();
await new WaitWhile(() => audioS.isPlaying);
audioS.SafeDestroy();
}
}
}