134 lines
3.9 KiB
C#
134 lines
3.9 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
/// <summary>
|
|
/// SystemController singleton
|
|
/// </summary>
|
|
public class SystemController
|
|
{
|
|
/// <summary>
|
|
/// The instance controlling the singleton
|
|
/// </summary>
|
|
private static SystemController instance = null;
|
|
|
|
/// <summary>
|
|
/// Stack of the loaded scenes, used to easily go back to previous scenes
|
|
/// </summary>
|
|
private Stack<int> sceneStack = new Stack<int>();
|
|
|
|
/// <summary>
|
|
/// Index of the previous loaded scene
|
|
/// </summary>
|
|
public int previousScene = -1;
|
|
|
|
/// <summary>
|
|
/// Index of the current loaded scene
|
|
/// </summary>
|
|
public int currentScene = -1;
|
|
|
|
/// <summary>
|
|
/// Get the instance loaded by the singleton
|
|
/// </summary>
|
|
/// <returns>SystemController instance</returns>
|
|
public static SystemController GetInstance()
|
|
{
|
|
// Create a new instance if non exists
|
|
if (instance == null)
|
|
{
|
|
instance = new SystemController();
|
|
instance.currentScene = SceneManager.GetActiveScene().buildIndex;
|
|
instance.sceneStack.Push(instance.currentScene);
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the number of passively 'active' scenes
|
|
/// </summary>
|
|
public int GetSceneStackSize() { return sceneStack.Count; }
|
|
|
|
/// <summary>
|
|
/// Load the scene and push on the stack
|
|
/// </summary>
|
|
/// <param name="scenePath">Path of the scene</param>
|
|
public void LoadNextScene(string scenePath)
|
|
{
|
|
LoadNextScene(SystemController.GetSceneIndex(scenePath));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the index of a given scene
|
|
/// </summary>
|
|
/// <param name="scenePath">Path of the scene</param>
|
|
/// <returns></returns>
|
|
public static int GetSceneIndex(string scenePath)
|
|
{
|
|
return SceneUtility.GetBuildIndexByScenePath(scenePath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load the scene and push on the stack
|
|
/// </summary>
|
|
/// <param name="sceneIndex">Buildindex of the scene</param>
|
|
public void LoadNextScene(int sceneIndex)
|
|
{
|
|
previousScene = currentScene;
|
|
currentScene = sceneIndex;
|
|
|
|
sceneStack.Push(currentScene);
|
|
SceneManager.LoadScene(currentScene);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Swap the current scene with the new scene on the stack
|
|
/// </summary>
|
|
/// <param name="scenePath">Path of the scene</param>
|
|
public void SwapScene(string scenePath)
|
|
{
|
|
SwapScene(SystemController.GetSceneIndex(scenePath));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Swap the current scene with the new scene on the stack
|
|
/// </summary>
|
|
/// <param name="sceneIndex">Buildindex of the scene</param>
|
|
public void SwapScene(int sceneIndex)
|
|
{
|
|
currentScene = sceneStack.Pop();
|
|
LoadNextScene(sceneIndex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Go back to the previous scene and unload the current scene
|
|
/// </summary>
|
|
public void BackToPreviousScene()
|
|
{
|
|
previousScene = sceneStack.Pop();
|
|
if (sceneStack.Count > 0) SceneManager.LoadScene(currentScene = sceneStack.Peek());
|
|
else Application.Quit();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Go back to a specific scene, unloading all the scenes on the way
|
|
/// </summary>
|
|
/// <param name="scenePath">Path of the scene</param>
|
|
public void BackToScene(string scenePath)
|
|
{
|
|
BackToScene(SystemController.GetSceneIndex(scenePath));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Go back to a specific scene, unloading all the scene on the way
|
|
/// </summary>
|
|
/// <param name="sceneIndex">Buildindex of the scene</param>
|
|
public void BackToScene(int sceneIndex)
|
|
{
|
|
previousScene = currentScene;
|
|
while (0 < sceneStack.Count && sceneStack.Peek() != sceneIndex) sceneStack.Pop();
|
|
|
|
if (sceneStack.Count > 0) SceneManager.LoadScene(currentScene = sceneStack.Peek());
|
|
else Application.Quit();
|
|
}
|
|
}
|