using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; /// /// SystemController singleton /// public class SystemController { /// /// The instance controlling the singleton /// private static SystemController instance = null; /// /// Stack of the loaded scenes, used to easily go back to previous scenes /// private Stack sceneStack = new Stack(); /// /// Get the instance loaded by the singleton /// /// SystemController instance public static SystemController GetInstance() { // Create a new instance if non exists if (instance == null) { instance = new SystemController(); instance.sceneStack.Push(SceneManager.GetActiveScene().buildIndex); } return instance; } /// /// Load the scene and push on the stack /// /// Path of the scene public void LoadNextScene(string scenePath) { LoadNextScene(SceneUtility.GetBuildIndexByScenePath(scenePath)); } /// /// Load the scene and push on the stack /// /// Buildindex of the scene public void LoadNextScene(int sceneIndex) { sceneStack.Push(sceneIndex); SceneManager.LoadScene(sceneIndex); } /// /// Swap the current scene with the new scene on the stack /// /// Path of the scene public void SwapScene(string scenePath) { SwapScene(SceneUtility.GetBuildIndexByScenePath(scenePath)); } /// /// Swap the current scene with the new scene on the stack /// /// Buildindex of the scene public void SwapScene(int sceneIndex) { sceneStack.Pop(); LoadNextScene(sceneIndex); } /// /// Go back to the previous scene and unload the current scene /// public void BackToPreviousScene() { sceneStack.Pop(); if (sceneStack.Count > 0) SceneManager.LoadScene(sceneStack.Peek()); else Application.Quit(); } /// /// Go back to a specific scene, unloading all the scenes on the way /// /// Path of the scene public void BackToScene(string scenePath) { BackToScene(SceneUtility.GetBuildIndexByScenePath(scenePath)); } /// /// Go back to a specific scene, unloading all the scene on the way /// /// Buildindex of the scene public void BackToScene(int sceneIndex) { while (0 < sceneStack.Count && sceneStack.Peek() != sceneIndex) sceneStack.Pop(); if (sceneStack.Count > 0) SceneManager.LoadScene(sceneStack.Peek()); else Application.Quit(); } }