MathewHDYT / Unity-Audio-Manager

🔊 Used to play/change/stop/mute/... one or multiple sounds at a certain circumstance or event in 2D and 3D simply via. code.
https://mathewhdyt.github.io/Unity-Audio-Manager/
MIT License
67 stars 4 forks source link

Transition 2 audio clip #5

Closed Avatarchik closed 4 months ago

Avatarchik commented 6 months ago

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.

MathewHDYT commented 6 months ago

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

Avatarchik commented 6 months ago

TransitionTo Fade in and fade out transitions

Avatarchik commented 6 months ago

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

MathewHDYT commented 6 months ago

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".

Avatarchik commented 6 months ago

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)

MathewHDYT commented 6 months ago

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);
}