dronkowitz

PauseMenu.cs

Aug 11th, 2021 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class PauseMenu : MonoBehaviour
  7. {
  8.     public GameObject pauseMenu;
  9.     public List<AudioSource> pausedSounds;
  10.    
  11.     // Start is called before the first frame update
  12.     void Start()
  13.     {
  14.        
  15.     }
  16.  
  17.     // Update is called once per frame
  18.     void Update()
  19.     {
  20.         if (Input.GetKeyDown(KeyCode.Escape))
  21.         {
  22.             SwitchPause();
  23.         }
  24.     }
  25.  
  26.     public void PauseGame()
  27.     {
  28.         pausedSounds = new List<AudioSource>();
  29.         AudioSource[] allSounds = FindObjectsOfType<AudioSource>();
  30.         foreach(AudioSource source in allSounds)
  31.         {
  32.             if (source.isPlaying)
  33.             {
  34.                 source.Pause();
  35.                 pausedSounds.Add(source);
  36.             }
  37.         }
  38.  
  39.         foreach(TriggerDialog td in FindObjectsOfType<TriggerDialog>())
  40.         {
  41.             td.pause = true;
  42.         }
  43.         pauseMenu.SetActive(true);
  44.         Time.timeScale = 0;
  45.     }
  46.  
  47.     public void UnpauseGame()
  48.     {
  49.         foreach(AudioSource source in pausedSounds)
  50.         {
  51.             source.UnPause();
  52.         }
  53.         foreach (TriggerDialog td in FindObjectsOfType<TriggerDialog>())
  54.         {
  55.             td.pause = false;
  56.         }
  57.         Time.timeScale = 1;
  58.         pauseMenu.SetActive(false);
  59.        
  60.     }
  61.  
  62.     public void SwitchPause()
  63.     {
  64.         if (pauseMenu.activeSelf)
  65.         {
  66.             UnpauseGame();
  67.         }
  68.         else
  69.         {
  70.             PauseGame();
  71.         }
  72.        
  73.     }
  74.  
  75.     public void Restartlevel()
  76.     {
  77.         Scene myscene = SceneManager.GetActiveScene();
  78.         SceneManager.LoadScene(myscene.name);
  79.     }
  80.  
  81.     public void QuitGame()
  82.     {
  83.         Application.Quit();
  84.     }
  85. }
  86.  
Add Comment
Please, Sign In to add comment