SimpleBackgroundMusic

SimpleBackgroundMusic#

NAMESPACE:

UnityUtils.ScriptUtils.Audio

The SimpleBackgroundMusic 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.

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.
        SimpleBackgroundMusic.Instance.StartContinuousMusic();

        // Stop playing music.
        SimpleBackgroundMusic.Instance.StopContinuousMusic();
     }
}

Tip

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

Functions#

UnityUtils.ScriptUtils.Audio.SimpleBackgroundMusic : MonoBehaviour

Public Functions

void StopContinousMusic()#

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

void PlayContinuousMusic()#

Starts to PlayMusicContinuously until stopped.

void PlaySingleRandomMusicTrack()#

Plays a random song in musicTracks once.

void PlaySpecificMusicTrack(AudioClip clip)#

Plays a specific Music track once.

AudioClip GetRandomSong()#

return:

Random Music track within musicTracks

Public Members

AudioClip[] musicTracks

An array of audio clips representing the available Music tracks.

AudioClip 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 logOnSongPlay#

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

bool logOnSongStop#

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

bool logRandomSongCooldown#

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

bool logFadeVolume#

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

bool logSongProgress#

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

float logSongProgessEveryPercent = 1#

Will only log song progress every logSongProgessEveryPercent PERCENT.