From e6e9c0826f2d622caefa5e1a99d643a85f4c058f Mon Sep 17 00:00:00 2001 From: cross28 Date: Mon, 18 Apr 2022 04:10:52 -0500 Subject: feat: Rewrote the audio manager. Added background music --- Assets/Scripts/AudioManager.cs | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Assets/Scripts/AudioManager.cs (limited to 'Assets/Scripts/AudioManager.cs') 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(); + 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(); + } +} -- cgit v1.2.3-70-g09d2