PracProg/Assets/Managers/AudioManager/AudioManager.cs
2021-02-04 19:49:39 +01:00

48 lines
936 B
C#

using UnityEngine.Audio;
using UnityEngine;
using System;
public class AudioManager : MonoBehaviour
{
public Sound[] sounds;
public static AudioManager audiomanager;
void Awake()
{
DontDestroyOnLoad(gameObject);
if(audiomanager == null)
{
audiomanager = this;
}
else
{
Destroy(gameObject);
}
foreach(Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}
public void Play(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if(s == null)
{
Debug.LogWarning("The sound " + name + "does not exist");
return;
}
s.source.Play();
}
}