UnityGameProjectsCode / NightwatchGame / PauseMenu.cs
PauseMenu.cs
Raw
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class PauseMenu : MonoBehaviour
{
    public GameObject pauseCanvas;
    public PlayerController pControl;
    public AudioMixer masterMix;
    public GameManager gameMgr;

    private void Start()
    {
        pauseCanvas.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape) && pauseCanvas.activeSelf)
        {
            gameMgr.PauseGame(false);
            pauseCanvas.SetActive(false);
            pControl.SetFreezePlayer(false);
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;

            return;
        }

        if (Input.GetKeyDown(KeyCode.Escape) && !pauseCanvas.activeSelf)
        {
            gameMgr.PauseGame(true);
            pauseCanvas.SetActive(true);
            pControl.SetFreezePlayer(true);
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;

            return;
        }
    }

    public void OnReturnButtonPress()
    {
        //close menu, un-pause time, and unlock player
        gameMgr.PauseGame(false);
        pauseCanvas.SetActive(false);
        pControl.SetFreezePlayer(false);
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    public void OnFullScreenToggle(Toggle fullScreenToggle)
    {
        if (fullScreenToggle.isOn)
            Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
        else
            Screen.fullScreenMode = FullScreenMode.Windowed;
    }

    public void OnQuitGamePress()
    {
        Application.Quit();
    }

    public void SetMasterAudioLevel(Slider thisSlider)
    {
        float newVolume = Mathf.Log10(thisSlider.value) * 20;
        masterMix.SetFloat("masterVolume", newVolume);
    }

    public void SetMusicAudioLevel(Slider thisSlider)
    {
        float newVolume = Mathf.Log10(thisSlider.value) * 20;
        masterMix.SetFloat("musicVolume", newVolume);
    }

    public void SetEffectsAudioLevel(Slider thisSlider)
    {
        float newVolume = Mathf.Log10(thisSlider.value) * 20;
        masterMix.SetFloat("effectsVolume", newVolume);
    }
}