diff options
author | cross28 <icross028@gmail.com> | 2022-04-18 04:10:52 -0500 |
---|---|---|
committer | cross28 <icross028@gmail.com> | 2022-04-18 04:10:52 -0500 |
commit | e6e9c0826f2d622caefa5e1a99d643a85f4c058f (patch) | |
tree | 3b52d2ee39abbcb11b2d572e2ec2e3b870a97749 /Assets/Scripts/AudioManager.cs | |
parent | 16fff95b13a3c28f4cee6791a897412ce0a32708 (diff) |
feat: Rewrote the audio manager. Added background music
Diffstat (limited to 'Assets/Scripts/AudioManager.cs')
-rw-r--r-- | Assets/Scripts/AudioManager.cs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Assets/Scripts/AudioManager.cs b/Assets/Scripts/AudioManager.cs new file mode 100644 index 0000000..1809375 --- /dev/null +++ b/Assets/Scripts/AudioManager.cs @@ -0,0 +1,44 @@ +using System; +using UnityEngine; +using UnityEngine.Audio; + +public class AudioManager : MonoBehaviour +{ + public Sound[] sounds; + + public static AudioManager instance; + + private void Awake() + { + if (instance == null) instance = this; + else { + Destroy(gameObject); + return; + } + + DontDestroyOnLoad(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; + s.source.loop = s.loop; + } + } + + private void Start() + { + Play("BackgroundMusic"); + } + + public void Play(string name) + { + Sound s = Array.Find(sounds, sound => sound.name == name); + if (s == null) { + Debug.LogWarning("Sound Error: " + name + " could not be played"); + return; + } + s.source.Play(); + } +} |