You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
5.2 KiB
196 lines
5.2 KiB
using UnityEngine; |
|
using System.Collections.Generic; |
|
|
|
|
|
public class MusicManager : MonoBehaviour |
|
{ |
|
private static MusicManager m_instance = null; |
|
private string m_playedMusicFileName = ""; |
|
List<string> m_allMusicFileNames = new List<string>(); |
|
List<string> m_usedMusicFileNames = new List<string>(); |
|
float m_audioClipTime = 0f; |
|
|
|
AudioSource m_audioSourse = null; |
|
bool m_needStartNewTrack = true; |
|
|
|
bool m_inited = false; |
|
bool m_needFadeUpMusic = false; |
|
bool m_needFadeDownMusic = false; |
|
|
|
//------------------------------------------------------------------ |
|
public string PlayedMusicFileName |
|
{ |
|
get { return m_playedMusicFileName; } |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public static MusicManager Instance |
|
{ |
|
get { return m_instance; } |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
void Init () |
|
{ |
|
m_instance = this; |
|
|
|
m_audioSourse = GetComponent<AudioSource>(); |
|
|
|
#if UNITY_IOS |
|
GlobalsVar.gMusicVolume = 0.7f; |
|
#endif |
|
m_audioSourse.volume = GlobalsVar.gMusicVolume; |
|
|
|
InitPossibleMusicTracks(); |
|
m_inited = true; |
|
|
|
DontDestroyOnLoad(this); |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
void InitPossibleMusicTracks() |
|
{ |
|
for (int i = 1; i < 100; ++i) |
|
{ |
|
string musicPath = "Music/theme_" + i.ToString(); |
|
AudioClip clip = Resources.Load<AudioClip>(musicPath); |
|
if (!clip) |
|
break; |
|
|
|
m_allMusicFileNames.Add(musicPath); |
|
} |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
bool IsPlaying() |
|
{ |
|
if (m_audioClipTime <= 0f) |
|
return false; |
|
|
|
return true; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public void StartMusic() |
|
{ |
|
if (!GlobalsVar.gNeedPlayMusic) |
|
return; |
|
|
|
PlayMusic(); |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
void PlayMusic() |
|
{ |
|
if (m_audioSourse.isPlaying) |
|
return; |
|
|
|
m_playedMusicFileName = m_allMusicFileNames[0]; |
|
m_audioSourse.clip = Resources.Load<AudioClip>( m_playedMusicFileName ); |
|
m_audioClipTime = m_audioSourse.clip.length; |
|
|
|
m_audioSourse.volume = 0f; |
|
m_audioSourse.Play(); |
|
FadeUpMusicVolume(); |
|
|
|
m_needStartNewTrack = true; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public AudioSource PlayMusicByName(string musicName) |
|
{ |
|
if (musicName == "") |
|
return null; |
|
|
|
m_audioSourse.clip = Resources.Load<AudioClip>(musicName); |
|
m_audioClipTime = m_audioSourse.clip.length; |
|
|
|
m_audioSourse.volume = 0f; |
|
m_audioSourse.Play(); |
|
FadeUpMusicVolume(); |
|
|
|
m_needStartNewTrack = true; |
|
return m_audioSourse; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public void FadeUpMusicVolume(float time = 5f) |
|
{ |
|
m_needFadeUpMusic = true; |
|
// float volume = m_audioSourse.volume; |
|
// LeanTween.value(gameObject, volume, 1f, time).setOnUpdate( |
|
// (float value) => |
|
// { |
|
// SetVolume(value); |
|
// }); |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public void FadeDownMusicVolume( System.Action onComplete = null, float time = 5f ) |
|
{ |
|
m_needFadeDownMusic = true; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public void SetVolume( float volumeValue ) |
|
{ |
|
if (m_audioSourse) |
|
m_audioSourse.volume = volumeValue; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public void StopMusic() |
|
{ |
|
m_needStartNewTrack = false; |
|
if (m_audioSourse) |
|
m_audioSourse.Stop(); |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public void Update () |
|
{ |
|
if (!m_inited) |
|
Init(); |
|
|
|
if (!GlobalsVar.gBoard) |
|
return; |
|
|
|
m_audioClipTime -= Time.deltaTime; |
|
m_audioClipTime = CommonFunctions.ClampDown(m_audioClipTime, 0f); |
|
|
|
if (m_audioClipTime <= 0.0f && m_needStartNewTrack) |
|
StartMusic(); |
|
|
|
UpdateMusicFade(); |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
void UpdateMusicFade() |
|
{ |
|
if (m_needFadeUpMusic) |
|
{ |
|
float volume = m_audioSourse.volume; |
|
volume += Time.unscaledDeltaTime; |
|
if (volume > GlobalsVar.gMusicVolume) |
|
{ |
|
volume = GlobalsVar.gMusicVolume; |
|
m_needFadeUpMusic = false; |
|
} |
|
|
|
SetVolume(volume); |
|
} |
|
else if (m_needFadeDownMusic) |
|
{ |
|
float volume = m_audioSourse.volume; |
|
volume -= Time.unscaledDeltaTime; |
|
if (volume < 0f) |
|
{ |
|
volume = 0f; |
|
m_needFadeDownMusic = false; |
|
StopMusic(); |
|
} |
|
|
|
SetVolume(volume); |
|
} |
|
} |
|
}
|
|
|