using System.Collections; using System.Collections.Generic; using UnityEngine; public class AudioPlayer : MonoBehaviour { // ========================================================================= // Background Themes public AudioSource BackgroundThemeBotanicalRoom; public AudioSource BackgroundThemeMachineRoom; public AudioSource BackgroundThemeCryoRoom; public AudioSource BackgroundThemeKitchenRoom; public AudioSource BackgroundThemeControlRoom; private AudioSource CurrentBackgroundTheme; // Sound Fx public AudioSource FxClick1; public AudioSource FxClick2; public AudioSource FxClick3; public AudioSource FxClick4; public AudioSource FxExplosion; public AudioSource FxData; public AudioSource FxSuitNoise; public AudioSource FxSquelch1; public AudioSource FxSquelch2; public AudioSource FxLaser1; public AudioSource FxLaser2; // Parameters for fading between background themes public float FadeSpeed = 0.025f; public float FadeDelay = 0.01f; public float Volume = 1f; // ========================================================================= // Singleton public static AudioPlayer Instance = null; private void Awake() { if (Instance == null) { Instance = this; } else if (Instance != this) { Destroy(gameObject); } DontDestroyOnLoad(gameObject); } // ========================================================================= // Public API public void PlayFxClick1() { PlayFx(FxClick1); } public void PlayFxClick2() { PlayFx(FxClick2); } public void PlayFxClick3() { PlayFx(FxClick3); } public void PlayFxClick4() { PlayFx(FxClick4); } public void PlayFxExplosion() { PlayFx(FxExplosion); } public void PlayFxData() { PlayFx(FxData); } public void PlayFxSuitNoise() { PlayFx(FxSuitNoise); } public void PlayFxSquelch1() { PlayFx(FxSquelch1); } public void PlayFxSquelch2() { PlayFx(FxSquelch2); } public void PlayFxLaser1() { PlayFx(FxLaser1); } public void PlayFxLaser2() { PlayFx(FxLaser2); } public void PlayBackgroundThemeBotanicalRoom() { PlayBackgroundTheme(BackgroundThemeBotanicalRoom); } public void PlayBackgroundThemeMachineRoom() { PlayBackgroundTheme(BackgroundThemeMachineRoom); } public void PlayBackgroundThemeCryoRoom() { PlayBackgroundTheme(BackgroundThemeCryoRoom); } public void PlayBackgroundThemeKitchenRoom() { PlayBackgroundTheme(BackgroundThemeKitchenRoom); } public void PlayBackgroundThemeControlRoom() { PlayBackgroundTheme(BackgroundThemeControlRoom); } // ========================================================================= // Internal control private void PlayFx(AudioSource fx) { if (fx.isPlaying) { fx.Stop(); fx.time = 0; } fx.Play(); } private void PlayBackgroundTheme(AudioSource backgroundTheme) { StartCoroutine( FadeNextBackgroundTheme( CurrentBackgroundTheme, backgroundTheme, FadeSpeed, FadeDelay, Volume ) ); CurrentBackgroundTheme = backgroundTheme; } // Background theme fade in/ fade out static IEnumerator FadeNextBackgroundTheme( AudioSource current, AudioSource next, float fadeSpeed, float fadeDelay, float volume ) { // Fade out current clip, if neccessary if (current != null) { while (current.volume > 0) { current.volume -= fadeSpeed * volume; yield return new WaitForSeconds(fadeDelay); } current.Stop(); } // Fade in next clip next.loop = true; next.volume = 0; next.Play(); while (next.volume < volume) { next.volume += fadeSpeed * volume; yield return new WaitForSeconds(fadeDelay); } } }