Skip to content

Instantly share code, notes, and snippets.

@FVSHaLuan
Last active April 26, 2017 21:34
Show Gist options
  • Save FVSHaLuan/ae4eec40331cdf17ddc0 to your computer and use it in GitHub Desktop.
Save FVSHaLuan/ae4eec40331cdf17ddc0 to your computer and use it in GitHub Desktop.
**Function: - Add menu DoC/Start with first game scene to Unity3D editor main menus. - The menu starts playing your game from the specified scene and brings you back to the scene you were editting when stop playing
using UnityEngine;
using System.Collections;
using UnityEditor;
/* by FVS - Ha Luan */
public class StartWithSpecificScene : Editor
{
const string settingKey = "FVSHaluanStartWithSpecificScene";
const string firstScenePath = "Assets/Scenes/Login.unity";
static string sceneBeforeStart = "";
static bool isInited = false;
[MenuItem("DoC/Start with first game scene")]
static void StartGame()
{
if (!isInited)
{
Init();
}
if (EditorApplication.isPlaying)
{
EditorApplication.isPlaying = false;
return;
}
sceneBeforeStart = EditorApplication.currentScene;
EditorApplication.SaveCurrentSceneIfUserWantsTo();
EditorApplication.OpenScene(firstScenePath);
SaveScenePath();
EditorApplication.isPlaying = true;
}
[InitializeOnLoadMethod]
static void Init()
{
EditorApplication.playmodeStateChanged += OnUnityPlayModeChanged;
isInited = true;
}
static void OnStopGame()
{
LoadScenePath();
if (!string.IsNullOrEmpty(sceneBeforeStart))
{
EditorApplication.OpenScene(sceneBeforeStart);
sceneBeforeStart = "";
}
}
static void SaveScenePath()
{
EditorPrefs.SetString(settingKey, sceneBeforeStart);
}
static void LoadScenePath()
{
sceneBeforeStart = EditorPrefs.GetString(settingKey, "");
}
static void OnUnityPlayModeChanged()
{
if (EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode)
{
OnStopGame();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment