Advertisement
katubug

pauseHandler

Jan 15th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3.  
  4. public class PauseHandler : MonoBehaviour
  5. {
  6. PlayerInput _playerInput;
  7. private bool _isPausing;
  8. public bool IsPaused;
  9. public GameObject pauseMenu;
  10.  
  11. void Awake()
  12. {
  13. _playerInput = new PlayerInput();
  14. _playerInput.MenuControls.Pause.started += onPause;
  15. _playerInput.MenuControls.Pause.canceled += onPause;
  16. _playerInput.MenuControls.Pause.performed += onPause;
  17. }
  18.  
  19. private void Start()
  20. {
  21. pauseMenu.SetActive(false);
  22. }
  23.  
  24. void onPause(InputAction.CallbackContext context)
  25. {
  26. _isPausing = context.ReadValueAsButton();
  27. Debug.Log("attempting to pause");
  28. }
  29.  
  30. public void HandlePause()
  31. {
  32. if (_isPausing && Time.timeScale > 0)
  33. {
  34. Debug.Log("paused");
  35.  
  36. Time.timeScale = 0;
  37. AudioListener.pause = true;
  38.  
  39. IsPaused = true;
  40.  
  41. pauseMenu.SetActive(true);
  42. }
  43. else if (!_isPausing && Time.timeScale == 0)
  44. {
  45. Debug.Log("unpaused");
  46. Time.timeScale = 1;
  47. AudioListener.pause = false;
  48.  
  49. IsPaused = false;
  50.  
  51. pauseMenu.SetActive(false);
  52. }
  53. }
  54.  
  55. private void Update()
  56. {
  57. HandlePause();
  58. }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement