Advertisement
GaelVanhalst

Linked: AudioManager

Jan 25th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.77 KB | None | 0 0
  1. public static class AudioManager
  2. {
  3.     //Music
  4.     private static AudioClip _currentSong;
  5.     private static AudioSource _musicSource;
  6.     private static float _musicVolume = 1.0f;
  7.     private static bool _muteMusic;
  8.  
  9.     public static float MusicVolume
  10.     {
  11.         get
  12.         {
  13.             if (_muteMusic) return 0;
  14.             return _musicVolume;
  15.         }
  16.         set
  17.         {
  18.             _musicVolume = Mathf.Clamp(value, 0, 1);
  19.             UpdateMusicVolume();
  20.         }
  21.     }
  22.  
  23.     public static float UnmutedMusicVolume
  24.     {
  25.         get { return _musicVolume; }
  26.     }
  27.  
  28.     public static bool MuteMusic
  29.     {
  30.         get { return _muteMusic; }
  31.         set
  32.         {
  33.             _muteMusic = value;
  34.             UpdateMusicVolume();
  35.         }
  36.     }
  37.  
  38.     //Sounds
  39.     private static List<GameAudio> _gameAudios = new List<GameAudio>();
  40.     private static Queue<GameObject> _oneShotAudioPool = new Queue<GameObject>();
  41.     private static GameObject _oneShotAudioObject;
  42.     private static float _soundVolume = 1.0f;
  43.  
  44.     private static bool _muteSound;
  45.  
  46.     public static float SoundVolume
  47.     {
  48.         get
  49.         {
  50.             if (_muteSound) return 0;
  51.             return _soundVolume;
  52.         }
  53.         set
  54.         {
  55.             _soundVolume = Mathf.Clamp(value, 0, 1);
  56.             UpdateSoundVolume();
  57.         }
  58.     }
  59.  
  60.     public static float UnmutedSoundVolume
  61.     {
  62.         get { return _soundVolume; }
  63.     }
  64.  
  65.     public static bool MuteSound
  66.     {
  67.         get { return _muteSound; }
  68.         set
  69.         {
  70.             _muteSound = value;
  71.             UpdateSoundVolume();
  72.         }
  73.     }
  74.  
  75.     //Music
  76.     /// <summary>
  77.     /// Will play last song if song is null
  78.     /// </summary>
  79.     /// <param name="song">Can be null</param>
  80.     public static void PlayMusic(AudioClip song)
  81.     {
  82.         var differentSong = false;
  83.         if (song != _currentSong)
  84.         {
  85.             _currentSong = song;
  86.             differentSong = true;
  87.         }
  88.         if (_musicSource)
  89.         {
  90.             if (_currentSong)
  91.             {
  92.                 _musicSource.volume = MusicVolume;
  93.                 if (differentSong)
  94.                 {
  95.                     _musicSource.clip = _currentSong;
  96.                     _musicSource.Play();
  97.                 }
  98.                 else if (!_musicSource.isPlaying) _musicSource.Play();
  99.             }
  100.             else
  101.             {
  102.                 _musicSource.Stop();
  103.                 _musicSource.clip = null;
  104.             }
  105.         }
  106.     }
  107.  
  108.     public static void SetMusicSource(GameObject musicSource)
  109.     {
  110.         if (_musicSource) return;
  111.  
  112.         var newMusicSourceObject = Object.Instantiate(musicSource);
  113.         Object.DontDestroyOnLoad(newMusicSourceObject);
  114.         _musicSource = newMusicSourceObject.GetComponent<AudioSource>();
  115.         if (_musicSource.clip) _musicSource.Play();
  116.     }
  117.  
  118.     public static void PauseMusic(bool pause)
  119.     {
  120.         if (!_musicSource) return;
  121.         if (pause)
  122.         {
  123.             _musicSource.Pause();
  124.         }
  125.         else
  126.         {
  127.             _musicSource.UnPause();
  128.         }
  129.     }
  130.  
  131.     public static void UpdateMusicVolume()
  132.     {
  133.         if (_musicSource != null)
  134.         {
  135.             _musicSource.volume = MusicVolume;
  136.         }
  137.     }
  138.  
  139.     //Sounds
  140.  
  141.         //Get random pitch
  142.     public static float GetPitchByRangeShift(float min, float max)
  143.     {
  144.         if (max < min)
  145.         {
  146.             var temp = min;
  147.             min = max;
  148.             max = temp;
  149.         }
  150.         float pitch = Random.Range(min, max);
  151.         if (pitch < 0)
  152.         {
  153.             pitch = 1/(1 - pitch);
  154.         }
  155.         else
  156.         {
  157.             pitch += 1;
  158.         }
  159.  
  160.         return pitch;
  161.     }
  162.  
  163.     /// <summary>
  164.     /// Is for audio that can also be played in reverse
  165.     /// </summary>
  166.     public static void PlayNormal(AudioSource audio, float pitch = 1.0f)
  167.     {
  168.         audio.pitch = pitch;
  169.         if (audio.isPlaying) return;
  170.         audio.timeSamples = 0;
  171.         audio.Play();
  172.     }
  173.  
  174.     public static void PlayInReverse(AudioSource audio, float pitch = 1.0f)
  175.     {
  176.         audio.pitch = -pitch;
  177.         if (audio.isPlaying) return;
  178.         audio.timeSamples = audio.clip.samples - 1;
  179.         audio.Play();
  180.     }
  181.  
  182.     public static void PrepareOneShotAudioPool(int amountPrepooled, GameObject oneShotAudioObject)
  183.     {
  184.         if (_oneShotAudioPool == null) _oneShotAudioPool = new Queue<GameObject>();
  185.         else _oneShotAudioPool.Clear();
  186.  
  187.         if (oneShotAudioObject)
  188.         {
  189.             if (oneShotAudioObject.GetComponent<OneShotAudio>()) _oneShotAudioObject = oneShotAudioObject;
  190.         }
  191.  
  192.         for (int i = 0; i < amountPrepooled; i++)
  193.         {
  194.             var spawnedObject = Object.Instantiate(_oneShotAudioObject);
  195.             if (spawnedObject == null) break;
  196.             PutBackOnOneShotAudioPool(spawnedObject.GetComponent<OneShotAudio>());
  197.         }
  198.     }
  199.  
  200.     public static void PutBackOnOneShotAudioPool(OneShotAudio oneShotAudio)
  201.     {
  202.         if (oneShotAudio)
  203.         {
  204.             _oneShotAudioPool.Enqueue(oneShotAudio.gameObject);
  205.             oneShotAudio.gameObject.SetActive(false);
  206.         }
  207.     }
  208.  
  209.     public static OneShotAudio GetOneShotAudio(Vector3 position)
  210.     {
  211.         if (_oneShotAudioObject == null)
  212.         {
  213.             Debug.LogError("No OneShotAudioObject source founded");
  214.             return null;
  215.         }
  216.         GameObject oneShotAudio = null;
  217.         bool succes = false;
  218.  
  219.         while (_oneShotAudioPool.Count > 0)
  220.         {
  221.             oneShotAudio = _oneShotAudioPool.Dequeue();
  222.             if (oneShotAudio == null) continue;
  223.             var script = oneShotAudio.GetComponent<OneShotAudio>();
  224.             if (script == null)
  225.             {
  226.                 Object.Destroy(oneShotAudio);
  227.                 continue;
  228.             }
  229.             oneShotAudio.SetActive(true);
  230.             script.Reset();
  231.  
  232.             succes = true;
  233.             break;
  234.         }
  235.         if (!succes) oneShotAudio = Object.Instantiate(_oneShotAudioObject);
  236.         oneShotAudio.transform.position = position;
  237.         return oneShotAudio.GetComponent<OneShotAudio>();
  238.     }
  239.  
  240.     public static void PauseSounds(bool pause)
  241.     {
  242.         if (pause)
  243.         {
  244.             CleanAudioList();
  245.             for (int i = 0; i < _gameAudios.Count; i++)
  246.             {
  247.                 var gameAudio = _gameAudios[i];
  248.                 if (!gameAudio.AudioSource) continue;
  249.                 if (gameAudio.AudioSource.isPlaying) gameAudio.AudioSource.Pause();
  250.             }
  251.         }
  252.         else
  253.         {
  254.             CleanAudioList();
  255.             for (int i = 0; i < _gameAudios.Count; i++)
  256.             {
  257.                 var gameAudio = _gameAudios[i];
  258.                 if (gameAudio.AudioSource == null) continue;
  259.                 gameAudio.AudioSource.UnPause();
  260.             }
  261.         }
  262.     }
  263.  
  264.     public static void AddGameAudio(GameAudio gameAudio)
  265.     {
  266.         gameAudio.UpdateVolume();
  267.         _gameAudios.Add(gameAudio);
  268.     }
  269.  
  270.     public static void RemoveGameAudio(GameAudio gameAudio)
  271.     {
  272.         _gameAudios.Remove(gameAudio);
  273.     }
  274.  
  275.     public static void InitializeAudioList()
  276.     {
  277.         if (_gameAudios != null) _gameAudios.Clear();
  278.         else _gameAudios = new List<GameAudio>();
  279.     }
  280.  
  281.     public static void CleanAudioList()
  282.     {
  283.         if (_gameAudios == null) _gameAudios = new List<GameAudio>();
  284.         _gameAudios.RemoveAll(item => item == null);
  285.         for (int i = 0; i < _gameAudios.Count - 1; i++)
  286.         {
  287.             for (int j = i + 1; j < _gameAudios.Count;)
  288.             {
  289.                 if (_gameAudios[i] == _gameAudios[j])
  290.                 {
  291.                     _gameAudios.RemoveAt(j);
  292.                 }
  293.                 else j++;
  294.             }
  295.         }
  296.     }
  297.  
  298.     public static void UpdateSoundVolume()
  299.     {
  300.         bool requestClean = false;
  301.         for (int i = 0; i < _gameAudios.Count; i++)
  302.         {
  303.             var gameAudio = _gameAudios[i];
  304.             if (gameAudio == null)
  305.             {
  306.                 requestClean = true;
  307.                 continue;
  308.             }
  309.             if (gameAudio.AudioSource != null)
  310.             {
  311.                 gameAudio.UpdateVolume();
  312.             }
  313.         }
  314.         if (requestClean) CleanAudioList();
  315.     }
  316. }
  317.  
  318. [Serializable]
  319. public class GameAudio
  320. {
  321.     [SerializeField]
  322.     private AudioSource _audioSource;
  323.  
  324.     [SerializeField]
  325.     private float _unscaledVolume=1.0f;
  326.     public AudioSource AudioSource
  327.     {
  328.         get { return _audioSource; }
  329.         set
  330.         {
  331.             _audioSource = value;
  332.             UpdateVolume();
  333.         }
  334.     }
  335.  
  336.     public AudioClip AudioClip
  337.     {
  338.         get
  339.         {
  340.             if (_audioSource == null) return null;
  341.             return _audioSource.clip;
  342.         }
  343.         set
  344.         {
  345.             if (_audioSource == null)
  346.             {
  347.                 Debug.LogError("There is no audio source, so clip can't be set!");
  348.                 return;
  349.             }
  350.             _audioSource.clip = value;
  351.         }
  352.     }
  353.  
  354.     public float UnscaledVolume
  355.     {
  356.         get { return _unscaledVolume; }
  357.         set
  358.         {
  359.             _unscaledVolume = value;
  360.             UpdateVolume();
  361.         }
  362.     }
  363.  
  364.     public GameAudio(AudioSource audioSource, float unscaledVolume=1.0f)
  365.     {
  366.         _audioSource = audioSource;
  367.         _unscaledVolume = unscaledVolume;
  368.         GenericUpdateVolume();
  369.     }
  370.  
  371.     private float GenericUpdateVolume()
  372.     {
  373.         if (AudioSource == null) return -1;
  374.         AudioSource.volume = UnscaledVolume * AudioManager.SoundVolume;
  375.         return AudioSource.volume;
  376.     }
  377.     public virtual float UpdateVolume()
  378.     {
  379.         return GenericUpdateVolume();
  380.     }
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement