Guest User

Unity BGMController class

a guest
Dec 21st, 2015
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BGMController : MonoBehaviour {
  5.     public float Volume = 1;
  6.     public AudioClip Music;
  7.     public float WaitBeforePlaying = 0;
  8.     public string[] AudioLevels;
  9.     //Loop back to this point
  10.     public float LoopBackTo = 0;
  11.     public bool KeepAlive = true;
  12.     //Time from the END of the track to loop from (crossfade)
  13.     public float LoopFromEnd = 0;
  14.     public bool firstPlay;
  15.     public AudioSource CurrentlyPlaying, NextPlaying;
  16.     void Start()
  17.     {
  18.         if (WaitBeforePlaying > 0) Invoke("PlayBGM", WaitBeforePlaying);
  19.         else Invoke("PlayBGM", 0.01f);
  20.     }
  21.     private bool hasInited = false;
  22.     public void Restart()
  23.     {
  24.         if (!hasInited) return;
  25.         if (CurrentlyPlaying != null)
  26.         {
  27.             if (CurrentlyPlaying.gameObject != this.gameObject)
  28.             {
  29.                 Destroy(CurrentlyPlaying.gameObject);
  30.                 CurrentlyPlaying = null;
  31.                 NextPlaying = null;
  32.             }
  33.             else
  34.             {
  35.                 this.GetComponent<AudioSource>().Stop();
  36.             }
  37.         }
  38.         if (NextPlaying != null && NextPlaying != this)
  39.         {
  40.             Destroy(NextPlaying.gameObject);
  41.             NextPlaying = null;
  42.         }
  43.         PlayBGM();
  44.     }
  45.     void PlayBGM()
  46.     {
  47.         hasInited = true;
  48.         if (CurrentlyPlaying == null)
  49.         {
  50.             CurrentlyPlaying = this.GetComponent<AudioSource>();
  51.             CurrentlyPlaying.Stop();
  52.         }
  53.         if (NextPlaying == null)
  54.         {
  55.             GameObject p = new GameObject("AudioSource");
  56.             NextPlaying = p.AddComponent<AudioSource>();
  57.             NextPlaying.playOnAwake = false;
  58.             NextPlaying.time = LoopBackTo;
  59.             p.transform.parent = this.transform;
  60.             p.transform.localPosition = Vector3.zero;
  61.         }
  62.  
  63.         UpdateVolume();
  64.         CurrentlyPlaying.enabled = true;
  65.             CurrentlyPlaying.clip = Music;
  66.             CurrentlyPlaying.Play();
  67.             CurrentlyPlaying.loop = false;
  68.         NextPlaying.clip = Music;
  69.         NextPlaying.Stop();
  70.        
  71.         NextPlaying.loop = false;
  72.         firstPlay = true;
  73.     }
  74.     public void UpdateVolume()
  75.     {
  76.         float multiplier = Volume;
  77.         foreach (string pref in AudioLevels)
  78.         {
  79.             multiplier *= PlayerPrefs.GetFloat(pref, 0.5f) ;
  80.         }
  81.         if (CurrentlyPlaying != null)
  82.         {
  83.             CurrentlyPlaying.volume = multiplier;
  84.         }
  85.         if (NextPlaying != null)
  86.         {
  87.             NextPlaying.volume = multiplier;
  88.         }
  89.     }
  90.     void LateUpdate()
  91.     {
  92.         if (CurrentlyPlaying != null)
  93.         {
  94.         if (firstPlay && CurrentlyPlaying.time > LoopBackTo) firstPlay = false;
  95.             if (CurrentlyPlaying.time > Music.length - LoopFromEnd)
  96.             {
  97.                 NextPlaying.Play();
  98.                 CurrentlyPlaying.Stop();
  99.                 var p = CurrentlyPlaying;
  100.                 CurrentlyPlaying = NextPlaying;
  101.                 NextPlaying = p;
  102.                 CurrentlyPlaying.time = LoopBackTo;
  103.             }
  104.         }
  105.     }
  106.  
  107.     public void Play()
  108.     {
  109.         CurrentlyPlaying.Play();
  110.     }
  111.  
  112.     public void Stop()
  113.     {
  114.         CurrentlyPlaying.Stop();
  115.     }
  116. }
RAW Paste Data