ofb/Assets/Scripts/SceneController.cs

67 lines
1.7 KiB
C#
Raw Normal View History

2023-04-23 22:18:37 -07:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
2023-04-25 15:38:12 -07:00
using UnityEngine.UI;
2023-04-23 22:18:37 -07:00
public class SceneController : MonoBehaviour
{
public static SceneController Instance = null;
2023-04-23 22:18:37 -07:00
// this object will always exist!
void Awake()
{
if (Instance == null)
{
Instance = this;
2023-04-25 15:38:12 -07:00
}
else
{
Destroy(this.gameObject);
return;
}
// Make this object stay around when switching scenes
2023-04-23 22:18:37 -07:00
DontDestroyOnLoad(this.gameObject);
2023-04-25 15:38:12 -07:00
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
2023-04-25 15:38:12 -07:00
GameObject pauseMenu = GameObject.Find("PauseMenuCanvas");
if (scene.buildIndex == 0)
{ // if this is the menu
2023-04-25 15:38:12 -07:00
GameObject.Find("NewGameButton").GetComponent<Button>().onClick.AddListener(NextScene);
}
// if this is the last scene
if (scene.buildIndex == (SceneManager.sceneCountInBuildSettings - 1)) {
GameObject backToMenuButton = GameObject.Find("BackToMenuButton");
if (backToMenuButton != null) {
backToMenuButton.GetComponent<Button>().onClick.AddListener(delegate{LoadChosenScene(0);});
backToMenuButton.SetActive(false);
}
}
2023-04-23 22:18:37 -07:00
}
public void NextScene()
{
2023-04-23 22:18:37 -07:00
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void BackToMainMenu()
{
2023-04-25 15:38:12 -07:00
SceneManager.LoadScene(0); // main menu scene should be 0
}
public void LoadChosenScene(int index)
{
SceneManager.LoadScene(index);
}
public void LoadSceneByName(string name) {
SceneManager.LoadScene(name);
}
2023-04-23 22:18:37 -07:00
}