Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Audio manager for the sound in the game
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
You can’t perform that action at this time.