public void Initialize(bool startWithPrologue) { if (startWithPrologue) { isExitingPrologue = false; prologueMusic.StartTrack(); } else { isExitingPrologue = true; mainMenuMusic.StartTrack(); } }
// timed sequence of events to make the transition smooth IEnumerator MusicTransition(float delay) { yield return(new WaitForSeconds(delay)); // wait for the beat nextMusic.StartTrack(); float waitTime = SECONDS_PER_BAR * 0.5f; // wait for a full bar yield return(new WaitForSeconds(waitTime)); float fadeTime = SECONDS_PER_BAR * 0.5f; // fade for a bit longer // fade out the prologue float p = 0f; float startVolume = curMusic.GetVolume(); while (p < 1f) { // we only need to fade most of the way out curMusic.SetVolume(startVolume * (1 - p)); p += Time.deltaTime / fadeTime; yield return(null); } curMusic.StopTrack(); // wait for main menu to start before stopping prologue curMusic.SetVolume(startVolume); // revert the track to its original volume, just in case Destroy(curMusic.gameObject); // not needed anymore curMusic = nextMusic; nextMusic = null; isInTransition = false; transition = null; }
void Start() { isInTransition = false; if (curMusic != null && !curMusic.isPlaying) { curMusic.StartTrack(); } }
/// <summary> /// Starts a transition (instantaneous if withFading = true) to the next track with aligned tempo and bars. /// </summary> /// <param name="withFading">Whether the transition involves fading.</param> /// <param name="track">The track to play. Setting track will force a transition to start.</param> /// <returns>returns -1 if no transition happened, 0 if it was instant, or the delay before the transition will start.</returns> public float StartTransition(bool withFading, CustomMusicLoopController track = null) { if (track != null) { //Attempt to forcibly set the next track if (!SetNextTrack(track, true) || isInTransition || track != nextMusic) { // failed to set a new track, abort return(-1); } } if (nextMusic == null) { return(-1); } else if (curMusic == null) { curMusic = nextMusic; nextMusic = null; curMusic.StartTrack(); return(0); } else if (nextMusic == curMusic) { nextMusic = null; return(-1); } if (!withFading) { if (nextMusic != null) { if (isInTransition) { StopTransition(); } curMusic.StopTrack(); //wait for main menu to start before stopping prologue curMusic.SetVolume(volume); Destroy(curMusic.gameObject); // not needed anymore curMusic = nextMusic; nextMusic = null; curMusic.StartTrack(); return(0); } return(-1); } if (isInTransition) { return(-1); } isInTransition = true; float curPlayheadTime = curMusic.GetCurrentTime(); int nextBar = Mathf.CeilToInt(curPlayheadTime / SECONDS_PER_BAR); float nextBarTime = nextBar * SECONDS_PER_BAR; transition = StartCoroutine(MusicTransition(nextBarTime - curPlayheadTime)); return(nextBarTime - curPlayheadTime); }