Unity Hack: Scene Switcher Script

This is the most useful Editor script I that regularly use in Unity since working on Life Not Supported. It allows you to quickly switch between any scene that’s been set in Build Settings->Scenes In Build.

Just place this script in your Editor folder then open the window under Tools->SceneSwitcher (There’s a copy button on the top right of the code window).

using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

public class SceneSwitcher : EditorWindow 
{
    private Vector2 scrollPos;
    [MenuItem("Tools/SceneSwitcher")]
    internal static void Init()
    {
        var window = (SceneSwitcher)GetWindow(typeof(SceneSwitcher), false, "Scene Switcher");
        window.position = new Rect(window.position.xMin + 100f, window.position.yMin + 100f, 200f, 400f);
    }

    internal void OnGUI()
    {
        EditorGUILayout.BeginVertical();
        this.scrollPos = EditorGUILayout.BeginScrollView(this.scrollPos, false, false);

        GUILayout.Label("Scene Selector", EditorStyles.boldLabel);
        for (var i = 0; i < EditorBuildSettings.scenes.Length; i++)
        {
            var scene = EditorBuildSettings.scenes[i];
            if (scene.enabled)
            {
                var sceneName = Path.GetFileNameWithoutExtension(scene.path);
                var pressed = GUILayout.Button("[" + i + "] " + sceneName, new GUIStyle(GUI.skin.GetStyle("Button")) { alignment = TextAnchor.MiddleLeft });
                if (pressed)
                {

                    if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
                    {
                        EditorSceneManager.OpenScene(scene.path);
                    }
                }
            }
        }

        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndVertical();
    }
}

Then you can easily dock it wherever suits you. I like to have it inbetween the Hierarchy and Scene View (Where I also dock ProBuilder).

That’s it! Thats the end of the page.

Leave a comment

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