3DTD/Assets/Scripts/Manager/AudioManager.cs

136 lines
3.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Audio;
public class AudioManager : MonoBehaviour
{
public static AudioManager Instance;
public AudioLibraryObject audioLibrary;
public AudioMixerGroup mixer;
public AudioMixerGroup sfxMixer;
private void Awake()
{
if (Instance != null)
{
Destroy(this);
// TODO: manager itself should not handle this
// DontDestroyOnLoad(this);
return;
}
Instance = this;
}
public static AudioClip FindAudioClip(string nameOfClip)
{
return Instance.audioLibrary.Clips.First(x => x.name.ToLower().Replace(" ", "")== nameOfClip.ToLower().Replace(" ", ""));
}
public static AudioSource PlaySound(AudioClip clip, Vector3 sourcePos, bool manuallyHandle = false, bool is3D = true, bool loop = false, bool fadeIn = false, bool music = false)
{
return PlaySoundInternal(clip, sourcePos, manuallyHandle, is3D, loop, fadeIn, music);
}
public static AudioSource PlaySound(string nameOfClip, Vector3 sourcePos, bool manuallyHandle = false, bool is3D = true, bool loop = false, bool fadeIn = false, bool music = false)
{
AudioClip clip = FindAudioClip(nameOfClip);
return PlaySoundInternal(clip, sourcePos, manuallyHandle, is3D, loop, fadeIn, music);
}
private static AudioSource PlaySoundInternal(AudioClip clip, Vector3 sourcePos, bool manuallyHandle, bool is3D, bool loop, bool fadeIn, bool music)
{
if (Instance == null)
{
Debug.LogWarning("AudioManager has not been initialized. Please initialize it before use.");
return null;
}
var sourceObject = new GameObject("SoundSource");
var audioSource = sourceObject.AddComponent<AudioSource>();
audioSource.loop = loop;
audioSource.clip = clip;
sourceObject.transform.position = sourcePos;
sourceObject.transform.parent = Instance.transform;
audioSource.volume = GameManager.Instance.Volume;
if (music)
audioSource.outputAudioMixerGroup = Instance.mixer;
else
audioSource.outputAudioMixerGroup = Instance.sfxMixer;
if (is3D)
{
audioSource.rolloffMode = AudioRolloffMode.Linear;
audioSource.spatialBlend = 1f;
}
if (!manuallyHandle)
Instance.StartCoroutine(Instance.StartSound(audioSource, fadeIn));
return audioSource;
}
public static List<SoundSource> AliveSources = new List<SoundSource>();
IEnumerator StartSound(AudioSource audioSource, bool fadeIn = false)
{
AliveSources.Add(new SoundSource(audioSource));
if (fadeIn)
{
audioSource.volume = 0;
StartCoroutine(StartMusicFadeIn(audioSource));
}
audioSource.Play();
yield return new WaitUntil(() => audioSource.gameObject == null || !audioSource.isPlaying);
try
{
Destroy(audioSource.gameObject);
}
catch (SystemException err)
{
}
}
public IEnumerator StartMusicFadeIn(AudioSource AS)
{
while (true)
{
if (AS.volume >= 1)
{
break;
}
AS.volume += 0.01f;
yield return new WaitForSecondsRealtime(0.05f);
}
}
public void StopAllAudio()
{
StopAllCoroutines();
foreach (SoundSource item in AliveSources)
{
if (item == null || item.source == null) continue;
item.source.Stop();
Destroy(item.source.gameObject);
}
AliveSources.Clear();
}
}
public class SoundSource
{
public string Id { get; set; }
public AudioSource source { get; set; }
public SoundSource(AudioSource originalSource)
{
source = originalSource;
}
}