Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.15 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SoundManager : MonoBehaviour {
  6.  
  7.     static SoundManager instance;
  8.     public static SoundManager GetInstance()
  9.     {
  10.         if (!instance)
  11.         {
  12.             GameObject soundManager = new GameObject("SoundManager");
  13.             instance = soundManager.AddComponent(typeof(SoundManager)) as SoundManager;
  14.             instance.Initialize();
  15.         }
  16.  
  17.         return instance;
  18.     }
  19.  
  20.     const float MaxVolume_BGM = 1f;
  21.     const float MaxVolume_SFX = 1f;
  22.     static float CurrentVolumeNormalized_BGM = 1f;
  23.     static float CurrentVolumeNormalized_SFX = 1f;
  24.     static bool isMuted = false;
  25.  
  26.     List<AudioSource> sfxSources;
  27.     AudioSource bgmSource;
  28.  
  29.  
  30.  
  31.     void Initialize()
  32.     {
  33.         // add our bgm sound source
  34.         bgmSource = gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
  35.         bgmSource.loop = true;
  36.         bgmSource.playOnAwake = false;
  37.         bgmSource.volume = GetBGMVolume();
  38.         DontDestroyOnLoad(gameObject);
  39.     }
  40.  
  41.     // ==================== Volume Getters =====================
  42.  
  43.     static float GetBGMVolume()
  44.     {
  45.         return isMuted ? 0f : MaxVolume_BGM * CurrentVolumeNormalized_BGM;
  46.     }
  47.  
  48.     public static float GetSFXVolume()
  49.     {
  50.         return isMuted ? 0f : MaxVolume_SFX * CurrentVolumeNormalized_SFX;
  51.     }
  52.  
  53.     // ====================== BGM Utils ======================
  54.  
  55.     void FadeBGMOut(float fadeDuration)
  56.     {
  57.         SoundManager soundMan = GetInstance();
  58.         float delay = 0f;
  59.         float toVolume = 0f;
  60.  
  61.         if (soundMan.bgmSource.clip == null)
  62.         {
  63.             Debug.LogError("Error: Could not fade BGM out as BGM AudioSource has no currently playing clip.");
  64.         }
  65.  
  66.         StartCoroutine(FadeBGM(toVolume, delay, fadeDuration));
  67.     }
  68.  
  69.     void FadeBGMIn(AudioClip bgmClip, float delay, float fadeDuration)
  70.     {
  71.         SoundManager soundMan = GetInstance();
  72.         soundMan.bgmSource.clip = bgmClip;
  73.         soundMan.bgmSource.Play();
  74.  
  75.         float toVolume = GetBGMVolume();
  76.  
  77.         StartCoroutine(FadeBGM(toVolume, delay, fadeDuration));
  78.     }
  79.  
  80.     IEnumerator FadeBGM(float fadeToVolume, float delay, float duration)
  81.     {
  82.         yield return new WaitForSeconds(delay);
  83.  
  84.         SoundManager soundMan = GetInstance();
  85.         float elapsed = 0f;
  86.         while (duration > 0)
  87.         {
  88.             float t = (elapsed / duration);
  89.             float volume = Mathf.Lerp(0f, fadeToVolume * CurrentVolumeNormalized_BGM, t);
  90.             soundMan.bgmSource.volume = volume;
  91.  
  92.             elapsed += Time.deltaTime;
  93.             yield return 0;
  94.         }
  95.     }
  96.  
  97.     // ====================== BGM Functions ======================
  98.  
  99.     public static void PlayBGM(AudioClip bgmClip, bool fade, float fadeDuration)
  100.     {
  101.         SoundManager soundMan = GetInstance();
  102.  
  103.         if (fade)
  104.         {
  105.             if (soundMan.bgmSource.isPlaying)
  106.             {
  107.                 // fade out, then switch and fade in
  108.                 soundMan.FadeBGMOut(fadeDuration / 2);
  109.                 soundMan.FadeBGMIn(bgmClip, fadeDuration / 2, fadeDuration / 2);
  110.  
  111.             }
  112.             else
  113.             {
  114.                 // just fade in
  115.                 float delay = 0f;
  116.                 soundMan.FadeBGMIn(bgmClip, delay, fadeDuration);
  117.             }
  118.         }
  119.         else
  120.         {
  121.             // play immediately
  122.             soundMan.bgmSource.volume = GetBGMVolume();
  123.             soundMan.bgmSource.clip = bgmClip;
  124.             soundMan.bgmSource.Play();
  125.         }
  126.     }
  127.  
  128.     public static void StopBGM(bool fade, float fadeDuration)
  129.     {
  130.         SoundManager soundMan = GetInstance();
  131.         if (soundMan.bgmSource.isPlaying)
  132.         {
  133.             // fade out, then switch and fade in
  134.             if (fade)
  135.             {
  136.                 soundMan.FadeBGMOut(fadeDuration);
  137.             }
  138.             else
  139.             {
  140.                 soundMan.bgmSource.Stop();
  141.             }
  142.         }
  143.     }
  144.  
  145.     // ======================= SFX Utils ====================================
  146.  
  147.     AudioSource GetSFXSource()
  148.     {
  149.         // set up a new sfx sound source for each new sfx clip
  150.         AudioSource sfxSource = gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
  151.         sfxSource.loop = false;
  152.         sfxSource.playOnAwake = false;
  153.         sfxSource.volume = GetSFXVolume();
  154.  
  155.         if (sfxSources == null)
  156.         {
  157.             sfxSources = new List<AudioSource>();
  158.         }
  159.  
  160.         sfxSources.Add(sfxSource);
  161.  
  162.         return sfxSource;
  163.     }
  164.  
  165.     IEnumerator RemoveSFXSource(AudioSource sfxSource)
  166.     {
  167.         yield return new WaitForSeconds(sfxSource.clip.length);
  168.         sfxSources.Remove(sfxSource);
  169.         Destroy(sfxSource);
  170.     }
  171.  
  172.     private void RemoveSFXSourcee(AudioSource sfxSource)
  173.     {
  174.         if (!sfxSource.isPlaying)
  175.         {
  176.             sfxSources.Remove(sfxSource);
  177.             Destroy(sfxSource);
  178.         }
  179.     }
  180.  
  181.     IEnumerator RemoveSFXSourceFixedLength(AudioSource sfxSource, float length)
  182.     {
  183.         yield return new WaitForSeconds(length);
  184.         sfxSources.Remove(sfxSource);
  185.         Destroy(sfxSource);
  186.     }
  187.  
  188.     // ====================== SFX Functions =================================
  189.  
  190.     public static void PlaySFX(AudioClip sfxClip)
  191.     {
  192.         SoundManager soundMan = GetInstance();
  193.         AudioSource source = soundMan.GetSFXSource();
  194.         source.volume = GetSFXVolume();
  195.         source.clip = sfxClip;
  196.         source.Play();
  197.  
  198.         //soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));
  199.         soundMan.RemoveSFXSourcee(source);
  200.     }
  201.  
  202.     public static void PlaySFXRandomized(AudioClip sfxClip)
  203.     {
  204.         SoundManager soundMan = GetInstance();
  205.         AudioSource source = soundMan.GetSFXSource();
  206.         source.volume = GetSFXVolume();
  207.         source.clip = sfxClip;
  208.         source.pitch = Random.Range(0.85f, 1.2f);
  209.         source.Play();
  210.  
  211.         // soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));
  212.         soundMan.RemoveSFXSourcee(source);
  213.  
  214.     }
  215.  
  216.     public static void PlaySFXFixedDuration(AudioClip sfxClip, float duration, float volumeMultiplier = 1.0f)
  217.     {
  218.         SoundManager soundMan = GetInstance();
  219.         AudioSource source = soundMan.GetSFXSource();
  220.         source.volume = GetSFXVolume() * volumeMultiplier;
  221.         source.clip = sfxClip;
  222.         source.loop = true;
  223.         source.Play();
  224.  
  225.         soundMan.StartCoroutine(soundMan.RemoveSFXSourceFixedLength(source, duration));
  226.     }
  227.  
  228.     // ==================== Volume Control Functions ==========================
  229.  
  230.     public static void DisableSoundImmediate()
  231.     {
  232.         SoundManager soundMan = GetInstance();
  233.         soundMan.StopAllCoroutines();
  234.         if (soundMan.sfxSources != null)
  235.         {
  236.             foreach (AudioSource source in soundMan.sfxSources)
  237.             {
  238.                 source.volume = 0;
  239.             }
  240.         }
  241.         soundMan.bgmSource.volume = 0f;
  242.         isMuted = true;
  243.     }
  244.  
  245.     public static void EnableSoundImmediate()
  246.     {
  247.         SoundManager soundMan = GetInstance();
  248.         if (soundMan.sfxSources != null)
  249.         {
  250.             foreach (AudioSource source in soundMan.sfxSources)
  251.             {
  252.                 source.volume = GetSFXVolume();
  253.             }
  254.         }
  255.         soundMan.bgmSource.volume = GetBGMVolume();
  256.         isMuted = false;
  257.     }
  258.  
  259.     public static void SetGlobalVolume(float newVolume)
  260.     {
  261.         CurrentVolumeNormalized_BGM = newVolume;
  262.         CurrentVolumeNormalized_SFX = newVolume;
  263.         AdjustSoundImmediate();
  264.     }
  265.  
  266.     public static void SetSFXVolume(float newVolume)
  267.     {
  268.         CurrentVolumeNormalized_SFX = newVolume;
  269.         AdjustSoundImmediate();
  270.     }
  271.  
  272.     public static void SetBGMVolume(float newVolume)
  273.     {
  274.         CurrentVolumeNormalized_BGM = newVolume;
  275.         AdjustSoundImmediate();
  276.     }
  277.  
  278.     public static void AdjustSoundImmediate()
  279.     {
  280.         SoundManager soundMan = GetInstance();
  281.         if (soundMan.sfxSources != null)
  282.         {
  283.             foreach (AudioSource source in soundMan.sfxSources)
  284.             {
  285.                 source.volume = GetSFXVolume();
  286.             }
  287.         }
  288.         Debug.Log("BGM Volume: " + GetBGMVolume());
  289.         soundMan.bgmSource.volume = GetBGMVolume();
  290.         Debug.Log("BGM Volume is now: " + GetBGMVolume());
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement