Skip to content

Instantly share code, notes, and snippets.

@tonymtz
Created September 24, 2019 03:34
Show Gist options
  • Save tonymtz/0e356f48c5c24377ca034e3af8c1ead2 to your computer and use it in GitHub Desktop.
Save tonymtz/0e356f48c5c24377ca034e3af8c1ead2 to your computer and use it in GitHub Desktop.
Unity Audio Manager for Sfx and Music
using UnityEngine;
public class PlayMusic : PlaySfx {
[SerializeField] private bool repeat;
public override void Play() {
soundManagerController.PlaySfx(audioClip, volume);
}
void OnDestroy() {
if (playWhen == SoundManagerController.EventType.BeforeDestroy) {
Play();
}
}
}
using System;
using UnityEngine;
public class PlaySfx : MonoBehaviour {
[SerializeField] protected SoundManagerController.EventType playWhen = SoundManagerController.EventType.Other;
[SerializeField] [Range(0, 1)] protected float volume = 1f;
[SerializeField] protected AudioClip audioClip;
protected SoundManagerController soundManagerController;
void Start() {
GameObject soundManager = GameObject.FindWithTag("SoundManager");
if (audioClip == null) {
throw new ArgumentException("No `AudioClip` added to script");
}
if (soundManager == null) {
throw new ArgumentException("No `SoundManager` object found");
}
soundManagerController = soundManager.GetComponent<SoundManagerController>();
if (soundManagerController == null) {
throw new ArgumentException("No `SoundManager` object found");
}
if (playWhen == SoundManagerController.EventType.StartScript) {
Play();
}
}
void OnDestroy() {
if (playWhen == SoundManagerController.EventType.BeforeDestroy) {
Play();
}
}
public virtual void Play() {
soundManagerController.PlayMusic(audioClip, volume);
}
}
using System.Collections.Generic;
using UnityEngine;
public class SoundManagerController : MonoBehaviour {
public enum EventType {
StartScript,
BeforeDestroy,
Other
}
private static SoundManagerController instance;
private AudioSource effectSource;
private AudioSource musicSource;
private float savedVolume = 0f;
public static SoundManagerController Instance => instance;
private void Awake() {
if (Instance == null) {
instance = this;
} else if (Instance != this) {
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
if (musicSource == null || effectSource == null) {
effectSource = gameObject.AddComponent<AudioSource>();
musicSource = gameObject.AddComponent<AudioSource>();
musicSource.ignoreListenerPause = true;
}
}
public void PlaySfx(AudioClip clip, float volume) {
// TODO multiply by global volume
effectSource.PlayOneShot(clip, volume);
}
public void PlayMusic(AudioClip clip, float volume) {
// TODO multiply by global volume
musicSource.clip = clip;
musicSource.volume = volume;
musicSource.Play();
}
public void TogglePauseSfx(bool pause) {
if (pause) {
AudioListener.pause = true;
savedVolume = musicSource.volume;
musicSource.volume /= 2;
} else {
AudioListener.pause = false;
musicSource.volume = savedVolume;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment