Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------
- // Plays background music &
- // manages loop points + fading
- //----------------------------------------
- using UnityEngine;
- namespace Dark_Earth
- {
- public class BGM : Singleton<BGM>
- {
- protected BGM () {}
- private const string FILE_EXTENSION = ".ogg";
- private AudioSource m_pMusic;
- private string m_sBGM_File = "";
- private float m_fLoop_From, m_fLoop_To, m_fFade_Time, m_fFade_Volume;
- private float m_fVolume = 1.00f;
- bool m_bFading;
- //----------------------------------------
- // Allocate Memory
- //----------------------------------------
- void Awake ()
- {
- m_pMusic = gameObject.AddComponent ("AudioSource") as AudioSource;
- }
- //----------------------------------------
- // Loop points + fading
- //----------------------------------------
- void Update ()
- {
- if (m_pMusic == null)
- return;
- if (m_bFading == true)
- {
- m_fFade_Volume -= ((1 / m_fFade_Time) * Time.deltaTime);
- if (m_fFade_Volume <= 0)
- Stop ();
- else
- m_pMusic.volume = (m_fVolume * m_fFade_Volume);
- }
- else
- m_pMusic.volume = m_fVolume;
- if (m_pMusic.loop == true)
- {
- if (m_pMusic.time >= m_fLoop_To)
- m_pMusic.time = m_fLoop_From;
- }
- }
- //----------------------------------------
- // Play BGM
- //----------------------------------------
- public void Play (string sFile, bool bLoop, float fLoop_From, float fLoop_To)
- {
- string sPath = (Application.dataPath + "/Resources/BGM/" + sFile + FILE_EXTENSION);
- if (System.IO.File.Exists (sPath) == false)
- return;
- if (sFile == m_sBGM_File)
- return;
- m_sBGM_File = sFile;
- m_bFading = false;
- if (m_pMusic.clip != null)
- Resources.UnloadAsset (m_pMusic.clip);
- m_pMusic.clip = Resources.Load ("BGM/"+sFile) as AudioClip;
- m_pMusic.loop = bLoop;
- if (fLoop_To == 0.00f)
- fLoop_To = m_pMusic.clip.length;
- m_fLoop_From = fLoop_From;
- m_fLoop_To = fLoop_To;
- m_pMusic.Play ();
- }
- public void Play (string sFile, bool bLoop)
- {
- Play (sFile, bLoop, 0.00f, 0.00f);
- }
- public void Play (string sFile)
- {
- Play (sFile, true);
- }
- //----------------------------------------
- // Fading
- //----------------------------------------
- public void Fade_Out (float fTime)
- {
- if (m_sBGM_File == "")
- return;
- m_bFading = true;
- m_fFade_Time = fTime;
- m_fFade_Volume = 1.00f;
- }
- //----------------------------------------
- // BGM Stop
- //----------------------------------------
- public void Stop ()
- {
- if (m_sBGM_File == "")
- return;
- m_pMusic.Stop ();
- m_sBGM_File = "";
- m_bFading = false;
- }
- //----------------------------------------
- // BGM Pause
- //----------------------------------------
- public void Pause ()
- {
- if (m_sBGM_File == "" || m_bFading == true)
- return;
- m_pMusic.Pause ();
- }
- public int Volume
- {
- set { m_fVolume = ((float)value / 100); }
- get { return ((int)m_fVolume * 100); }
- }
- }
- }
- //@Copyright by Rhutos, Rising Games
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement