Created
September 24, 2019 03:34
-
-
Save tonymtz/0e356f48c5c24377ca034e3af8c1ead2 to your computer and use it in GitHub Desktop.
Unity Audio Manager for Sfx and Music
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 PlayMusic : PlaySfx { | |
[SerializeField] private bool repeat; | |
public override void Play() { | |
soundManagerController.PlaySfx(audioClip, volume); | |
} | |
void OnDestroy() { | |
if (playWhen == SoundManagerController.EventType.BeforeDestroy) { | |
Play(); | |
} | |
} | |
} |
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 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); | |
} | |
} |
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 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