SoundtrackManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using UnityEngine.SceneManagement;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.SoundtrackSystem
  8. {
  9. [HideMonoScript]
  10. public class SoundtrackManager : MonoBehaviour
  11. {
  12. #region Singleton
  13. private static SoundtrackManager soundtrackManager;
  14. public static SoundtrackManager instance
  15. {
  16. get {
  17. if(!soundtrackManager)
  18. {
  19. soundtrackManager = FindObjectOfType (typeof(SoundtrackManager)) as SoundtrackManager;
  20. if(!soundtrackManager)
  21. {
  22. //Debug.LogError("There need to one active SountrackManager script on the scene.");
  23. return null;
  24. }
  25. }
  26. return soundtrackManager;
  27. }
  28. }
  29. #endregion
  30. public AudioSource audioSource1;
  31. public AudioSource audioSource2;
  32. public Soundtrack currentTrack;
  33. public Playlist playlist;
  34. public int currentTrackIndex;
  35. public bool playOnStart = true;
  36. public bool showDebug = false;
  37. private int currentAudioSourceIndex = 1;
  38. private bool isPlaying = false;
  39. private bool playlistEnded = false;
  40. void Start()
  41. {
  42. if(playOnStart)
  43. {
  44. if(playlist != null) StartPlaylist(playlist, currentTrackIndex);
  45. else if(currentTrack != null) PlayTrack(currentTrack);
  46. }
  47. }
  48. void FixedUpdate()
  49. {
  50. if(!isPlaying) return;
  51. AudioSource currentAudioSource = GetAudioSource(currentAudioSourceIndex);
  52. if (!currentAudioSource.isPlaying && !playlistEnded)
  53. {
  54. // Song endend in current audio source
  55. if(playlist == null) return;
  56. currentTrackIndex += 1;
  57. if(playlist.soundtracks.Count <= currentTrackIndex)
  58. {
  59. // Reached the end of the playlist
  60. playlistEnded = true;
  61. if(showDebug) Debug.Log($"Reached the end of the playlist \'{playlist.title}\'");
  62. }
  63. else
  64. {
  65. // Play next song
  66. PlayTrack(playlist.soundtracks[currentTrackIndex]);
  67. }
  68. }
  69. }
  70. public static void StartPlaylist(Playlist newPlaylist, int index = 0)
  71. {
  72. if(instance == null) return;
  73. if(newPlaylist == null)
  74. {
  75. Debug.LogWarning("Missing playlist... music wont play");
  76. return;
  77. }
  78. if(instance.showDebug) Debug.Log($"Starting playlist \'{newPlaylist.title}\'");
  79. instance.playlist = newPlaylist;
  80. instance.currentTrackIndex = index;
  81. instance.playlistEnded = false;
  82. PlayTrack(instance.playlist.soundtracks[instance.currentTrackIndex]);
  83. }
  84. public static void PlayTrack(string soundtrackName)
  85. {
  86. if(instance == null) return;
  87. for (int i = 0; i < instance.playlist.soundtracks.Count; i++)
  88. {
  89. if(instance.playlist.soundtracks[i].name == soundtrackName)
  90. {
  91. PlayTrack(instance.playlist.soundtracks[i]);
  92. return;
  93. }
  94. }
  95. Debug.LogWarning($"Could not find soundtrack \"{soundtrackName}\" in playlist", instance.gameObject);
  96. }
  97. public static void PlayTrack(Soundtrack soundtrack)
  98. {
  99. if(instance == null) return;
  100. if(instance.showDebug) Debug.Log($"{(soundtrack.fadeIn ? "Fading in" : "Playing")} song \'{soundtrack.name}\'");
  101. instance.isPlaying = true;
  102. instance.currentTrack = soundtrack;
  103. instance.SwitchAudioSource();
  104. instance.Play(soundtrack, instance.currentAudioSourceIndex);
  105. if(soundtrack.fadeIn) instance.StartCoroutine(SoundtrackManager.StartFade(instance.currentAudioSourceIndex, soundtrack.fadeTime, 1f, false));
  106. else
  107. {
  108. if(instance.currentAudioSourceIndex == 1) instance.audioSource1.volume = 1f;
  109. else if(instance.currentAudioSourceIndex == 2) instance.audioSource2.volume = 1f;
  110. }
  111. }
  112. public static void FadeOutSoundtrack(float time = 1f)
  113. {
  114. if(instance == null) return;
  115. if(instance.showDebug) Debug.Log($"Fading out song \'{instance.playlist.soundtracks[instance.currentTrackIndex].name}\'");
  116. instance.StartCoroutine(SoundtrackManager.StartFade(instance.currentAudioSourceIndex, time, 0f, true));
  117. }
  118. public static IEnumerator StartFade(int audioSourceIndex, float duration, float targetVolume, bool stop = false)
  119. {
  120. if(instance == null) yield break;
  121. AudioSource audioSource = instance.GetAudioSource(audioSourceIndex);
  122. if(stop == false) audioSource.Play();
  123. float currentTime = 0;
  124. float start = audioSource.volume;
  125. while (currentTime < duration)
  126. {
  127. currentTime += Time.deltaTime;
  128. audioSource.volume = Mathf.Lerp(start, targetVolume, currentTime / duration);
  129. yield return null;
  130. }
  131. if(stop == true)
  132. {
  133. audioSource.Stop();
  134. audioSource.clip = null;
  135. }
  136. if(instance.showDebug) Debug.Log($"Song \'{instance.currentTrack.name}\' ended");
  137. yield break;
  138. }
  139. private void Play(Soundtrack soundtrack, int index)
  140. {
  141. if(index == 1)
  142. {
  143. audioSource1.clip = soundtrack.audioClip;
  144. audioSource1.Play();
  145. }
  146. else if(index == 2)
  147. {
  148. audioSource2.clip = soundtrack.audioClip;
  149. audioSource2.Play();
  150. }
  151. }
  152. private AudioSource GetAudioSource(int index)
  153. {
  154. if(index == 1) return audioSource1;
  155. else return audioSource2;
  156. }
  157. private void SwitchAudioSource()
  158. {
  159. if(currentAudioSourceIndex == 1) currentAudioSourceIndex = 2;
  160. else if(currentAudioSourceIndex == 2) currentAudioSourceIndex = 1;
  161. }
  162. }
  163. }