Transition Music between Scenes in Unity using ADX2

In Unity, scenes represent levels, and they are used to break up games into small chunks. This means that scenes can be unloaded from memory before a new one is loaded, efficiently culling assets from hogging up system resources. However, sometimes we want things to persist between levels, such as background music that transitions between menus and gameplay. Fortunately, ADX2 makes this functionality very easy to implement in Unity.

Setting the Scenes

To playback sounds with ADX2, we need a CriWareLibraryInitializer, a CriWareErrorHandler, and an object with a Cri Atom component (this is called “CRIWARE” by default).

Each of these objects has a toggle option which can be found in the bottom of the Inspector labelled “Dont Destroy On Load”. By ticking the box for this setting in each of these objects, we can ensure that they are not destroyed when a new scene is loaded.

18-1

If the newly loaded scene already has these core CRI objects, ADX2 will automatically handle managing the correct configuration. This means that we don’t need to worry about accidentally creating duplicate objects. Note: Even though it may appear that copies are being created in the Hierarchy window, these duplicates are simply empty game objects.

Finally, by nesting the Cri Atom Source which contains our music inside of the object which contains our Cri Atom component (the default one named “CRIWARE”), we can ensure that the music will also be passed along to the new scene. With these settings enabled, our music should effortlessly continue playing between scenes.

Quick Fades

If we don’t want our music to continue playing across scenes, but also don’t want it to stop abruptly, we can instead fade our music across scenes. We could ensure this logic is set inside of Atom Craft, but since we only need it to happen between scene transitions in this instance, we can instead opt to perform this fade-out with some code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class StartScene : MonoBehaviour
{
    public CriAtomSource source;

    public void PlayGame()
    {
        source.player.SetEnvelopeReleaseTime(1000);
        source.Stop();

        SceneManager.LoadScene(1);
    }
}


This script simply sets the release time (to 1000ms) of our music Cue and tells it to stop playing. The new scene is then loaded, during which the music fades out based on our release time. And that’s all it takes to transition your music between different scenes in Unity!

Leave a Reply

Your email address will not be published. Required fields are marked *