MusicManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. public class MusicManager : MonoBehaviour
  5. {
  6. #region Unity Singleton
  7. private static MusicManager _instance;
  8. public static MusicManager Instance
  9. {
  10. get
  11. {
  12. if (_instance == null)
  13. {
  14. _instance = FindObjectOfType(typeof(MusicManager)) as MusicManager;
  15. if (_instance == null)
  16. {
  17. 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));
  18. }
  19. }
  20. return _instance;
  21. }
  22. }
  23. void Awake()
  24. {
  25. if (_instance != null && _instance != this)
  26. {
  27. AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name));
  28. Destroy(gameObject);
  29. return;
  30. }
  31. _instance = this;
  32. DontDestroyOnLoad(gameObject);
  33. }
  34. #endregion
  35. public AudioClip gameMusic;
  36. public AudioClip menuMusic;
  37. int track;
  38. AudioSource[] sources;
  39. int currentSource;
  40. int nextSource;
  41. void Start()
  42. {
  43. sources = (AudioSource[])GetComponents<AudioSource>();
  44. AVDebug.Assert(sources.Length >= 2, "Make sure you have at least 2 Audio Sources set up on the game object hosting the MusicManager");
  45. sources[0].clip = menuMusic;
  46. sources[0].Play();
  47. sources[currentSource].volume = SoundManager.Instance.MusicVolume;
  48. nextSource = 1;
  49. NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
  50. NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  51. NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
  52. NotificationCenter.AddListener(OnEndTimerStart, NotificationType.CelebrityDisappeared);
  53. }
  54. void OnDestroy()
  55. {
  56. NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
  57. NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  58. NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver);
  59. NotificationCenter.RemoveListener(OnEndTimerStart, NotificationType.CelebrityDisappeared);
  60. }
  61. private void OnEndTimerStart(Notification note)
  62. {
  63. //sources[currentSource].Stop();
  64. }
  65. void FadeTo(AudioClip clip, float fadeTime)
  66. {
  67. AudioSource source = sources[nextSource];
  68. source.clip = clip;
  69. bool shouldLoop = (clip == menuMusic);
  70. source.loop = shouldLoop;
  71. source.Play();
  72. source.volume = 0;
  73. currentSource = nextSource;
  74. nextSource = (++nextSource) % 2;
  75. source.time = 0; //sources[nextSource].time;
  76. iTween.Stop(gameObject);
  77. iTween.ValueTo(gameObject, iTween.Hash(
  78. "time", fadeTime,
  79. "from", 0,
  80. "to", 1,
  81. "onupdate", "OnFadeUpdate",
  82. "onupdatetarget", gameObject));
  83. }
  84. void OnFadeUpdate(float newVolume)
  85. {
  86. int prevSource = nextSource; //since there are only 2, it must be
  87. if (sources[prevSource].volume != 0) //if it hasn't already been muted
  88. {
  89. sources[prevSource].volume = (1 - newVolume) * SoundManager.Instance.MusicVolume; //inverse
  90. }
  91. sources[currentSource].volume = newVolume * SoundManager.Instance.MusicVolume;
  92. if (newVolume == 1)
  93. {
  94. sources[prevSource].Stop();
  95. }
  96. }
  97. void FadeOutCurrentSource(float fadeTime)
  98. {
  99. iTween.Stop(gameObject);
  100. iTween.ValueTo(gameObject, iTween.Hash(
  101. "time", fadeTime,
  102. "from", 1,
  103. "to", 0,
  104. "onupdate", "OnFadeOutCurrentSourceUpdate",
  105. "onupdatetarget", gameObject));
  106. }
  107. void OnFadeOutCurrentSourceUpdate(float newVolume)
  108. {
  109. sources[currentSource].volume = newVolume * SoundManager.Instance.MusicVolume;
  110. }
  111. public void SetMusicVolume(float value)
  112. {
  113. sources[currentSource].volume = value;
  114. if (MenuManager._instance.gameOverSummaryScreen.gameObject.activeSelf)
  115. {
  116. Debug.Log("Play");
  117. sources[currentSource].Play();
  118. }
  119. else if (!MenuManager._instance.ad.isActiveAndEnabled)
  120. {
  121. sources[currentSource].Play();
  122. }
  123. }
  124. void OnGameStart(Notification note)
  125. {
  126. FadeOutCurrentSource(0.5f);
  127. }
  128. void OnStartSpawningEnemies(Notification note)
  129. {
  130. FadeTo(gameMusic, 0);
  131. }
  132. void OnGameOver(Notification note)
  133. {
  134. FadeTo(menuMusic, 1);
  135. }
  136. }