ofb/Assets/Scripts/SceneController.cs
Nicholas Novak 5a7ce8fb2b change: Switched the state controller and scene controller to a singleton class
This doesnt' change any of the logic, but simplifies a lot of the main
game loop code.

Many things still rely on the singleton class, but shouldn't so this
will be fixed in a later commit
2023-05-04 11:25:58 -07:00

54 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneController : MonoBehaviour
{
public static SceneController Instance = null;
// this object will always exist!
void Awake()
{
if (Instance == null)
{
Instance = this;
}
// Make this object stay around when switching scenes
DontDestroyOnLoad(this.gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
GameObject pauseMenu = GameObject.Find("PauseMenuCanvas");
if (pauseMenu != null)
{
Button quitButton = GameObject.Find("QuitButton").GetComponent<Button>();
quitButton.onClick.AddListener(BackToMainMenu);
}
if (scene.buildIndex == 0)
{ // if this is the menu
GameObject.Find("NewGameButton").GetComponent<Button>().onClick.AddListener(NextScene);
}
}
public void NextScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void BackToMainMenu()
{
SceneManager.LoadScene(0); // main menu scene should be 0
}
public void LoadChosenScene(int index)
{
SceneManager.LoadScene(index);
}
}