/// <summary> /// Transitions from one music configuration to another, using the specified transition. /// /// MusicStack elements that share the same <see cref="IMusicAsset"/> can reuse the existing audio player /// when transitioning between them. This lets the music keep going. /// </summary> private void TransitionToMusic(IMusicStackElement oldElement, IMusicStackElement newElement, Transition transition) { // We get the music data associated with each stack element. var oldMusicData = GetAudioData(oldElement); var newMusicData = GetAudioData(newElement); if (oldMusicData != null) { playerControllers[oldMusicData.GetMusicID()].StopMusic(transition.fadeOutTime); } if (newMusicData != null) { var id = newMusicData.GetMusicID(); if (playerControllers.ContainsKey(id)) { // Reuse an existing player if one exists. playerControllers[id].StartMusic(transition.fadeInTime, transition.fadeInDelay, newElement.GetDesiredVolume()); } else { // Create a new player var player = newMusicData.CreatePlayer(); player.outputAudioMixerGroup = mixerGroup; var controller = new MusicPlayerController(player); playerControllers.Add(id, controller); controller.StartMusic(transition.fadeInTime, transition.fadeInDelay, newElement.GetDesiredVolume()); } } }
/// <summary> /// Adds an elment to the music stack. If this becomes the top element, it will transition to these music settings. /// </summary> public PrioritySortingKey AddToMusicStack(IMusicStackElement addedMusic) { var prevMusic = currentMusic; var newKey = new PrioritySortingKey((int)addedMusic.GetPriority()); musicStack.Add(newKey, addedMusic); if (currentMusic != prevMusic) { Asserts.AssertTrue(addedMusic == currentMusic, "Somehow we added to the music stack and yet a different music rose to the top?"); TransitionToMusic(prevMusic, currentMusic, currentMusic.GetTakeControlTransition()); } return(newKey); }
private static IMusicAsset GetAudioData(IMusicStackElement element) { return(element == null ? null : element.GetMusicAsset()); }