Advertisement
uglenXD

audioManager.cs

Apr 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class audioManager : MonoBehaviour
  6. {
  7.     public static audioManager instance;
  8.  
  9.     public static SONG activeSong = null;
  10.     public static List<SONG> allSongs = new List<SONG>();
  11.  
  12.     public float songTransitionSpeed = 2f;
  13.     public bool songSmoothTransitions = true;
  14.  
  15.     void Awake()
  16.     {
  17.         if(instance == null)
  18.         {
  19.             instance = this;
  20.             DontDestroyOnLoad(gameObject);
  21.         }
  22.         else
  23.         {
  24.             DestroyImmediate(gameObject);
  25.         }
  26.     }
  27.  
  28.     public void PlaySFX(AudioClip effect, float volume = 1f, float pitch = 1f)
  29.     {
  30.         AudioSource source = CreateNewSource(string.Format("SFX [{0}]", effect.name));
  31.         source.clip = effect;
  32.         source.volume = volume;
  33.         source.pitch = pitch;
  34.         source.Play();
  35.  
  36.         Destroy(source.gameObject, effect.length);
  37.     }
  38.  
  39.     public void PlaySong(AudioClip song, float maxVolume = 1f, float pitch = 1f, float startingVolume = 0f, bool playOnStart = true, bool loop = true)
  40.     {
  41.         if(song != null)
  42.         {
  43.             for(int i = 0; i < allSongs.Count; i++)
  44.             {
  45.                 SONG s = allSongs[i];
  46.                 if(s.clip == song)
  47.                 {
  48.                     activeSong = s;
  49.                     break;
  50.                 }
  51.             }
  52.             if(activeSong == null || activeSong.clip != song)
  53.             {
  54.                 activeSong = new SONG(song, maxVolume, pitch, startingVolume, playOnStart, loop);
  55.             }
  56.         }
  57.         else
  58.         {
  59.             activeSong = null;
  60.         }
  61.  
  62.         StopAllCoroutines();
  63.         StartCoroutine(VolumeLeveling());
  64.  
  65.  
  66.     }
  67.  
  68.     IEnumerator VolumeLeveling()
  69.     {
  70.         while(TransitionSongs())
  71.         {
  72.             yield return new WaitForEndOfFrame();
  73.         }
  74.     }
  75.  
  76.     bool TransitionSongs()
  77.     {
  78.         bool anyValueChanged = false;
  79.  
  80.         float speed = songTransitionSpeed * Time.deltaTime;
  81.         for (int i = allSongs.Count - 1; i >= 0; i--)
  82.         {
  83.             SONG song = allSongs[i];
  84.             if (song == activeSong)
  85.             {
  86.                 if (song.volume < song.maxVolume)
  87.                 {
  88.                     song.volume = songSmoothTransitions ? Mathf.Lerp(song.volume, song.maxVolume, speed) : Mathf.MoveTowards(song.volume, song.maxVolume, speed);
  89.                     anyValueChanged = true;
  90.                 }
  91.             }
  92.             else
  93.             {
  94.                 if (song.volume > 0)
  95.                 {
  96.                     song.volume = songSmoothTransitions ? Mathf.Lerp(song.volume, 0f, speed) : Mathf.MoveTowards(song.volume, 0f, speed);
  97.                     anyValueChanged = true;
  98.                 }
  99.                 else
  100.                 {
  101.                     allSongs.RemoveAt(i);
  102.                     song.DestroySong();
  103.                     continue;
  104.                 }
  105.             }
  106.         }
  107.  
  108.         return anyValueChanged;
  109.     }
  110.  
  111.  
  112.     public static AudioSource CreateNewSource(string _name)
  113.     {
  114.         AudioSource newSource = new GameObject(_name).AddComponent<AudioSource>();
  115.         newSource.transform.SetParent(instance.transform);
  116.         return newSource;
  117.     }
  118.  
  119.     [System.Serializable]
  120.     public class SONG
  121.     {
  122.         public AudioSource source;
  123.         public AudioClip clip { get { return source.clip; } set { source.clip = value; } }
  124.  
  125.         public float maxVolume = 1f;
  126.  
  127.         public SONG(AudioClip clip, float _maxVolume, float pitch, float startingVolume, bool playOnStart, bool loop)
  128.         {
  129.             source = audioManager.CreateNewSource(string.Format("SONG [{0}]", clip.name));
  130.             source.clip = clip;
  131.             source.volume = startingVolume;
  132.             maxVolume = _maxVolume;
  133.             source.pitch = pitch;
  134.             source.loop = loop;
  135.  
  136.             audioManager.allSongs.Add(this);
  137.  
  138.             if(playOnStart)
  139.             {
  140.                 source.Play();
  141.             }
  142.         }
  143.  
  144.         public float volume { get { return source.volume; } set { source.volume = value; } }
  145.         public float pitch { get { return source.pitch; } set { source.pitch = value; } }
  146.  
  147.         public void Play()
  148.         {
  149.             source.Play();
  150.         }
  151.  
  152.         public void Stop()
  153.         {
  154.             source.Stop();
  155.         }
  156.  
  157.         public void Pause()
  158.         {
  159.             source.Pause();
  160.         }
  161.  
  162.         public void UnPause()
  163.         {
  164.             source.UnPause();
  165.         }
  166.  
  167.         public void DestroySong()
  168.         {
  169.             audioManager.allSongs.Remove(this);
  170.             DestroyImmediate(source.gameObject);
  171.         }
  172.     }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement