Audio manager for the sound in the game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine.Audio; | |
using System; | |
using UnityEngine; | |
//Credit to Brackeys youtube tutorial on Audio managers, as the majority of this code and learning how to use it was made by him. | |
public partial class SAudioManager : MonoBehaviour | |
{ | |
public Sound[] sounds; | |
public static SAudioManager instance; | |
//AudioManager | |
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; | |
} | |
} | |
void Start() | |
{ | |
Play("Theme"); | |
} | |
public void Play(string name) | |
{ | |
Sound s = Array.Find(sounds, sound => sound.name == name); | |
if (s == null) | |
{ | |
Debug.LogWarning("Sound: " + name + " not found"); | |
return; | |
} | |
s.source.Play(); | |
} | |
//this addition to the code was made by me, the rest was from Brackeys tutorial | |
public void Stop(string name) | |
{ | |
Sound s = Array.Find(sounds, sound => sound.name == name); | |
s.source.Stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How thanks I was looking for this