Advertisement
EmmyDev

Pause Menu System

Dec 29th, 2022 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.Audio;
  7.  
  8.  
  9. public class MenuController : MonoBehaviour
  10. {
  11.     public AudioMixer mixer;
  12.  
  13.     public bool inRegMenu = false;
  14.     public bool paused = false;
  15.     public GameObject playerCam;
  16.     public GameObject uiStuff;
  17.     public GameObject pauseMenuObj;
  18.     //public AudioListener listener;
  19.    
  20.     public GameObject firstSettings;
  21.     public bool settingsOpen;
  22.     public GameObject settingsMenu;
  23.     public Slider sensSlider;
  24.  
  25.     //activate once shit
  26.     public bool activateOnce = true;
  27.  
  28.     public Slider volumeSlider;
  29.     public void QuitToMenu(){
  30.        
  31.         Cursor.visible = true;
  32.         SceneManager.LoadScene("MenuStuff", LoadSceneMode.Single);
  33.        
  34.        
  35.     }
  36.     public void ExitGame(){
  37.         print("bye bye");
  38.         Application.Quit();
  39.     }
  40.     public void UnPause(){
  41.         paused = false;
  42.     }
  43.     public void Settings(){
  44.  
  45.         volumeSlider.value = PlayerPrefs.GetFloat("SavedVolume");
  46.         sensSlider.value = PlayerPrefs.GetFloat("SavedSensitivity");
  47.         settingsMenu.SetActive(true);
  48.         GameObject myEventSystem = GameObject.Find("EventSystem");
  49.         myEventSystem.GetComponent<UnityEngine.EventSystems.EventSystem>().SetSelectedGameObject(null);
  50.         settingsOpen = true;
  51.     }
  52.     public void SettingsOff(){
  53.        
  54.         PlayerPrefs.SetFloat("SavedVolume", volumeSlider.value);
  55.         PlayerPrefs.SetFloat("SavedSensitivity", sensSlider.value);
  56.         settingsOpen = false;
  57.         settingsMenu.SetActive(false);
  58.         GameObject myEventSystem = GameObject.Find("EventSystem");
  59.         myEventSystem.GetComponent<UnityEngine.EventSystems.EventSystem>().SetSelectedGameObject(null);
  60.        
  61.     }
  62.    
  63.     //every frame updates or smth :3
  64.     void Update(){
  65.        
  66.         if(inRegMenu == true){
  67.             Cursor.lockState = CursorLockMode.None;
  68.             Cursor.visible = true;
  69.             Time.timeScale = 1;
  70.             //pauseMenuObj.SetActive(true);
  71.             //playerCam.SetActive(true);
  72.             //uiStuff.SetActive(true);
  73.         }
  74.         mixer.SetFloat("VolumeMain",Mathf.Log10(PlayerPrefs.GetFloat("SavedVolume"))* 20f);
  75.         if(settingsOpen == true){
  76.             firstSettings.SetActive(false);
  77.         }
  78.         else if(settingsOpen == false)
  79.         {
  80.             firstSettings.SetActive(true);
  81.         }
  82.  
  83.         if(Input.GetKeyDown(KeyCode.Escape) && settingsOpen == false && inRegMenu == false){
  84.             paused = !paused;
  85.         }
  86.         else if(Input.GetKeyDown(KeyCode.Escape) && settingsOpen == true){
  87.             SettingsOff();
  88.            
  89.            
  90.         }
  91.         if(paused == true && playerCam != null){
  92.            
  93.             activateOnce = true;
  94.             if(SceneManager.GetActiveScene().name != "MenuStuff")
  95.             {
  96.                 AudioListener.pause = true;
  97.             }
  98.            
  99.             Cursor.visible = true;
  100.             Cursor.lockState = CursorLockMode.None;
  101.             Time.timeScale = 0;
  102.             pauseMenuObj.SetActive(true);
  103.             playerCam.SetActive(false);
  104.             uiStuff.SetActive(false);
  105.         }
  106.         else if(playerCam != null)
  107.         {
  108.             Cursor.lockState = CursorLockMode.Locked;
  109.             Cursor.visible = false;
  110.             Time.timeScale = 1;
  111.             pauseMenuObj.SetActive(false);
  112.             if(activateOnce == true){
  113.                 AudioListener.pause = false;
  114.                 playerCam.SetActive(true);
  115.                 uiStuff.SetActive(true);
  116.                 activateOnce = false;
  117.             }
  118.            
  119.            
  120.            
  121.         }
  122.         if(SceneManager.GetActiveScene().name == "MenuStuff"){
  123.             UnPause();
  124.         }
  125.        
  126.     }
  127.    
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement