Advertisement
ChrisTutorials

AudioManager

May 25th, 2018
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.76 KB | None | 0 0
  1. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Audio Manager.
  4. //
  5. // This code is release under the MIT licence. It is provided as-is and without any warranty.
  6. //
  7. // Developed by Daniel Rodríguez (Seth Illgard) in April 2010 http://www.silentkraken.com
  8. //
  9. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
  10.  
  11. using System;
  12. using UnityEngine;
  13. using UnityEngine.Audio;
  14.  
  15. public class AudioManager : MonoBehaviour
  16. {
  17.     #region Public Fields
  18.  
  19.     public static AudioManager Instance;
  20.  
  21.     public AudioMixerGroup masterGroup;
  22.     public AudioMixer masterMixer;
  23.     public AudioMixerGroup musicGroup;
  24.  
  25.     public AudioMixerGroup soundGroup;
  26.  
  27.     #endregion Public Fields
  28.  
  29.     #region Public Enums
  30.  
  31.     public enum AudioChannel { Master, Sound, Music }
  32.  
  33.     #endregion Public Enums
  34.  
  35.     #region Public Methods
  36.  
  37.     /// <summary>
  38.     /// Plays a sound at the given point in space by creating an empty game object with an
  39.     /// AudioSource in that place and destroys it after it finished playing.
  40.     /// </summary>
  41.     /// <param name="clip"></param>
  42.     /// <param name="emitter"></param>
  43.     /// <param name="volume"></param>
  44.     /// <param name="pitch"></param>
  45.     /// <returns></returns>
  46.     public AudioSource CreatePlaySource(AudioClip clip, Transform emitter, float volume, float pitch, bool music = false)
  47.     {
  48.         GameObject go = new GameObject("Audio: " + clip.name);
  49.         go.transform.position = emitter.position;
  50.         go.transform.parent = emitter;
  51.  
  52.         //Create the source
  53.         AudioSource source = go.AddComponent<AudioSource>();
  54.         source.clip = clip;
  55.         source.volume = volume;
  56.         source.pitch = pitch;
  57.  
  58.         // Output sound through the sound group or music group
  59.         if (music)
  60.             source.outputAudioMixerGroup = musicGroup;
  61.         else
  62.             source.outputAudioMixerGroup = soundGroup;
  63.  
  64.         source.Play();
  65.         return source;
  66.     }
  67.  
  68.     public AudioSource Play(AudioClip clip, Transform emitter)
  69.     {
  70.         return Play(clip, emitter, 1f, 1f);
  71.     }
  72.  
  73.     public AudioSource Play(AudioClip clip, Transform emitter, float volume)
  74.     {
  75.         return Play(clip, emitter, volume, 1f);
  76.     }
  77.  
  78.     /// <summary>
  79.     /// Plays a sound by creating an empty game object with an AudioSource and attaching it to
  80.     /// the given transform (so it moves with the transform). Destroys it after it finished playing.
  81.     /// </summary>
  82.     /// <param name="clip"></param>
  83.     /// <param name="emitter"></param>
  84.     /// <param name="volume"></param>
  85.     /// <param name="pitch"></param>
  86.     /// <returns></returns>
  87.     public AudioSource Play(AudioClip clip, Transform emitter, float volume, float pitch)
  88.     {
  89.         //Create an empty game object
  90.         AudioSource source = CreatePlaySource(clip, emitter, volume, pitch);
  91.         Destroy(source.gameObject, clip.length);
  92.         return source;
  93.     }
  94.  
  95.     public AudioSource Play(AudioClip clip, Vector3 point)
  96.     {
  97.         return Play(clip, point, 1f, 1f);
  98.     }
  99.  
  100.     public AudioSource Play(AudioClip clip, Vector3 point, float volume)
  101.     {
  102.         return Play(clip, point, volume, 1f);
  103.     }
  104.  
  105.     /// <summary>
  106.     /// Plays a sound at the given point in space by creating an empty game object with an
  107.     /// AudioSource in that place and destroys it after it finished playing.
  108.     /// </summary>
  109.     /// <param name="clip"></param>
  110.     /// <param name="point"></param>
  111.     /// <param name="volume"></param>
  112.     /// <param name="pitch"></param>
  113.     /// <returns></returns>
  114.     public AudioSource Play(AudioClip clip, Vector3 point, float volume, float pitch)
  115.     {
  116.         AudioSource source = CreatePlaySource(clip, point, volume, pitch);
  117.         Destroy(source.gameObject, clip.length);
  118.         return source;
  119.     }
  120.  
  121.     /// <summary>
  122.     /// Plays the sound effect in a loop. Should destroy the audio source in your script when it
  123.     /// is ready to end.
  124.     /// </summary>
  125.     /// <param name="clip"></param>
  126.     /// <param name="point"></param>
  127.     /// <param name="volume"></param>
  128.     /// <param name="pitch"></param>
  129.     /// <returns></returns>
  130.     public AudioSource PlayLoop(AudioClip clip, Transform emitter, float volume = 1f, float pitch = 1f, bool music = true)
  131.     {
  132.         AudioSource source = CreatePlaySource(clip, emitter, volume, pitch, true);
  133.         source.loop = true;
  134.         return source;
  135.     }
  136.  
  137.     /// <summary>
  138.     /// Plays the sound effect in a loop. Should destroy the audio source in your script when it
  139.     /// is ready to end.
  140.     /// </summary>
  141.     /// <param name="clip"></param>
  142.     /// <param name="point"></param>
  143.     /// <param name="volume"></param>
  144.     /// <param name="pitch"></param>
  145.     /// <returns></returns>
  146.     public AudioSource PlayLoop(AudioClip clip, Vector3 point, float volume = 1f, float pitch = 1f, bool music = true)
  147.     {
  148.         AudioSource source = CreatePlaySource(clip, point, volume, pitch, true);
  149.         source.loop = true;
  150.         return source;
  151.     }
  152.  
  153.     public void SetVolume(AudioChannel channel, float volume)
  154.     {
  155.         // Converts the 0 - 100 input into decibles | volume = 1 is -40 DB / Mute. Volume 100 is
  156.         // 0 0 DB.
  157.         float adjustedVolume = -40 + (volume * 8 / 20);
  158.  
  159.         // Effectively completed muted if volume if 0
  160.         if (volume == 0)
  161.         {
  162.             adjustedVolume = -100;
  163.         }
  164.  
  165.         switch (channel)
  166.         {
  167.             case AudioChannel.Master:
  168.                 masterMixer.SetFloat("MasterVolume", adjustedVolume);
  169.                 break;
  170.  
  171.             case AudioChannel.Sound:
  172.                 masterMixer.SetFloat("SoundVolume", adjustedVolume);
  173.                 break;
  174.  
  175.             case AudioChannel.Music:
  176.                 masterMixer.SetFloat("MusicVolume", adjustedVolume);
  177.                 break;
  178.         }
  179.     }
  180.  
  181.     #endregion Public Methods
  182.  
  183.     #region Private Methods
  184.  
  185.     private void Awake()
  186.     {
  187.         //Check if instance already exists
  188.         if (Instance == null)
  189.         {
  190.             //if not, set instance to this
  191.             Instance = this;
  192.         }
  193.  
  194.         //If instance already exists and it's not this:
  195.         else if (Instance != this)
  196.  
  197.             //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
  198.             Destroy(gameObject);
  199.     }
  200.  
  201.     private AudioSource CreatePlaySource(AudioClip clip, Vector3 point, float volume, float pitch, bool music = false)
  202.     {
  203.         //Create an empty game object
  204.         GameObject go = new GameObject("Audio: " + clip.name);
  205.         go.transform.position = point;
  206.  
  207.         //Create the source
  208.         AudioSource source = go.AddComponent<AudioSource>();
  209.         source.clip = clip;
  210.         source.volume = volume;
  211.         source.pitch = pitch;
  212.  
  213.         // Output sound through the sound group or music group
  214.         if (music)
  215.             source.outputAudioMixerGroup = musicGroup;
  216.         else
  217.             source.outputAudioMixerGroup = soundGroup;
  218.  
  219.         source.Play();
  220.         return source;
  221.     }
  222.  
  223.     /// <summary>
  224.     /// Set up audio levels
  225.     /// </summary>
  226.     private void Start()
  227.     {
  228.         // Set the audio levels from player preferences
  229.         float masterVolume = PlayerPrefs.GetFloat("MasterVolume", 100);
  230.         float soundVolume = PlayerPrefs.GetFloat("SoundVolume", 100);
  231.         float musicVolume = PlayerPrefs.GetFloat("MusicVolume", 100);
  232.  
  233.         // Update the audio mixer
  234.         SetVolume(AudioChannel.Master, masterVolume);
  235.         SetVolume(AudioChannel.Sound, soundVolume);
  236.         SetVolume(AudioChannel.Music, musicVolume);
  237.     }
  238.  
  239.     #endregion Private Methods
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement