using UnityEngine.SceneManagement; /// /// LOAD command. Load the specified scene by name. /// namespace UnityConsole.Commands { public static class LoadSceneCommand { public static readonly string name = "LoadScene"; public static readonly string description = "Load the specified scene by name. Before you can load a scene you have to add it to the list of levels used in the game. Use File->Build Settings... in Unity and add the levels you need to the level list there."; public static readonly string usage = "Load scene"; public static ConsoleCommandResult Execute(params string[] args) { if (args.Length == 0) { return HelpCommand.Execute(name); } return LoadLevel(args[0]); } private static ConsoleCommandResult LoadLevel(string sceneName) { try { SceneManager.LoadScene(sceneName, LoadSceneMode.Single); } catch { return ConsoleCommandResult.Failed($"Failed to load {sceneName}."); } Scene scene = SceneManager.GetSceneByName(sceneName); if (scene.IsValid() && scene.isLoaded) { // Assume success if we had to load the scene we were already in return ConsoleCommandResult.Succeeded($"Success loading {sceneName}."); } return ConsoleCommandResult.Failed( $"Failed to load {sceneName}. Are you sure it's in the list of levels in Build Settings?"); } } }