Last active
May 6, 2026 11:57
-
-
Save frarees/16fe2b411c31403f2b3011c194dfcf07 to your computer and use it in GitHub Desktop.
Enter Play Mode from the initial scene
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Linq; | |
| using UnityEditor; | |
| using UnityEngine; | |
| using UnityEditor.SceneManagement; | |
| using UnityEngine.SceneManagement; | |
| [InitializeOnLoad] | |
| static class EnterPlay | |
| { | |
| private class SceneSetupObject : ScriptableObject | |
| { | |
| public SceneSetup[] sceneSetup; | |
| } | |
| private static readonly SceneSetupObject _sceneSetupObject; | |
| static EnterPlay() | |
| { | |
| _sceneSetupObject = Resources.FindObjectsOfTypeAll<SceneSetupObject>().FirstOrDefault(); | |
| if (!_sceneSetupObject) | |
| { | |
| _sceneSetupObject = ScriptableObject.CreateInstance<SceneSetupObject>(); | |
| } | |
| _sceneSetupObject.hideFlags = HideFlags.HideAndDontSave; | |
| EditorApplication.playModeStateChanged += OnPlayModeStateChanged; | |
| } | |
| private static void OnPlayModeStateChanged(PlayModeStateChange stateChange) | |
| { | |
| switch (stateChange) | |
| { | |
| case PlayModeStateChange.ExitingEditMode: | |
| _sceneSetupObject.sceneSetup = EditorSceneManager.GetSceneManagerSetup(); | |
| EditorSceneManager.OpenScene(SceneUtility.GetScenePathByBuildIndex(0)); | |
| break; | |
| case PlayModeStateChange.EnteredEditMode: | |
| EditorSceneManager.RestoreSceneManagerSetup(_sceneSetupObject.sceneSetup); | |
| break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment