Skip to content

Instantly share code, notes, and snippets.

@flarb
Created December 20, 2012 08:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flarb/4343900 to your computer and use it in GitHub Desktop.
Save flarb/4343900 to your computer and use it in GitHub Desktop.
Lets you save the currently selected Unity3D scene. Then automatically load and run the saved scene via keyboard shortcut. If you break up your Unity3D projects into multiple scenes--including a loading scene--this saves a lot of manual switching of scenes.
using UnityEngine;
using System.Collections;
using UnityEditor;
/*
* Written by Ralph A. Barbagallo III
*
* www.ralphbarbagallo.com
*
* I wrote this becuase I was sick of switching scenes to play my loader scene and test my game.
* If you break up your game into multiple scenes, this could be a timesaver. Even if it is
* ridiculously simplistic
*/
public class PlayScene : EditorWindow {
/*
* SetScene
*
* Saves the currently selected scene name to the prefs. Bails
* if not a scene.
*/
[MenuItem("Window/Set Scene")]
public static void SetScene()
{
Object sel = Selection.activeObject;
if (sel == null)
{
Debug.LogError("No scene selected.");
return;
}
string sceneName = AssetDatabase.GetAssetOrScenePath(sel);
if (!sceneName.EndsWith(".unity"))
{
Debug.LogError("Not a scene.");
return;
}
EditorPrefs.SetString("runscene", sceneName);
Debug.Log("Scene set to: " + sceneName);
}
/*
* PlaySetScene
*
* Load scene at path saved in the prefs, then run it.
*/
[MenuItem("Window/Play Scene %p")]
public static void PlaySetScene()
{
string sceneName = EditorPrefs.GetString("runscene", null);
if (sceneName == null)
{
Debug.LogError("No scene saved. Select scene and use SetScene to choose the scene to load.");
return;
}
EditorApplication.OpenScene(sceneName);
EditorApplication.ExecuteMenuItem("Edit/Play");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment