Using Unity Editor scripting to create a scene quick access

Sign in to queue

Description

As you may all know, Unity makes use of a lot of scenes - and everyone who has worked with Unity for a bit will know that the Project View can get a bit overloaded.

This can make it a bit painful to quickly switch between scenes if you need to.

This is where Unity Editor Scripting comes in really handy - I will show you how to create a little quick access menu that enables you to quickly switch between predefined scenes. With this method you will have to manually add new scenes to the script as you add them to your game. In my example project, I have three game scenes: a start scene, the main game and a game over screen.

Let's start, first things first.

1. Create a new folder called Editor

This should be on the top level of your project view, and make sure that you really called it Editor - it is a special folder name that indicates that it's for use inside the editor. This also causes unity to exclude those scripts from the actual game build. The final folder structure should look like this:

Generic Episode Image

2. In this folder, create a new Script - I will use C# for my example.

You could call this, for example, OpenScene

Generic Episode Image.

3. Open that newly created script

4. and add the following statement on top of the code:

using UnityEditor;

Generic Episode Image

(this is what it should look like).

Now you're ready to actually get started. First, get rid of the existing methods:

void Start()
{

}

void Update()
{

}

They are irrelevant for our use.

5. Add a new Menu Item

Add a new line of code, directly below

public class OpenScene : MonoBehaviour {

which should say the following:

[MenuItem("OpenScene/Start Scene")]

Explanation:

MenuItem adds a new menu item. The hierarchy for this is defined in the string within the brackets. In this case, OpenScene will be on the same level as File and Component, for example, and Start Scene is an item of OpenScene.

Or, visually more appealing:

Generic Episode Image

Now lets start with accessing said Start Scene - for which we will add the following method just below the line of code we just entered:

static void StartScene()
{
 
}

 In case the menu item doesn't show up: it may very well be that you forgot "static". Happened to me... ;)

6. Let's save the current scene before navigating away from it

Between the curly braces of the method that we just added, we only need two more lines of code. The first will allow the user to save any changes before they move away from the current scene. This is done by the following line of code:

 

EditorApplication.SaveCurrentSceneIfUserWantsTo();

Which will open the following dialog:

Generic Episode Image

7. Open a different scene

To start a new scene (in my case the start scene) use the following line of code:

EditorApplication.OpenScene("Assets/Scenes/StartScreen.unity");

EditorApplication.OpenScene is pretty self explanatory - it opens the scene which you specify in the string section which it is followed by.

The path to the scene always always always starts with "Assets/" - as that is the first folder you need to access before you get to the files in your project view (check in the explorer view). The rest depends on your folder structure. My start scene is in the folder "Scenes" and is called "StartScreen". All scenes have the file extention .unity

Now your code should look something like this:

Generic Episode Image

8. Now save everything and go back to Unity to try it out.

Have fun!

Tags:

C#, editor, Unity, tutorial

The Discussion

Add Your 2 Cents