Adaptive Music in Unity: Use the Exported Script

intermediate · about 30 minutes · checked

You will set up the exported controller in Unity and change the music from your game code.

All lessons

To add adaptive music in Unity with a Mowjera export, copy Mowjera.cs into your scripts, put the audio files in Assets/StreamingAssets/Mowjera/, add the Mowjera component to a GameObject, then call TransitionTo when something happens in your game. The script crossfades between your states.

The authors of this guide have not tested these exports inside Unity. The steps follow the code the exporter generates. Test them in your own project.

Before you start

  • Export your game music bundle and unzip it.
  • You should know how to create and attach a C# script in Unity. Adaptive music is music that changes while the game is played.
Export panel with 1 the Export Bundle button
Export panel with 1 the Export Bundle button

Step 1: copy the files

  1. Copy unity/Mowjera.cs into Assets/Scripts/.
  2. Create the folder Assets/StreamingAssets/Mowjera/.
  3. Copy every file from the zip's audio/ folder into it. Do not rename them.

Step 2: add the component

Add the Mowjera component to one GameObject in your first scene. It keeps itself alive when you load another scene. If a second copy appears, the script destroys that copy's whole GameObject, so give the component an object of its own.

Step 3: start and change the music

The script loads the audio files after the game starts. If you call Play before a file has loaded, that file is skipped, so do not call it directly in Start. This example waits one second. The script has no "ready" signal, so choose a delay that suits your project.

The examples use states named Unseen and Alarm. Use your own names, as listed in the GameMusicState list at the top of the script.

using System.Collections;
using UnityEngine;

public class MusicDirector : MonoBehaviour
{
    IEnumerator Start()
    {
        yield return new WaitForSeconds(1f);
        Mowjera.Mowjera.Instance.Play(Mowjera.GameMusicState.Unseen);
    }

    public void OnPlayerSpotted()
    {
        var music = Mowjera.Mowjera.Instance;
        music.transitionMode = Mowjera.TransitionMode.Immediate;
        music.fadeTime = 0.5f;
        music.TransitionTo(Mowjera.GameMusicState.Alarm);
    }
}

Call OnPlayerSpotted from your game logic. StopAll() stops the music.

Which settings can you change?

NameWhat it doesStarting value
fadeTimeSeconds a crossfade takes, also for Immediate2
transitionModeImmediate, NextBar or NextPhrase (two bars)NextBar
masterVolumeOverall music volume, 0 to 11

A bar is a short group of beats.

Are your transition times applied automatically?

No. The controller uses one fadeTime and one transitionMode for every change. The timing and fade you set on each arrow are written to metadata.json, along with any condition text, but the script does not read that file. Set the two values before each TransitionTo call, as above. See game music transitions.

How do you control layers?

If you exported with Layer stems, the generated script has extra members. Parameter values run from 0 to 1.

var music = Mowjera.Mowjera.Instance;
music.SetIntensity(0.7f);
music.SetParameter("danger", 0.4f);
float danger = music.GetParameter("danger");

danger is only an example: it works if your score defines a parameter with that id. The comment at the top of the script and the README show your ids. parameterRamp (0.35 seconds) sets how quickly layers fade. Without layer stems, these methods do not exist.

What you should hear

The first state starts and loops. When you call TransitionTo, the music crossfades to the new state.

If something goes wrong

ProblemFix
Silence and a "could not load" warningCheck the folder path and that the file names are unchanged
Silence without a warningPlay may have run before loading finished. Wait longer, then call Play again
A change waits while the game is pausedA delayed switch uses scaled game time, so with Time.timeScale at 0 it waits. Use Immediate for pause menus
"unknown parameter" warningUse the exact parameter id from the script
Loading fails on a phone or web buildThe script builds a file:// address, which suits desktop. Check how your platform reads StreamingAssets and adapt LoadClips

What to try next

Rehearse the changes in the browser with test your adaptive music, or compare the Godot version.

Try it now

Reading helps; clicking teaches. Open your dashboard and do the steps.

Open my dashboard

From the blog