Example #1
0
    // Play mode change callback handles the scene load/reload.
    private static void OnPlayModeChanged(PlayModeStateChange state)
    {
        if (!LoadMasterOnPlay)
        {
            return;
        }

        if (!EditorApplication.isPlaying && EditorApplication.isPlayingOrWillChangePlaymode)
        {
            // User pressed play -- autoload master scene.
            for (int i = 0; i < EditorSceneManager.sceneCount; i++)
            {
                if (i == 0)
                {
                    PreviousScene = EditorSceneManager.GetSceneAt(i).path;
                }
                else
                {
                    PreviousScene += ":" + EditorSceneManager.GetSceneAt(i).path;
                }
            }
            if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
            {
                try
                {
                    EditorSceneManager.OpenScene(MasterScene);
                }
                catch
                {
                    Debug.LogError(string.Format("error: scene not found: {0}", MasterScene));
                    EditorApplication.isPlaying = false;
                }
            }
            else
            {
                // User cancelled the save operation -- cancel play as well.
                EditorApplication.isPlaying = false;
            }
        }

        // isPlaying check required because cannot OpenScene while playing
        if (!EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode)
        {
            // User pressed stop -- reload previous scene.
            try
            {
                char[] split = { ':' };
                foreach (var scene in PreviousScene.Split(split, StringSplitOptions.RemoveEmptyEntries))
                {
                    EditorSceneManager.OpenScene(scene, OpenSceneMode.Additive);
                }
            }
            catch
            {
                Debug.LogError(string.Format("error: scene not found: {0}", PreviousScene));
            }
        }
    }
        public static void OnPlayModeChanged(PlayModeStateChange state)
        {
            if (!loadMainMenuOnEditorPlay)
            {
                return;
            }


            if (state == PlayModeStateChange.ExitingEditMode)
            {
                // User pressed play -- autoload initials scene.
                if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
                {
                    List <string> paths = new List <string>();
                    for (int i = 0; i < EditorSceneManager.sceneCount; i++)
                    {
                        paths.Add(EditorSceneManager.GetSceneAt(i).path);
                    }

                    PreviousScene = string.Join(splitKeyS, paths);

                    string mainMenuScenePath = GameManagerSettings.instance.mainMenuScenePath;

                    try {
                        EditorSceneManager.OpenScene(mainMenuScenePath);
                    }
                    catch {
                        Debug.LogError("Error: scene not found: " + mainMenuScenePath);
                        loadMainMenuOnEditorPlay    = false;
                        EditorApplication.isPlaying = false;
                    }
                }
                else
                {
                    // User cancelled the save operation -- cancel play as well.
                    loadMainMenuOnEditorPlay    = false;
                    EditorApplication.isPlaying = false;
                }
            }
            // User pressed stop -- reload previous scene.
            else if (state == PlayModeStateChange.EnteredEditMode)
            {
                loadMainMenuOnEditorPlay = false;

                string[] scenes = PreviousScene.Split(splitKey);
                for (int i = 0; i < scenes.Length; i++)
                {
                    try     { EditorSceneManager.OpenScene(scenes[i], i == 0 ? OpenSceneMode.Single : OpenSceneMode.Additive); }
                    catch   { Debug.LogError("Error: scene not found: " + scenes[i]); }
                }
            }
        }