PauseMenu | FEP

PHOTO EMBED

Thu May 12 2022 17:30:54 GMT+0000 (Coordinated Universal Time)

Saved by @BiscuitTinx #c#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PauseMenu : MonoBehaviour
{
    public static bool GameIsPaused = false;

    public GameObject pauseMenuUi;


    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (GameIsPaused)
            {
                Resume();
                AudioPlay();
            }
            else
            {
                Pause();
                AudioMute();
            }
        }
    }

    public void Resume()
    {
        // Gets rid of the cursor when clicking resume
        Cursor.lockState = CursorLockMode.Locked;

        pauseMenuUi.SetActive(false);
        // sets speed of the game back to normal
        Time.timeScale = 1f;
        GameIsPaused = false;
    }

    public void Pause()
    {
        // cursor reappears and can interact with the UI of the pause menu
        Cursor.lockState = CursorLockMode.Confined;

        pauseMenuUi.SetActive(true);
        // stops the game from playing and freezes time
        Time.timeScale = 0f;
        GameIsPaused = true;
    }

    public AudioListener audioListener;

    public void AudioMute()
    {
        // Stops audio
        AudioListener.pause = true;
    }

    public void AudioPlay()
    {
        // Plays audio
        AudioListener.pause = false;
    }

    public void LoadMenu()
    {
        // unfreezes time so you can access the UIs in the main menu
        Time.timeScale = 1f;
        SceneManager.LoadScene("MainMenu");
    }

    public void QuitGame()
    {
        // closes the application when fully built
        Debug.Log("Quitting Game");
        Application.Quit();
    }
}
content_copyCOPY