Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / PauseMenuBehaviour.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using TMPro;
  7. public class PauseMenuBehaviour : MonoBehaviour
  8. {
  9. float timeScale;
  10. [SerializeField] private Texture2D cursorTexture;
  11. private Vector2 cursorHotspot;
  12. //[SerializeField] private AudioListener audioListener;
  13. public static bool isPaused { get; private set; } = false;
  14. public GameObject menu;
  15. private void Start()
  16. {
  17. Cursor.visible = false;
  18. cursorHotspot = Vector2.zero;
  19. Cursor.SetCursor(cursorTexture, cursorHotspot, CursorMode.ForceSoftware);
  20. isPaused = false;
  21. if (menu != null)
  22. {
  23. isPaused = menu.activeSelf;
  24. }
  25. Cursor.lockState = CursorLockMode.Locked;
  26. }
  27. public void Resume()
  28. {
  29. if (isPaused)
  30. {
  31. Time.timeScale = timeScale;
  32. isPaused = false;
  33. Cursor.visible = false;
  34. Cursor.lockState = CursorLockMode.Locked;
  35. }
  36. }
  37. public void Pause()
  38. {
  39. if (!isPaused) {
  40. timeScale = Time.timeScale;
  41. Time.timeScale = 0;
  42. isPaused = true;
  43. Cursor.visible = true;
  44. Cursor.lockState = CursorLockMode.None;
  45. }
  46. }
  47. public void ReturnToMainMenu()
  48. {
  49. Resume();
  50. Cursor.lockState = CursorLockMode.None;
  51. SceneManager.LoadScene(1);
  52. }
  53. private void Update()
  54. {
  55. if (Input.GetKeyDown(KeyCode.Escape))
  56. {
  57. if (isPaused)
  58. {
  59. Resume();
  60. }
  61. else
  62. {
  63. Pause();
  64. }
  65. }
  66. if (menu != null)
  67. {
  68. menu.SetActive(isPaused);
  69. }
  70. }
  71. //Button UI Behaviour
  72. public void ButtonOverlay(TMP_Text buttontext)
  73. {
  74. buttontext.color = new Color32(255, 212, 73, 255);
  75. }
  76. public void ButtonExitOverlay(TMP_Text buttontext)
  77. {
  78. buttontext.color = Color.black;
  79. }
  80. }