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(); if (_instance == null) { var go = new GameObject("PauseMenu"); _instance = go.AddComponent(); // 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; } /// /// Sets the pause menu game object active or inactive based on the current level /// /// The name of the level/scene 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}"); } /// /// Shows the pause menu and hides the pause button. Sets input mode to UI. /// 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); } /// /// Hides the pause menu and shows the pause button. Sets input mode to Game. /// 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); } /// /// Resumes the game by hiding the pause menu. /// public void ResumeGame() { HidePauseMenu(); } /// /// Exits to the main menu scene. /// public async void ExitToMainMenu() { // Replace with the actual scene name as set in Build Settings var progress = new Progress(p => Debug.Log($"Loading progress: {p * 100:F0}%")); await SceneManagerService.Instance.SwitchSceneAsync("MainMenu", progress); } /// /// Exits the application. /// public void ExitGame() { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif } /// /// 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. /// /// The selected level name or identifier from the dropdown 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(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; } } } }