173 lines
5.7 KiB
C#
173 lines
5.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using Input;
|
|
|
|
namespace UI
|
|
{
|
|
public class PauseMenu : MonoBehaviour
|
|
{
|
|
private static PauseMenu _instance;
|
|
private static bool _isQuitting;
|
|
|
|
public static PauseMenu Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null && Application.isPlaying && !_isQuitting)
|
|
{
|
|
_instance = FindAnyObjectByType<PauseMenu>();
|
|
if (_instance == null)
|
|
{
|
|
var go = new GameObject("PauseMenu");
|
|
_instance = go.AddComponent<PauseMenu>();
|
|
// DontDestroyOnLoad(go);
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
[Header("UI References")]
|
|
[SerializeField] private GameObject pauseMenuPanel;
|
|
[SerializeField] private GameObject pauseButton;
|
|
|
|
public event Action OnGamePaused;
|
|
public event Action OnGameResumed;
|
|
|
|
private void Start()
|
|
{
|
|
// Subscribe to scene loaded events
|
|
SceneManagerService.Instance.SceneLoadCompleted += SetPauseMenuByLevel;
|
|
|
|
// Set initial state based on current scene
|
|
SetPauseMenuByLevel(SceneManager.GetActiveScene().name);
|
|
|
|
// Initialize pause menu state
|
|
HidePauseMenu(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
// Unsubscribe when destroyed
|
|
if (SceneManagerService.Instance != null)
|
|
{
|
|
SceneManagerService.Instance.SceneLoadCompleted -= SetPauseMenuByLevel;
|
|
}
|
|
}
|
|
|
|
void OnApplicationQuit()
|
|
{
|
|
_isQuitting = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the pause menu game object active or inactive based on the current level
|
|
/// </summary>
|
|
/// <param name="levelName">The name of the level/scene</param>
|
|
public void SetPauseMenuByLevel(string levelName)
|
|
{
|
|
if (string.IsNullOrEmpty(levelName))
|
|
return;
|
|
|
|
bool isMainMenu = levelName.ToLower().Contains("mainmenu");
|
|
gameObject.SetActive(!isMainMenu);
|
|
|
|
if(!isMainMenu)
|
|
HidePauseMenu(false); // Ensure menu is hidden when switching to a game level
|
|
|
|
Debug.Log($"[PauseMenu] Setting pause menu active: {!isMainMenu} for scene: {levelName}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows the pause menu and hides the pause button. Sets input mode to UI.
|
|
/// </summary>
|
|
public void ShowPauseMenu()
|
|
{
|
|
if (pauseMenuPanel != null)
|
|
pauseMenuPanel.SetActive(true);
|
|
|
|
if (pauseButton != null)
|
|
pauseButton.SetActive(false);
|
|
|
|
// Change input mode to UI when menu is open
|
|
InputManager.Instance.SetInputMode(InputMode.UI);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hides the pause menu and shows the pause button. Sets input mode to Game.
|
|
/// </summary>
|
|
public void HidePauseMenu(bool resetInput = true)
|
|
{
|
|
if (pauseMenuPanel != null)
|
|
pauseMenuPanel.SetActive(false);
|
|
|
|
if (pauseButton != null)
|
|
pauseButton.SetActive(true);
|
|
|
|
// Change input mode back to Game when menu is closed
|
|
if(resetInput)
|
|
InputManager.Instance.SetInputMode(InputMode.Game);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resumes the game by hiding the pause menu.
|
|
/// </summary>
|
|
public void ResumeGame()
|
|
{
|
|
HidePauseMenu();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Exits to the main menu scene.
|
|
/// </summary>
|
|
public async void ExitToMainMenu()
|
|
{
|
|
// Replace with the actual scene name as set in Build Settings
|
|
var progress = new Progress<float>(p => Debug.Log($"Loading progress: {p * 100:F0}%"));
|
|
await SceneManagerService.Instance.SwitchSceneAsync("MainMenu", progress);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Exits the application.
|
|
/// </summary>
|
|
public void ExitGame()
|
|
{
|
|
#if UNITY_EDITOR
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
#else
|
|
Application.Quit();
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads a level based on the selection from a dropdown menu.
|
|
/// Connect this to a Dropdown's onValueChanged event and pass the selected option text.
|
|
/// </summary>
|
|
/// <param name="levelSelection">The selected level name or identifier from the dropdown</param>
|
|
public async void LoadLevel(int levelSelection)
|
|
{
|
|
// Hide the pause menu before loading a new level
|
|
HidePauseMenu();
|
|
|
|
// Replace with the actual scene name as set in Build Settings
|
|
var progress = new Progress<float>(p => Debug.Log($"Loading progress: {p * 100:F0}%"));
|
|
switch (levelSelection)
|
|
{
|
|
case 0:
|
|
await SceneManagerService.Instance.SwitchSceneAsync("MainMenu", progress);
|
|
break;
|
|
case 1:
|
|
await SceneManagerService.Instance.SwitchSceneAsync("AppleHillsOverworld", progress);
|
|
break;
|
|
case 2:
|
|
await SceneManagerService.Instance.SwitchSceneAsync("Quarry", progress);
|
|
break;
|
|
case 3:
|
|
await SceneManagerService.Instance.SwitchSceneAsync("DivingForPictures", progress);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|