MusicManager

MusicManager#

NAMESPACE:

UnityUtils.ScriptUtils.Audio

The MusicManager is used for quickly adding music to your games. It allows you to cycle through random songs with some adjustable properties for fading songs and have random delay between songs.

Also has some Actions that can be called! Is very useful for having a system that displays something when a new music track plays.

Warning

Turn on “Run in Background” in the Unity settings, if you don’t the script may bug out when tabbing in/out of the game.

Example Usage#

using UnityEngine;
using UnityUtils.ScriptUtils.Audio;

public class ExampleScript : MonoBehaviour
{
  void Start()
  {
        // Start playing music.
        MusicManager.Instance.playMusic = true;

        // Stop playing music.
        MusicManager.Instance.playMusic = false;
     }

     void OnEnable()
     {
        MusicManager.Instance.OnPlayMusicTrack += SomeFunction;
        MusicManager.Instance.OnStopMusicTrack += SomeOtherFunction;
     }

     void OnEnable()
     {
        MusicManager.Instance.OnPlayMusicTrack -= SomeFunction;
        MusicManager.Instance.OnStopMusicTrack -= SomeOtherFunction;
     }
}

Tip

Use .wav format audio for better audio quality and looping!

Functions#

UnityUtils.ScriptUtils.Audio.MusicManager : MonoBehaviour

Public Functions

void PlayContinuousMusic()#

Starts to PlayMusicContinuously until stopped.

void StopContinousMusic()#

Stops the musicSource from playing Music and looping until PlayMusicContinuously is called again (to start looping).

void PlayRandomMusicTrack()#

Plays a random song in musicTracks once.

void PlaySpecificMusicTrack(MusicClip clip)#

Plays a specific Music track once.

MusicClip GetRandomSong()#

return:

Random Music track within musicTracks

bool IsPlayingMusic()#

Returns whether music is currently playing or not.

Public Members

MusicClip[] musicTracks

An array of audio clips representing the available Music tracks.

MusicClip currentPlayingTrack#

The current playing track.

bool playOnAwake = true#

Whether to start playing Music as soon as this object awakes.

float fadeTime#

The duration, in seconds, over which the fade in/out effect occurs.

Vector2 randomSecondCooldownBetweenSongs#

Random cooldown time between songs in milliseconds.

bool logOnPlayMusicTrack#

If true, will output a Debug.Log(object) when a new track starts playing.

bool logOnStopMusicTrack#

If true, will output a Debug.Log(object) when a track stops playing.

bool logRandomSongCooldown#

If true, will output a Debug.Log(object) the randomSecondCooldownBetweenSongs when calculated.

bool logFadeVolume#

If true, will output a Debug.Log(object) when the fade time is not 0 or 1 (When its animating).

bool logSongProgress#

If true, will output a Debug.Log(object) every logSongProgessEveryPercent, detailing how much of the current song has been played.

float logSongProgessEveryPercent = 1#

Will only log song progress every logSongProgessEveryPercent PERCENT.

Coroutine playingMusicCoroutine#

The Coroutine playing music. Is able to be canceled.

Action<MusicClip> OnPlayMusicTrack#

Called when a music track starts playing, with the track that started playing as a parameter.

Action<MusicClip> OnStopMusicTrack#

Called when a music track stops playing, with the track that stopped playing as a parameter.