SoundtrackManager.cs 5.7 KB

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