using System.Collections; using System.Collections.Generic; using UnityEngine; public class MusicLoopController : MonoBehaviour { [SerializeField] private GameObject FirstLoopTrack; [SerializeField] private GameObject OtherLoopsTrack; private AudioSource FirstLoopAudio; private AudioSource OtherLoopsAudio; private void Awake() { FirstLoopAudio = FirstLoopTrack.GetComponent(); OtherLoopsAudio = OtherLoopsTrack.GetComponent(); } private void OnEnable() { FirstLoopAudio.Play(); OtherLoopsAudio.PlayDelayed(FirstLoopAudio.clip.length); } public void ResumePlaying() { OtherLoopsAudio.Play(); } public void FadeOut(float fadeTime) { //TODO Fade out the sound then stop both clips StartCoroutine(DecreaseVolume(fadeTime)); } private IEnumerator DecreaseVolume(float fadeTime) { float startVolume = FirstLoopAudio.volume; while(FirstLoopAudio.volume > 0) { FirstLoopAudio.volume -= startVolume * Time.deltaTime / fadeTime; OtherLoopsAudio.volume -= startVolume * Time.deltaTime / fadeTime; yield return null; } FirstLoopAudio.Stop(); OtherLoopsAudio.Stop(); FirstLoopAudio.volume = startVolume; OtherLoopsAudio.volume = startVolume; } }