Resource| Unity3D & C# | Quickload Scenes

I frequently find myself toggling between scenes in Unity, so I thought I’d share a quick script that I wrote that I use to streamline the process. Today, we’ll be creating a custom menu in the Unity editor that will contain a list of our most-used scenes. When we click on one of those scenes in the menu, Unity will load the scene for us. Pretty simple, but very useful!

As usual, please feel free to copy code in this tutorial, but please don’t paste it. If you want to learn how a piece of code works, it’s always best to write it down yourself! In doing so, hopefully you’ll produce a few typos, forcing you to debug the code and maybe understand it a little better.

Getting Started

We’ll start out by including UnityEditor and UnityEditor.SceneManagement since we’re going to extend the Unity editor itself.

(If you’re not sure what a Namespace is, watch this).

using UnityEngine;  
using UnityEditor;  
using UnityEditor.SceneManagement;

public class QuickSceneSelect : MonoBehaviour {

}

To keep things clean and reusable, let’s add a string variable to store the location of our scene folder. I keep my scenes in Assets/Custom Assets/Scenes. You can set this field to whatever works best for your project structure.

private static string _defaultScenePath = "Assets/Custom Assets/Scenes";  

We need to keep _defaultScenePath static because we will be using it in static methods later on. If you try to reference a non-static member in a static method, you’ll get an error and nobody likes those.

Writing a Workhorse Method

Let’s create a small method to load a scene for us. We can reuse it whenever we want to add a new scene to our custom menu.

private static void LoadScene(string _sceneName)  
{
    EditorSceneManager.OpenScene(_defaultScenePath + _sceneName + ".unity");
}

Creating the Custom Menu

Now we’ll add items to our custom menu. To do this, we first want to write an Attribute called [MenuItem()] and pass in the hierarchy of our custom menu and the name of our item. I think we should call the menu “Custom Tools” and then have a sub-menu called “Quickload Scene.” Let’s call our first scene “Menu.”

(If you’re not sure what an Attribute is, watch this).

[MenuItem("Custom Tools/Quickload Scene/Menu")]

Now we write the method to load the Menu and call our handy LoadScene()method from earlier.

private static void LoadMenu()  
{
    LoadScene("Menu");
}

Tying It All Together

Here’s what our shiny new QuickSceneSelect script looks like all put together.

using UnityEngine;  
using UnityEditor;  
using UnityEditor.SceneManagement;

public class QuickSceneSelect : MonoBehaviour {  
    private static string _defaultScenePath = "Assets/Custom Assets/Scenes";

    private static void LoadScene(string _sceneName)
    {
        EditorSceneManager.OpenScene(_defaultScenePath + _sceneName + ".unity");
    }

    [MenuItem("Custom Tools/Quickload Scene/Menu")]
    private static void LoadMenu()
    {
        LoadScene("Menu");
    }
}

If you want to add more items to your “Quickload Scene” menu, just do something like this…

[MenuItem("Custom Tools/Quickload Scene/Stats")]
private static void LoadStats()  
{
    LoadScene("Stats");
}

… or this!

[MenuItem("Custom Tools/Quickload Scene/Gameplay")]
private static void LoadGameplay()  
{
    LoadScene("Gameplay");
}

Wrapping Up

Finally, make sure that you put this script in your “Assets/Editor” folder. If you don’t put your editor scripts in the right place, Unity will have problems when you try to Build your game. If, for any reason, you don’t want to keep your editor scripts in the Editor folder, you can always wrap your editor code with #if UNITY_EDITOR and #endif.

Editor scripts are a really handy way to boost your productivity. I use them all the time. If you have any cool ideas for custom editor scripts, or if you have any questions, feel free to hit me up on Twitter or send me a message on Reddit. Hope you enjoyed this quick tutorial!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s