123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using UnityEngine;
- using System.Collections;
- using System;
- public class MusicManager : MonoBehaviour
- {
- #region Unity Singleton
- private static MusicManager _instance;
- public static MusicManager Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = FindObjectOfType(typeof(MusicManager)) as MusicManager;
- if (_instance == null)
- {
- AVDebug.LogError(string.Format("No gameObject with {0} component exists. Make sure to create a gameObject with {0} component", new System.Diagnostics.StackFrame().GetMethod().DeclaringType));
- }
- }
- return _instance;
- }
- }
- void Awake()
- {
- if (_instance != null && _instance != this)
- {
- AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name));
- Destroy(gameObject);
- return;
- }
- _instance = this;
- DontDestroyOnLoad(gameObject);
- }
- #endregion
- public AudioClip gameMusic;
- public AudioClip menuMusic;
- int track;
- AudioSource[] sources;
- int currentSource;
- int nextSource;
- void Start()
- {
- sources = (AudioSource[])GetComponents<AudioSource>();
- AVDebug.Assert(sources.Length >= 2, "Make sure you have at least 2 Audio Sources set up on the game object hosting the MusicManager");
- sources[0].clip = menuMusic;
- sources[0].Play();
- sources[currentSource].volume = SoundManager.Instance.MusicVolume;
- nextSource = 1;
- NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
- NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
- NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
- NotificationCenter.AddListener(OnEndTimerStart, NotificationType.CelebrityDisappeared);
- }
- void OnDestroy()
- {
- NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
- NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
- NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver);
- NotificationCenter.RemoveListener(OnEndTimerStart, NotificationType.CelebrityDisappeared);
- }
- private void OnEndTimerStart(Notification note)
- {
- //sources[currentSource].Stop();
- }
- void FadeTo(AudioClip clip, float fadeTime)
- {
- AudioSource source = sources[nextSource];
- source.clip = clip;
- bool shouldLoop = (clip == menuMusic);
- source.loop = shouldLoop;
- source.Play();
- source.volume = 0;
- currentSource = nextSource;
- nextSource = (++nextSource) % 2;
- source.time = 0; //sources[nextSource].time;
- iTween.Stop(gameObject);
- iTween.ValueTo(gameObject, iTween.Hash(
- "time", fadeTime,
- "from", 0,
- "to", 1,
- "onupdate", "OnFadeUpdate",
- "onupdatetarget", gameObject));
- }
- void OnFadeUpdate(float newVolume)
- {
- int prevSource = nextSource; //since there are only 2, it must be
- if (sources[prevSource].volume != 0) //if it hasn't already been muted
- {
- sources[prevSource].volume = (1 - newVolume) * SoundManager.Instance.MusicVolume; //inverse
- }
- sources[currentSource].volume = newVolume * SoundManager.Instance.MusicVolume;
- if (newVolume == 1)
- {
- sources[prevSource].Stop();
- }
- }
- void FadeOutCurrentSource(float fadeTime)
- {
- iTween.Stop(gameObject);
- iTween.ValueTo(gameObject, iTween.Hash(
- "time", fadeTime,
- "from", 1,
- "to", 0,
- "onupdate", "OnFadeOutCurrentSourceUpdate",
- "onupdatetarget", gameObject));
- }
- void OnFadeOutCurrentSourceUpdate(float newVolume)
- {
- sources[currentSource].volume = newVolume * SoundManager.Instance.MusicVolume;
- }
- public void SetMusicVolume(float value)
- {
- sources[currentSource].volume = value;
-
- if (MenuManager._instance.gameOverSummaryScreen.gameObject.activeSelf)
- {
- Debug.Log("Play");
- sources[currentSource].Play();
- }
- else if (!MenuManager._instance.ad.isActiveAndEnabled)
- {
- sources[currentSource].Play();
- }
- }
- void OnGameStart(Notification note)
- {
- FadeOutCurrentSource(0.5f);
- }
- void OnStartSpawningEnemies(Notification note)
- {
- FadeTo(gameMusic, 0);
- }
-
- void OnGameOver(Notification note)
- {
- FadeTo(menuMusic, 1);
- }
- }
|