fgm24/Assets/Scripts/UI/PauseMenu.cs

103 lines
2.2 KiB
C#
Raw Normal View History

2024-02-04 10:24:51 +01:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
2024-02-04 11:39:08 +01:00
using UnityEngine.SceneManagement;
2024-02-04 10:24:51 +01:00
public class PauseMenu : MonoBehaviour
{
public GameObject pauseMenuUI;
Gamepad pad;
// Variable to keep track of the game's pause state
private bool isPaused = false;
private void Start()
{
if (pauseMenuUI.activeSelf)
pauseMenuUI.SetActive(false);
}
void Update()
{
2024-02-04 10:29:51 +01:00
//for (int i = 0; i < Gamepad.all.Count; i++)
2024-02-04 11:52:16 +01:00
// if(Gamepad.all[i].selectButton)
2024-02-04 10:24:51 +01:00
2024-02-04 11:52:16 +01:00
foreach (var controller in Gamepad.all)
{
2024-02-04 11:59:49 +01:00
if (controller.startButton.wasReleasedThisFrame)
2024-02-04 11:52:16 +01:00
{
if (isPaused)
{
ResumeGame();
}
else
{
PauseGame();
}
}
if (controller.crossButton.IsPressed())
{
ResumeGame();
}
if (controller.circleButton.IsPressed())
{
QuitGame();
}
if (controller.squareButton.IsPressed())
{
MainMenu();
}
}
2024-02-04 10:24:51 +01:00
// Check if the Escape key is pressed
2024-02-04 10:29:51 +01:00
if (Input.GetKeyDown(KeyCode.Escape))
2024-02-04 10:24:51 +01:00
{
if (isPaused)
{
ResumeGame();
}
else
{
PauseGame();
}
}
}
// Method to resume the game
public void ResumeGame()
{
// Hide the pause menu
pauseMenuUI.SetActive(false);
// Set the time scale to 1 to resume normal gameplay
Time.timeScale = 1f;
// Update the pause state
isPaused = false;
}
// Method to pause the game
public void PauseGame()
{
// Show the pause menu
pauseMenuUI.SetActive(true);
// Set the time scale to 0 to pause the game
Time.timeScale = 0f;
// Update the pause state
isPaused = true;
}
2024-02-04 11:39:08 +01:00
public void MainMenu()
{
SceneManager.LoadScene("MainMenu");
}
public void QuitGame()
{
Application.Quit();
}
2024-02-04 10:24:51 +01:00
}