using System.Collections; using UnityEngine; namespace Management { /// /// Singleton pattern object that holds an audio source, designed as single source of /// truth and driver for the BGM music. /// public class BGM : MonoBehaviour { public enum TrackType { Core, Muted, Victory } private TrackType _currentlyPlaying = TrackType.Muted; // hold a static reference to enforce singleton pattern private static BGM _instance; // the audiosource that will play the music [SerializeField] private AudioSource[] audioSources; // Fades out the currently playing music if playing, then fades in // the provided clip over the amount of time specified. // if t == 0 swap is instant. public void FadeIn(TrackType type, float t = 0.5f) { StopAllCoroutines(); var currentAudioSource = _currentlyPlaying switch { TrackType.Muted => audioSources[0], TrackType.Core => audioSources[1], TrackType.Victory => audioSources[2], _ => audioSources[0], }; var targetAudioSource = type switch { TrackType.Muted => audioSources[0], TrackType.Core => audioSources[1], TrackType.Victory => audioSources[2], _ => audioSources[0], }; StartCoroutine(FadeInRoutine(currentAudioSource, targetAudioSource, t)); _currentlyPlaying = type; } private IEnumerator FadeInRoutine(AudioSource current, AudioSource target, float t) { var timer = 0f; while (timer < t) { timer += Time.deltaTime; current.volume = Mathf.Lerp(1, 0, timer / t); target.volume = Mathf.Lerp(0, 1, timer / t); yield return null; } current.volume = 0; target.volume = 1; } private void Awake() { // enforce singleton if (_instance != null && _instance != this) { Destroy(gameObject); return; } _instance = this; DontDestroyOnLoad(gameObject); } } }