Using Maestro with Unity Scene Switching

Loading and unloading scene in Unity is a good method to structure your application especially for a complex one.

If you are not aware of the technical aspect for loading Unity scene, I encourage you to read this excellent article.

How Maestro prefab works with scene switching?

A quick answer could be: like any other Unity gameObject. Maestro is build with standard Unity components, so it’s pretty straightforward!

A very simple case

Your Maestro prefab is loaded in a scene and you want to load another scene.

In this case, you will use the Additive mode. The new scene is loaded but the current one stay alive.

SceneManager.LoadScene("SwitchSceneChild", LoadSceneMode.Additive);
Code language: CSS (css)

Obviously, your Maestro prefab stay alive!

A more complex case

I want to loading my MidiFilePlayer in a scene and switch to others scenes without reloading any prefab and/or any break in the music.

In this case, you will use the Single mode. The new scene replaces the current scene and all gameObjects in this scene are unloaded including your Maestro Prefab.

SceneManager.LoadScene("SwitchSceneChild", LoadSceneMode.Single);
Code language: CSS (css)


Unity provides for a mechanism to preserve gameObject you want, thanks to DontDestroyOnLoad.

It’s as simple as that:


Create an empty gameObject (or reuse one), add a script for loading, for example a MidiFilePlayer, and apply DontDestroyOnLoad on the gameObject. When the scene is unloaded the full gameObject will be preserved including the MidiFilePlayer.

Other mechanisms are used in this script not directly related to scenes switching:

  • Give access to the MidiFilePlayer from others gameObject/script. Thanks to the singleton instance of a class (pure C#).
  • Loading a MidiFilePlayer dynamically by directly instantiating the class or loading from the prefab in the hierarchy.
/// Access to the MidiFilePlayer from the static class and /// its singleton instance: /// with LoadMidiFilePlayer.Instance.midiFilePlayer.MPTK_Next(); public MidiFilePlayer midiFilePlayer; /// <summary> /// Dynamically MidiFilePlayer loading or from a prefab in the hierarchy /// </summary> public bool dynamicMidiFilePlayerLoading; /// <summary> /// Singleton for this instance /// </summary> public static LoadMidiFilePlayer Instance; private void Awake() { // create a singleton for this instance if (Instance != null) { Destroy(gameObject); return; } Instance = this; // don't destroy it when the scene is unloaded DontDestroyOnLoad(gameObject); if (dynamicMidiFilePlayerLoading) { Debug.Log("Dynamically add MidiFilePlayer components."); Debug.Log("Nevertheless, loading a MidiFilePlayer prefab from the hierarchy is also possible."); // MidiPlayerGlobal is a singleton: only one instance can be created. if (MidiPlayerGlobal.Instance == null) gameObject.AddComponent<MidiPlayerGlobal>(); // When running, this component will be added to this gameObject. Set essential parameters. // Nevertheless loading a MidiFilePlayer prefab from the hierarchy is also possible. midiFilePlayer = gameObject.AddComponent<MidiFilePlayer>(); } else { Debug.Log("Load MidiFilePlayer Prefab from hierarchy."); Debug.Log("Nevertheless, dynamically loading MidiFilePlayer components is also possible."); midiFilePlayer = FindObjectOfType<MidiFilePlayer>(); if (midiFilePlayer == null) { Debug.LogWarning("Can't find a MidiFilePlayer Prefab in the current Scene Hierarchy. Add it with the MPTK menu."); return; } } midiFilePlayer.MPTK_CorePlayer = true; midiFilePlayer.MPTK_DirectSendToPlayer = true; midiFilePlayer.MPTK_MidiIndex = 10; midiFilePlayer.MPTK_Play(); }
Code language: C# (cs)

Get the demo!

Download this package for demonstration. It will be also available with the version 2.9.2 in the folder MidiPlayer\Assets\MidiPlayer\Demo\FreeMVP\SwitchScene.

Unzip and import this package in your Unity project. You need to already have Maestro Free or Pro in your project.

Add the scenes SwitchScene and SwitchSceneChild in your hierarchy. Unload scene SwitchSceneChild.
Your hierarchy after adding the scenes.
Add the two scenes in the Build Settings and run!

Script for switching between scenes is quite short (SwitchSceneController.cs):

public void LoadSceneChild() { SceneManager.LoadScene("SwitchSceneChild", LoadSceneMode.Single); } public void LoadSceneHome() { SceneManager.LoadScene("SwitchScene", LoadSceneMode.Single); }
Code language: JavaScript (javascript)

Script for access to the MidiFilePlayer from another scene is also very short (NextMidiFile.cs):

public void NextMidi() { // Access to the MidiFilePlayer from the static class and its singleton instance LoadMidiFilePlayer.Instance.midiFilePlayer.MPTK_Next(); }
Code language: JavaScript (javascript)

Have Fun!

Have any Question or Comment?

Leave a Reply

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

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.