Unity-Audio-Manager icon indicating copy to clipboard operation
Unity-Audio-Manager copied to clipboard

Transition 2 audio clip

Open Avatarchik opened this issue 10 months ago • 6 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

Avatarchik avatar Mar 27 '24 20:03 Avatarchik

Could you describe your feature request in more detail? It is currently unclear what exactly is requested.

Perhaps the example scene can provide some help. AdvancedExamples.cs

MathewHDYT avatar Mar 27 '24 20:03 MathewHDYT

TransitionTo Fade in and fade out transitions

Avatarchik avatar Mar 27 '24 20:03 Avatarchik

for example Play("Main_Menu"); then I call Play("Lobby"); and MainMenu smoothly reduces the volume and the Lobby will gradually increase the volume from 0

Avatarchik avatar Mar 27 '24 20:03 Avatarchik

This is how you would add a fade in effect on the volume, as can be seen in the AdvancedExamples.cs to any song.

am.SubscribeProgressCoroutine(fadeInSong.soundName, 0f, SongProgressCallback);
am.Play(fadeInSong.soundName);

public ProgressResponse InitFadeInSong(string name, float progress, ChildType child) {
    am.TryGetSource(name, out var source);
    source.Volume = 0f;
    am.LerpVolume(name, fadeInEndVolume, clipFadeInTime);
    return ProgressResponse.RESUB_IN_LOOP;
}

And this is how you would a fade out effect on the volume to any song.

am.TryGetSource(fadeOutSong.soundName, out var source);
float progress = ((source.Source.clip.length - clipFadeOutTime) / source.Source.clip.length);
am.SubscribeProgressCoroutine(fadeOutSong.soundName, progress, HandleFadeOutSong);
am.Play(fadeOutSong.soundName);

private ProgressResponse HandleFadeOutSong(string name, float progress, ChildType child) {
    am.LerpVolume(name, 0f, progress);
    return ProgressResponse.UNSUB;
}

For your case the fadeInSong.soundName, would be "Lobby" and fadeOutSong.soundName would be "Main_Menu".

MathewHDYT avatar Mar 27 '24 20:03 MathewHDYT

Can you provide a more detailed example, something I can’t figure out how to do? I want to write a function Transition (string fadeInSong, string fadeOutSong)

Avatarchik avatar Mar 27 '24 21:03 Avatarchik

When exactly would you call this method in your code? Simply once at the startup.

Or do you call it by yourself, once you want to transition from the main_menu to the lobby song?


For now this would be an Example method to fade out an already playing "Main_Menu" theme in 1 second and in that one second also starting fading in the "Lobby" theme.

private void TransitionWithFading(string fadeInSong, string fadeOutSong, float clipFadeInTime = 1f) {
    // Slowly decrease volume of the already playing song and slowly start to decrease the volume
    am.LerpVolume(fadeOutSong, 0f, clipFadeInTime);
    // Set volume to 0 before playing the song and slowly start to increase the volume
    am.TryGetSource(fadeInSong, out var source);
    float originalVolume = source.Volume;
    source.Volume = 0f;
    am.Play(fadeInSong);
    am.LerpVolume(fadeInSong, originalVolume, clipFadeInTime);
}

MathewHDYT avatar Mar 27 '24 22:03 MathewHDYT