Guest User

Untitled

a guest
Mar 6th, 2015
299
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*****************************************
  2.  * This file is part of Impulse Framework.
  3.  
  4.     Impulse Framework is free software: you can redistribute it and/or modify
  5.     it under the terms of the GNU Lesser General Public License as published by
  6.     the Free Software Foundation, either version 3 of the License, or
  7.     any later version.
  8.  
  9.     Impulse Framework is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU Lesser General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Lesser General Public License
  15.     along with Impulse Framework.  If not, see <http://www.gnu.org/licenses/>.
  16. *****************************************/
  17.  
  18. using UnityEngine;
  19. using System.Collections;
  20. [RequireComponent (typeof(AudioSource))]
  21. public class MusicManager : MonoBehaviour {
  22.     public static MusicManager Instance;            // Singleton
  23.     [SerializeField]
  24.     private float volume;
  25.     public float Volume {
  26.         get {
  27.             return source.volume;
  28.         }
  29.         set {
  30.             volume = value;
  31.             source.volume = value;
  32.         }
  33.     }
  34.  
  35.     public MusicPlaylist Playlist;
  36.     public bool Shuffle;
  37.  
  38.     public RepeatMode Repeat;
  39.     public float FadeDuration;
  40.     public bool PlayOnAwake;
  41.  
  42.     private AudioSource source;
  43.  
  44.  
  45.     void Start () {
  46.         // If there is no instance of this class, set it.
  47.         if (Instance == null) {
  48.             DontDestroyOnLoad (gameObject); // Don't destroy this object
  49.             Instance = this;
  50.         } else {
  51.             Debug.LogError ("There is already a Music Manager in the scene.");
  52.             GameObject.Destroy (this);
  53.         }
  54.  
  55.         // create a game object and add an AudioSource to it, to play music on
  56.         source = gameObject.GetComponent<AudioSource>();
  57.         source.name = "MusicAudioSource";
  58.         source.playOnAwake = false;
  59.         if (FadeDuration > 0)
  60.             source.volume = 0f;
  61.         else
  62.             Volume = volume;
  63.         if (Playlist == null)
  64.             return;
  65.         if (Playlist.MusicList.Length > 0)
  66.             source.clip = Playlist.MusicList [0];
  67.         else
  68.         {
  69.             Debug.LogError ("There are no music in the list");
  70.         }
  71.  
  72.  
  73.         if (PlayOnAwake)
  74.             Play ();
  75.     }
  76.  
  77.     public void Play()
  78.     {
  79.         StartCoroutine (PlayMusicList ());
  80.     }
  81.  
  82.     public void Stop(bool fade)
  83.     {
  84.         StopAllCoroutines ();
  85.         if (fade)
  86.             StartCoroutine(StopWithFade());
  87.         else
  88.             source.Stop();
  89.     }
  90.  
  91.     public void Next()
  92.     {
  93.         source.Stop ();
  94.     }
  95.  
  96.     public void ChangePlaylist(MusicPlaylist list)
  97.     {
  98.         Playlist = list;
  99.         _counter = 0;
  100.         StopAllCoroutines();
  101.         StartCoroutine(ChangePlaylistE());
  102.     }
  103.  
  104.     private IEnumerator ChangePlaylistE()
  105.     {
  106.         if (source.isPlaying)
  107.             yield return StartCoroutine(StopWithFade());
  108.         StartCoroutine(PlayMusicList());
  109.     }
  110.  
  111.     private IEnumerator StopWithFade()
  112.     {
  113.         if (FadeDuration > 0)
  114.         {
  115.             float lerpValue = 0f;
  116.             while (lerpValue < 1f) {
  117.                 lerpValue += Time.deltaTime / FadeDuration;
  118.                 source.volume = Mathf.Lerp (volume, 0f, lerpValue);
  119.                 yield return null;
  120.             }
  121.         }
  122.         source.Stop();
  123.     }
  124.  
  125.     public void PlaySong(AudioClip song)
  126.     {
  127.         StartCoroutine (PlaySongE (song));
  128.     }
  129.  
  130.     private IEnumerator PlaySongE(AudioClip clip)
  131.     {
  132.         source.Stop ();
  133.         source.clip = clip;
  134.         StartCoroutine (FadeIn ());
  135.         source.Play ();
  136.         while (source.isPlaying) {
  137.             if (source.clip.length - source.time <= FadeDuration) {
  138.                 yield return StartCoroutine (FadeOut ());
  139.             }
  140.             yield return null;
  141.         }
  142.     }
  143.  
  144.     private int _counter = 0;
  145.     private IEnumerator PlayMusicList()
  146.     {
  147.         while (true)
  148.         {
  149.             yield return StartCoroutine (PlaySongE (Playlist.MusicList [_counter]));
  150.             if (RepeatMode.Track == Repeat)
  151.             {
  152.             }
  153.             else if (Shuffle) {
  154.                 int newTrack = GetNewTrack ();
  155.                 while (newTrack == _counter)
  156.                 {
  157.                     newTrack = GetNewTrack ();
  158.                 }
  159.                 _counter = newTrack;
  160.  
  161.             }
  162.             else
  163.             {
  164.                 _counter++;
  165.                 if (_counter >= Playlist.MusicList.Length-1)
  166.                 {
  167.                     if (Repeat == RepeatMode.Playlist)
  168.                         _counter = 0;
  169.                     else
  170.                         yield break;
  171.                 }
  172.             }
  173.         }
  174.     }
  175.  
  176.     private IEnumerator FadeOut()
  177.     {
  178.         if (FadeDuration > 0f) {
  179.             float startTime = source.clip.length - FadeDuration;
  180.             float lerpValue = 0f;
  181.             while (lerpValue < 1f) {
  182.                 lerpValue = Mathf.InverseLerp (startTime, source.clip.length, source.time);
  183.                 source.volume = Mathf.Lerp (volume, 0f, lerpValue);
  184.                 yield return null;
  185.             }
  186.         } else {
  187.             yield break;
  188.         }
  189.     }
  190.  
  191.     private IEnumerator FadeIn()
  192.     {
  193.         if (FadeDuration > 0f)
  194.         {
  195.             float lerpValue = 0f;
  196.             while (lerpValue < 1f) {
  197.                 lerpValue = Mathf.InverseLerp (0f, FadeDuration, source.time);
  198.                 source.volume = Mathf.Lerp (0f, volume, lerpValue);
  199.                 yield return null;
  200.             }
  201.         }
  202.         else
  203.         {
  204.             yield break;
  205.         }
  206.     }
  207.  
  208.     private int GetNewTrack()
  209.     {
  210.         return Random.Range (0, Playlist.MusicList.Length);
  211.     }
  212. }
  213. public enum RepeatMode
  214. {
  215.     Playlist,
  216.     Track,
  217.     None
  218. }
RAW Paste Data