// timed sequence of events to make the transition smooth IEnumerator MenuMusicTransition(float delay) { yield return(new WaitForSeconds(delay)); // wait for the beat mainMenuMusic.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 = prologueMusic.GetVolume(); while (p < 1f) // we only need to fade most of the way out { prologueMusic.SetVolume(startVolume * (1 - p)); p += Time.deltaTime / fadeTime; yield return(null); } prologueMusic.StopTrack(); // wait for main menu to start before stopping prologue prologueMusic.SetVolume(startVolume); // revert the track to its original volume, just in case }
// 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; }