Search code examples
unity-game-enginehololensscene

SceneSystem.LoadContent doesn't unload the previous scene even with LoadSceneMode.Single


I'm creating an Hololens menu app. After various issues I'm now able to load the menu scene, click on a button to load a scene for example cube scene. My scene structure is the following one :

  • loading : empty scene which loads the menu
  • main : the menu scene, contains a menu with two buttons. Button1 loads cube scene and Button2 loads circle scene
  • cube : a simple scene with a 3D cube
  • circle : a simple scene with a 3D sphere

The problem
The problem is the fact that when I switch from menu to cube scene it doesn't unload the previous scene. So you can still see the menu in the cube scene. Here are my MRTK Scene System settings .

What I've tried
I have in total three scripts, the same as the Microsoft sample:
LoadFirstScene is the script contained in the loading scene which loads the menu and works :

using System.Collections;
using System.Collections.Generic;
using Microsoft.MixedReality.Toolkit;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadFirstScene : MonoBehaviour
{
    private static bool IsFirstLoad = true;
    private void Start()
    {
        if (IsFirstLoad)
        {
            IsFirstLoad = false;
            CoreServices.SceneSystem.LoadContent("main", LoadSceneMode.Single);
        }
    }
}

Then I have SceneLoader which is where the bug is happening I belive. It's supposed to load the next scene and unload the current one since I use LoadSceneMode.Single :

using System.Collections;
using System.Collections.Generic;
using Microsoft.MixedReality.Toolkit;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{

    /// Request that the MRTK SceneSystem load a scene of a given name.
    public void LoadScene(string sceneName) => CoreServices.SceneSystem.LoadContent(sceneName, LoadSceneMode.Single);
}

Finally I have LoadingSceneUtilities which is contained in main cube and circle scene in order to load the base scene (loading) :

using UnityEngine;
using UnityEditor;
#if UNITY_EDITOR
using UnityEditor.SceneManagement;
using UnityEditor.Experimental.SceneManagement;
#endif

[ExecuteInEditMode]
public class MainSceneUtilities : MonoBehaviour
{
#if UNITY_EDITOR
        void Start()
        {
            if (EditorApplication.isPlaying)
            {
                // In play mode, MRTK handles scene loading.
                return;
            }

            if (PrefabStageUtility.GetCurrentPrefabStage() != null)
            {
                // Do not additively load the base scene while editing a prefab.
                return;
            }

            UnityEngine.SceneManagement.Scene baseScene = EditorSceneManager.OpenScene("Assets/Scenes/loading.unity", OpenSceneMode.Additive);
            EditorSceneManager.SetActiveScene(baseScene);
        }
#endif
}

I don't understand why when I switch from main to cube, main is still loaded and displayed. It may be linked to the 3rd script, but since in the first one there is the boolean check it shouldn't be a problem.

I really took a look at the sample since it's working fine but I can't figure out why it isn't working on my own project.


Solution

  • I basically found my problem on this Scene System documentation page

    In MRTK Scene System settings I forgot to set the Content Scene. After adding the 3 scenes (menu cube circle) everything is working fine !

    MRTK settings