added buttons to pause menu
This commit is contained in:
		@@ -2,29 +2,40 @@ using System.Collections;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using UnityEngine;
 | 
			
		||||
using UnityEngine.SceneManagement;
 | 
			
		||||
using UnityEngine.UI;
 | 
			
		||||
 | 
			
		||||
public class SceneController : MonoBehaviour
 | 
			
		||||
{
 | 
			
		||||
    private int currentSceneIndex = 0;
 | 
			
		||||
 | 
			
		||||
    // this object will always exist!
 | 
			
		||||
    void Awake() {
 | 
			
		||||
        // check to see if a state controller already exists
 | 
			
		||||
        if (GameObject.FindGameObjectWithTag("SceneManager") != null) {
 | 
			
		||||
            Destroy(this.gameObject);
 | 
			
		||||
        } else { // if it doesn't, then this is the only one
 | 
			
		||||
            this.gameObject.tag = "SceneManager";
 | 
			
		||||
        }
 | 
			
		||||
        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() {
 | 
			
		||||
    //     currentSceneIndex += 1;
 | 
			
		||||
        
 | 
			
		||||
    //     print("attempting to load scene " + currentSceneIndex);
 | 
			
		||||
    //     if (currentSceneIndex < SceneManager.sceneCount) {
 | 
			
		||||
    //         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
 | 
			
		||||
    //     } else {
 | 
			
		||||
    //         print("no more scenes!");
 | 
			
		||||
    //     }
 | 
			
		||||
    // }
 | 
			
		||||
 | 
			
		||||
    public void NextScene() {
 | 
			
		||||
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void BackToMainMenu() {
 | 
			
		||||
        SceneManager.LoadScene(0); // main menu scene should be 0
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,12 @@ public class StateController : MonoBehaviour {
 | 
			
		||||
    public GameObject pauseMenuCanvas;
 | 
			
		||||
 | 
			
		||||
    void Awake() {
 | 
			
		||||
        // check to see if a state controller already exists
 | 
			
		||||
        if (GameObject.FindGameObjectWithTag("StateController") != null) {
 | 
			
		||||
            Destroy(this.gameObject);
 | 
			
		||||
        } else { // if it doesn't, then this is the only one
 | 
			
		||||
            this.gameObject.tag = "StateController";
 | 
			
		||||
        }
 | 
			
		||||
        DontDestroyOnLoad(this.gameObject);
 | 
			
		||||
        SceneManager.sceneLoaded += OnSceneLoaded;
 | 
			
		||||
    }
 | 
			
		||||
@@ -30,9 +36,15 @@ public class StateController : MonoBehaviour {
 | 
			
		||||
 | 
			
		||||
        pauseMenuCanvas = GameObject.Find("PauseMenuCanvas");
 | 
			
		||||
        if (pauseMenuCanvas != null) {
 | 
			
		||||
            Button resumeButton = GameObject.Find("ResumeButton").GetComponent<Button>();
 | 
			
		||||
            resumeButton.onClick.AddListener(Unpause);
 | 
			
		||||
            TogglePauseMenu(false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (isPaused) {
 | 
			
		||||
            Unpause();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void OnPause() {
 | 
			
		||||
@@ -46,6 +58,12 @@ public class StateController : MonoBehaviour {
 | 
			
		||||
        isPaused = !isPaused;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void Unpause() {
 | 
			
		||||
        Time.timeScale = 1;
 | 
			
		||||
        TogglePauseMenu(false);
 | 
			
		||||
        isPaused = !isPaused;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void TogglePauseMenu(bool showPauseMenu) {
 | 
			
		||||
        pauseMenuCanvas.SetActive(showPauseMenu);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user