Skip to content

Instantly share code, notes, and snippets.

@BigBallard
Last active December 3, 2022 16:03
Show Gist options
  • Save BigBallard/fe900234c8fa698014373c34901189e5 to your computer and use it in GitHub Desktop.
Save BigBallard/fe900234c8fa698014373c34901189e5 to your computer and use it in GitHub Desktop.
Unity Editor script to toggle loading entry scene at beginning of play mode.
/// <summary>
/// Menu options to enable or disable the loading of the 0th index entry scene in the build settings when play mode
/// is started. This is useful if you are working on a scene that is later in the intended scene flow of your game when
/// developing.
/// </summary>
[InitializeOnLoad]
public static class PlayModeEntryMenu
{
private const string EntryLoadKey = "editor.entry.load";
private static string _prevScene;
static PlayModeEntryMenu()
{
if (!PlayerPrefs.HasKey(EntryLoadKey)) PlayerPrefs.SetInt(EntryLoadKey, 0);
EditorApplication.playModeStateChanged += change =>
{
if (change != PlayModeStateChange.ExitingEditMode) return;
if (PlayerPrefs.GetInt(EntryLoadKey) != 1) return;
var firstScene = EditorBuildSettings.scenes[0].path;
var entryAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(firstScene);
EditorSceneManager.playModeStartScene = entryAsset;
};
}
[MenuItem("File/Play Mode/Enable Entry Load")]
public static void EnableEntryLoad()
{
PlayerPrefs.SetInt(EntryLoadKey, 1);
}
[MenuItem("File/Play Mode/Enable Entry Load", true)]
public static bool EnableEntryLoadValidate()
{
return PlayerPrefs.GetInt(EntryLoadKey) == 0;
}
[MenuItem("File/Play Mode/Disable Entry Load")]
public static void DisableEntryLoad()
{
PlayerPrefs.SetInt(EntryLoadKey, 0);
}
[MenuItem("File/Play Mode/Disable Entry Load", true)]
public static bool DisableEntryLoadValidate()
{
return PlayerPrefs.GetInt(EntryLoadKey) == 1;
}
}
@BigBallard
Copy link
Author

I created this Unity Editor script to help with simply starting Play Mode with the game's entry scene without having to go between the entry scene and the scene I am actively working on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment