Created
February 8, 2016 20:27
-
-
Save YoriKv/a3359965cd48f63782a4 to your computer and use it in GitHub Desktop.
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; | |
public class SimpleSoundManager:MonoBehaviour { | |
// Audio source | |
private AudioSource musicSrc; | |
private AudioSource effectSrc; | |
// Instance variable | |
private static SimpleSoundManager instance; | |
// Instance | |
public static SimpleSoundManager I { | |
get { | |
if(instance == null && Application.isPlaying) { | |
// Create container gameobject | |
GameObject smc = new GameObject("SimpleSoundManagerContainer"); | |
DontDestroyOnLoad(smc); // Persist | |
// Add sound manager as component | |
instance = smc.AddComponent<SimpleSoundManager>(); | |
} | |
return instance; | |
} | |
} | |
// Volume | |
public float MusicVolume { | |
get { return musicSrc.volume; } | |
set { musicSrc.volume = value; } | |
} | |
public float EffectVolume { | |
get { return effectSrc.volume; } | |
set { effectSrc.volume = value; } | |
} | |
// Init audio sources | |
public void Awake() { | |
// Add audio sources | |
musicSrc = gameObject.AddComponent<AudioSource>(); | |
effectSrc = gameObject.AddComponent<AudioSource>(); | |
// Set volumes | |
EffectVolume = MusicVolume = 1f; | |
} | |
/*********************** EFFECTS & MUSIC ****************************/ | |
// Single Effect | |
public void PlayEffectOnce(AudioClip snd) { | |
effectSrc.PlayOneShot(snd); | |
} | |
// Random Effect from list | |
public void PlayEffectOnce(AudioClip[] snds) { | |
if(snds.Length > 0) | |
PlayEffectOnce(snds[Random.Range(0, snds.Length)]); | |
} | |
// Music | |
public void PlayMusic(AudioClip snd, bool loop) { | |
musicSrc.clip = snd; | |
musicSrc.loop = loop; | |
musicSrc.Play(); | |
} | |
public void StopMusic() { | |
musicSrc.Stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment