Music

A Music object is an instance you can create from code to play one of the music tracks previously created in the Audio Creator window under the Music tab. It represents a complete music track—such as background themes, main themes, or any audio requiring continuous or atmospheric playback.


🛠 How to create a Music object

To use music from code, you must first create the track in the Audio Creator, and then invoke it using its tag or its Track identifier. For example:

new Music("MainTheme").Play();  

Or if you're using a Track directly:

new Music(Track.MainTheme).Play();

If you rename a music track in the Audio Collection window, its code reference (Track.MainTheme) will break.

Music mainTheme = new Music("MainTheme").SetVolume(0.6f)
.SetLoop(true).SetSpatialSound(false);  
mainTheme.Play();  

📝 Advanced Example

Music dungeonTheme = new Music(Track.Dungeon)  
    .SetVolume(0.4f)  
    .SetLoop(true)            // Loops the track when it ends
    .SetFadeOut(2.5f)         // 2.5-second fade-out at the end
    .SetOutput(Output.Music)  // Assigns it to a specific output channel
    .OnPause(() => Debug.Log("Dungeon theme paused"))  
    .Play();  

👉 Configuration of a dungeon theme with low volume, 2.5-second fade-out, loop enabled, and a callback when paused.

Last updated