123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using UnityEngine.SceneManagement;
- using KairoEngine.Core;
- namespace KairoEngine.SoundtrackSystem
- {
- [HideMonoScript]
- public class SoundtrackManager : MonoBehaviour
- {
- #region Singleton
- private static SoundtrackManager soundtrackManager;
- public static SoundtrackManager instance
- {
- get {
- if(!soundtrackManager)
- {
- soundtrackManager = FindObjectOfType (typeof(SoundtrackManager)) as SoundtrackManager;
- if(!soundtrackManager)
- {
- //Debug.LogError("There need to one active SountrackManager script on the scene.");
- return null;
- }
- }
- return soundtrackManager;
- }
- }
- #endregion
- public AudioSource audioSource1;
- public AudioSource audioSource2;
- public Soundtrack currentTrack;
- public Playlist playlist;
- public int currentTrackIndex;
- public bool playOnStart = true;
- public bool showDebug = false;
- private int currentAudioSourceIndex = 1;
- private bool isPlaying = false;
- private bool playlistEnded = false;
- void Start()
- {
- if(playOnStart)
- {
- if(playlist != null) StartPlaylist(playlist, currentTrackIndex);
- else if(currentTrack != null) PlayTrack(currentTrack);
- }
- }
- void FixedUpdate()
- {
- if(!isPlaying) return;
- AudioSource currentAudioSource = GetAudioSource(currentAudioSourceIndex);
- if (!currentAudioSource.isPlaying && !playlistEnded)
- {
- // Song endend in current audio source
-
- if(playlist == null) return;
- currentTrackIndex += 1;
- if(playlist.soundtracks.Count <= currentTrackIndex)
- {
- // Reached the end of the playlist
- playlistEnded = true;
- if(showDebug) Debug.Log($"Reached the end of the playlist \'{playlist.title}\'");
- }
- else
- {
- // Play next song
- PlayTrack(playlist.soundtracks[currentTrackIndex]);
- }
- }
- }
- public static void StartPlaylist(Playlist newPlaylist, int index = 0)
- {
- if(instance == null) return;
- if(newPlaylist == null)
- {
- Debug.LogWarning("Missing playlist... music wont play");
- return;
- }
- if(instance.showDebug) Debug.Log($"Starting playlist \'{newPlaylist.title}\'");
- instance.playlist = newPlaylist;
- instance.currentTrackIndex = index;
- instance.playlistEnded = false;
- PlayTrack(instance.playlist.soundtracks[instance.currentTrackIndex]);
- }
- public static void PlayTrack(string soundtrackName)
- {
- if(instance == null) return;
- for (int i = 0; i < instance.playlist.soundtracks.Count; i++)
- {
- if(instance.playlist.soundtracks[i].name == soundtrackName)
- {
- PlayTrack(instance.playlist.soundtracks[i]);
- return;
- }
- }
- Debug.LogWarning($"Could not find soundtrack \"{soundtrackName}\" in playlist", instance.gameObject);
- }
- public static void PlayTrack(Soundtrack soundtrack)
- {
- if(instance == null) return;
- if(instance.showDebug) Debug.Log($"{(soundtrack.fadeIn ? "Fading in" : "Playing")} song \'{soundtrack.name}\'");
- instance.isPlaying = true;
- instance.currentTrack = soundtrack;
- instance.SwitchAudioSource();
- instance.Play(soundtrack, instance.currentAudioSourceIndex);
- if(soundtrack.fadeIn) instance.StartCoroutine(SoundtrackManager.StartFade(instance.currentAudioSourceIndex, soundtrack.fadeTime, 1f, false));
- else
- {
- if(instance.currentAudioSourceIndex == 1) instance.audioSource1.volume = 1f;
- else if(instance.currentAudioSourceIndex == 2) instance.audioSource2.volume = 1f;
- }
- }
- public static void FadeOutSoundtrack(float time = 1f)
- {
- if(instance == null) return;
- if(instance.showDebug) Debug.Log($"Fading out song \'{instance.playlist.soundtracks[instance.currentTrackIndex].name}\'");
- instance.StartCoroutine(SoundtrackManager.StartFade(instance.currentAudioSourceIndex, time, 0f, true));
- }
- public static IEnumerator StartFade(int audioSourceIndex, float duration, float targetVolume, bool stop = false)
- {
- if(instance == null) yield break;
-
- AudioSource audioSource = instance.GetAudioSource(audioSourceIndex);
- if(stop == false) audioSource.Play();
- float currentTime = 0;
- float start = audioSource.volume;
- while (currentTime < duration)
- {
- currentTime += Time.deltaTime;
- audioSource.volume = Mathf.Lerp(start, targetVolume, currentTime / duration);
- yield return null;
- }
- if(stop == true)
- {
- audioSource.Stop();
- audioSource.clip = null;
- }
- if(instance.showDebug) Debug.Log($"Song \'{instance.currentTrack.name}\' ended");
- yield break;
- }
- private void Play(Soundtrack soundtrack, int index)
- {
- if(index == 1)
- {
- audioSource1.clip = soundtrack.audioClip;
- audioSource1.Play();
- }
- else if(index == 2)
- {
- audioSource2.clip = soundtrack.audioClip;
- audioSource2.Play();
- }
- }
- private AudioSource GetAudioSource(int index)
- {
- if(index == 1) return audioSource1;
- else return audioSource2;
- }
- private void SwitchAudioSource()
- {
- if(currentAudioSourceIndex == 1) currentAudioSourceIndex = 2;
- else if(currentAudioSourceIndex == 2) currentAudioSourceIndex = 1;
- }
- }
- }
|