SquareDash/Assets/Scripts/Audio/AudioManager.cs
2023-12-29 02:26:42 +01:00

79 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FMODUnity;
using FMOD.Studio;
public class AudioManager : MonoBehaviour
{
private List<EventInstance> eventInstances;
private EventInstance musicEventInstance;
public static AudioManager instance { get; private set; }
private void Awake()
{
if (instance != null)
{
Debug.LogError("Found more than one Audio Manager in the scene");
}
instance = this;
eventInstances = new List<EventInstance>();
}
private void Start()
{
InitializeMusic(FMODEvents.instance.music);
}
private void InitializeMusic(EventReference musicEventReference)
{
musicEventInstance = CreateEventInstance(musicEventReference);
musicEventInstance.start();
}
public void PlayOneShot(EventReference sound, Vector3 worldPos)
{
RuntimeManager.PlayOneShot(sound, worldPos);
}
public void PauseMusic()
{
musicEventInstance.setPaused(true);
}
public void PlayMusic()
{
musicEventInstance.setPaused(false);
}
public void StopMusic()
{
musicEventInstance.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
}
public EventInstance CreateEventInstance(EventReference eventReference)
{
EventInstance eventInstance = RuntimeManager.CreateInstance(eventReference);
eventInstances.Add(eventInstance);
return eventInstance;
}
private void CleanUp()
{
foreach (EventInstance eventInstance in eventInstances)
{
eventInstance.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
eventInstance.release();
}
}
private void OnDestroy()
{
CleanUp();
}
}