using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; namespace Ashwild.EditorTools { /// /// Toggleable editor option that forces play mode to always start from the first build scene /// (index 0, the boot/menu scene), no matter which scene is open. When enabled, pressing the /// normal Play button boots from scene 0 and Unity automatically returns to the scene you were /// editing once you stop — so you never have to manually open the menu scene to test. /// The toggle shows a checkmark in the menu and its state persists across sessions. /// [InitializeOnLoad] public static class PlayFromFirstScene { #region Constants private const string MenuPath = "Tools/Ashwild/Play From Scene 0"; /// /// EditorPrefs key persisting whether the "boot from scene 0" override is enabled. /// private const string EnabledKey = "Ashwild.PlayFromFirstScene.Enabled"; #endregion #region Initialization /// /// Re-applies the saved toggle state after every domain reload so the play-mode start scene /// stays bound even after a recompile. /// static PlayFromFirstScene() { EditorApplication.delayCall += () => ApplyStartScene(IsEnabled); } #endregion #region Menu /// /// Flips the override on or off and immediately (un)binds the play-mode start scene. /// [MenuItem(MenuPath, priority = -100)] public static void Toggle() { bool enabled = !IsEnabled; EditorPrefs.SetBool(EnabledKey, enabled); ApplyStartScene(enabled); } /// /// Draws the checkmark next to the menu entry to reflect the current state. /// [MenuItem(MenuPath, validate = true)] public static bool ToggleValidate() { Menu.SetChecked(MenuPath, IsEnabled); return true; } #endregion #region Internal Helpers /// /// Whether the override is currently enabled, read from persisted EditorPrefs. /// private static bool IsEnabled => EditorPrefs.GetBool(EnabledKey, false); /// /// Binds (or clears) Unity's playModeStartScene to build scene 0. While bound, every play /// session starts there and returns to the open scene on stop; clearing restores the default /// behaviour of playing the currently open scene. Logs and self-disables if scene 0 is missing. /// private static void ApplyStartScene(bool enabled) { if (!enabled) { EditorSceneManager.playModeStartScene = null; return; } if (EditorBuildSettings.scenes.Length == 0 || string.IsNullOrEmpty(EditorBuildSettings.scenes[0].path)) { Debug.LogError("[PlayFromFirstScene] No scene at Build Settings index 0 — add the menu scene there."); EditorPrefs.SetBool(EnabledKey, false); EditorSceneManager.playModeStartScene = null; return; } SceneAsset firstScene = AssetDatabase.LoadAssetAtPath(EditorBuildSettings.scenes[0].path); EditorSceneManager.playModeStartScene = firstScene; } #endregion } }