I would need to use EditorSceneManager to load the scene for play mode unit test. I need to use this one and not SceneManager.Load since I cannot have the scene in Build settings. The documentation about EditorSceneManager is clear that it is supposed to do what I need.
protected void LoadScene(string sceneName, Action onComplete)
{
if (string.IsNullOrEmpty(sceneName)) { Debug.LogError("Missing scene name"); return;}
UnityEditor.SceneManagement.EditorSceneManager.LoadScene(sceneName);
//...
}
I get error that the scene is not in the build settings as if SceneManager.LoadScene was called instead of the editor version.
Compiler also suggests to simplify to the following that I do not want to use:
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
though I have no directives that could confuse the code:
using System;
using System.Collections;
using UnityEditor;
using UnityEngine;
I was using a side solution with IPrebuildSetup, IPostBuildCleanup to add/remove the scene via the AssetDatabase and EditorBuildSettingsScene and it works. But it also runs the scene loading/unloading process in Jenkins while building the prod app. I need to avoid that since it could lead to shipping assets that are only used in test scenes.
The EditorSceneManager
inherits from SceneManager
that's why it provides the same method LoadScene
which afaik does the same thing, meaning there is no override or something like this for the EditorSceneManager
.
However it provides additional methods you can and should use for editor specific stuff.
You can use LoadSceneInPlayMode
This method allows you to load a Scene during playmode in the editor, without requiring the Scene to be included in the Build Settings Scene list.
UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode(sceneName);
Note: The parameter is called path
, not sceneName
, for a reason ;)
You have to provide the path as it is stored in Scene.path
Returns the relative path of the Scene. Like: "Assets/MyScenes/MyScene.unity"
Ok, that actually was it. I have used that one and its async version to no avail, I would just get an empty scene with the right naming. The problem is in the scene path, it needs to be as "Assets/<full_path>/sceneName.unity" and not just scene name as I did. Maybe you want to add that to the answer since I can see the problem to be a bit all over Google and most of the time it ends with "Ok I fixed it" and no actual solution.
Added it see the update above ;)