fgm24/Assets/Scripts/Managers/AudioManager/AudioManager.cs

125 lines
3.1 KiB
C#
Raw Normal View History

2024-02-04 01:34:18 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager Instance;
public AudioLibraryObject audioLibrary;
private void OnEnable()
{
if (Instance is null)
{
Instance = this;
// TODO: manager itself should not handle this
// DontDestroyOnLoad(this);
}
else
{
Destroy(this);
}
}
private void OnDisable()
{
Instance = null;
}
public static AudioClip FindAudioClip(string nameOfClip)
{
2024-02-04 01:41:00 +01:00
return Instance.audioLibrary.Clips.First(x => x.name == nameOfClip);
2024-02-04 01:34:18 +01:00
}
2024-02-04 04:03:19 +01:00
public static AudioSource PlaySound(string nameOfClip, Vector3 sourcePos, bool manuallyHandle = false, bool is3D = true, bool loop = false, bool fadeIn = false)
2024-02-04 01:34:18 +01:00
{
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 = FindAudioClip(nameOfClip);
sourceObject.transform.position = sourcePos;
2024-02-04 02:20:08 +01:00
sourceObject.transform.parent = Instance.transform;
2024-02-04 01:34:18 +01:00
if (is3D)
{
audioSource.rolloffMode = AudioRolloffMode.Linear;
audioSource.spatialBlend = 1f;
}
2024-02-04 04:03:19 +01:00
if (!manuallyHandle)
Instance.StartCoroutine(Instance.StartSound(audioSource, fadeIn));
2024-02-04 01:34:18 +01:00
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);
2024-02-04 16:32:14 +01:00
try
{
Destroy(audioSource.gameObject);
}
2024-06-01 16:26:03 +02:00
catch (SystemException)
2024-02-04 16:32:14 +01:00
{
}
2024-02-04 01:34:18 +01:00
}
public IEnumerator StartMusicFadeIn(AudioSource AS)
{
while (true)
{
if (AS.volume >= 1)
{
break;
}
AS.volume += 0.01f;
yield return new WaitForSecondsRealtime(0.05f);
}
}
2024-02-04 08:19:38 +01:00
2024-02-04 16:32:14 +01:00
public void StopAllAudio()
2024-02-04 08:19:38 +01:00
{
2024-02-04 16:32:14 +01:00
StopAllCoroutines();
2024-02-04 08:19:38 +01:00
foreach (SoundSource item in AliveSources)
{
if (item == null || item.source == null) continue;
2024-02-04 16:32:14 +01:00
2024-02-04 08:19:38 +01:00
item.source.Stop();
Destroy(item.source.gameObject);
}
AliveSources.Clear();
}
2024-02-04 01:34:18 +01:00
}
public class SoundSource
{
public string Id { get; set; }
public AudioSource source { get; set; }
public SoundSource(AudioSource originalSource)
{
source = originalSource;
}
}