// Play music using the music channels. // If the currently-playing music group is the same as the given music group, // this method will have no effect. public void PlayMusicGroup(SOAMusicChannelData musicGroup, float secondsToFadeOut = 0.0f, float secondsToWaitBeforeFadeIn = 0.0f, float secondsToFadeIn = 0.0f) { MusicGroup currentMusicGroup = GetCurrentMusicGroup(); int channelCount = musicGroup.GetLength(); // Don't move to a new music group if the current one is playing // the same music group as the one passed to this method. if (musicGroup == latestMusicGroupData) { // Instead, fade the channels to the corresponding volumes. for (int i = 0; i < channelCount; ++i) { AudioClip clip = musicGroup.GetClip(i); float targetVolume = musicGroup.GetVolume(i); int channelNumber = currentMusicGroup.GetChannelIndexFromClip(clip); SetMusicChannelVolume(channelNumber, targetVolume, secondsToFadeIn); } // That's all we need to do. return; } latestMusicGroupData = musicGroup; // Fade out the old music group. currentMusicGroup.Fade(0.0f, secondsToFadeOut); // Increment the current music group index. ++groupMusicCurrent; groupMusicCurrent %= groupsMusic.Length; // Get the new current music group and clear its data. currentMusicGroup = GetCurrentMusicGroup(); currentMusicGroup.Annihilate(); // Populate the music group's channels. for (int i = 0; i < channelCount; ++i) { AudioClip clip = musicGroup.GetClip(i); float targetVolume = musicGroup.GetVolume(i); AudioSource source = channelsMusic.GetFreeChannel(); source.volume = 0.0f; currentMusicGroup.AddChannel(source, clip); currentMusicGroup.FadeChannel(i, targetVolume, secondsToFadeIn, secondsToWaitBeforeFadeIn); } }
// Play one-shot audio using an SFX channel. public void PlaySFX(AudioClip clip) { AudioSource source = channelsSFX.GetFreeChannel(); source.PlayOneShot(clip); }