Music Manager

Overview

Music Manager allows you to easily play, pause and stop musics in your game with a single line of code.

All the functions in MusicManager are static. You do not have to create an object.

Because of this, you just have to import the package in Unity and you are ready to go.

Fades and volume changes are smoothed using a cubic function :

Files

Examples

Here are some examples:

// Will play "Dark Descent.mp3" in a Resources folder with a fade out/in of 5s
MusicManager.play("Dark Descent", 5.0f, 5.0f);

// Will play "The Dark Amulet.ogg" in 4s (3s fade out and 1s fade in)
MusicManager.play("The Dark Amulet", 3.0f, 1.0f);

// Pause and unpause the music
MusicManager.pause();
MusicManager.unpause();
               
// Smoothly stop the music over 3 seconds
MusicManager.stop(3.0f);
               
// Set the volume of the music to 25% immediatly
MusicManager.setVolume(0.25f);
// Smoothly set the volume of the music to 100% in 1 second
MusicManager.setVolume(1.0f, 1.0f);

// Advanced use: Access the audio component of the internal MusicManager object
GameObject player = MusicManager.getMusicEmitter();
int volume = player.audio.volume;

Functions

void MusicManager.play(AudioClip clip, float fadeOut = 0f, float fadeIn = 0f)

Will play the music after fading out the precedent music in fadeOut seconds and fading in the new music in fadeIn seconds.


void MusicManager.play(string file, float fadeOut = 0f, float fadeIn = 0f)

Convenient function which loads the music from a Resources folder by its filename.


void MusicManager.setLoop(bool t)

Set if the music should loop or not. Default is true.


void MusicManager.pause()

Pause the music.


void MusicManager.unpause()

Unpause the music.


void MusicManager.stop(float fadeOut = 0f)

Stop the music over fadeOut seconds.


void MusicManager.setVolume(float volume = 1.0f, float duration = 0f)

Set the music volume over duration seconds.

Advanced use

GameObject MusicManager.getMusicEmitter()

Return the GameObject containing the Audio Source of the music.

FAQ

« My music is not playing! »

Make sure that your music is not set to 3D sound:

Also, if you used the string version of the play() function, make sure you are correctly pointing to the music file. If your music is in Assets/Subfolder/Resources/Musics/supermusic.ogg, you should pass Musics/supermusic as the parameter. When the music is not found, a debug message is supposed to be shown in your console.