From Unify Community Wiki

Author: User:Yoyo

Description

This editor script lets you work in one scene but load another one when you press play in the Unity Editor. This functionality is useful when you are working on a scene for a level or section of the world, but you need to load a different "master scene" in order to play your game. By automatically switching to and from the master scene when you press play in the editor, the scene auto-loader speeds up your workflow.

This script is a complete reimplementation of an idea discussed on this forum thread.





Usage

Save the script below as Assets/Editor/SceneAutoLoader.cs. The script will be automatically activated when you open your project, creating a "Scene Autoload" sub-menu on the Unity Editor File menu.

The menu options are:

Select Master Scene... - choose the scene that should be loaded when you press play

Load Master On Play - select this to auto-load your master scene (if greyed out then this is the active option)

Don't Load Master On Play - select this to disable auto-load of your master scene (if greyed out then this is the active option)



This script is tested, but as with all community scripts, usage is at your own risk.





How It Works

The script uses InitializeOnLoad to auto-run its static constructor, which attaches a callback to EditorApplication.playmodeStateChanged. This callback will load the designated master scene when the user presses play, and reload the previous scene when they press stop.

Menu items are created to select the master scene, using EditorUtility.OpenFilePanel, and to enabled and disable auto-loading of the master scene.

Before auto-loading the master scene, EditorApplication.SaveCurrentSceneIfUserWantsTo is used to make sure edits in the sub-scene aren't thrown away without the user knowing. If the user chooses to cancel the save scene operation then the editor play operation is also cancelled.

EditorPrefs are used to remember the user's auto-loading preferences.

This forum thread discusses changes for the latest Unity API.

Revisions

March 7, 2013: Initial revision.

October 15, 2017: Updated to latest API.

April 14, 2018: Tiny API tweak.

C# - SceneAutoLoader.cs