using System; using NaughtyAttributes; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; using Random = UnityEngine.Random; public class MainMenuManager : MonoBehaviour { public enum MenuMode { MainMenu, PauseMenu, Options, } [SerializeField] [Required] TMP_Text titleLabel; [SerializeField] string[] titleOptions; [SerializeField] [Required] Transform startMenuParent; [SerializeField] [Required] Transform pauseParent; [SerializeField] [Required] Transform optionsParent; [SerializeField] [Required] GameTimer gameTimer; [SerializeField] [Required] EventSystem eventSystem; MenuMode mode; MenuMode? preOptionsMode; void Awake() => SetMenuMode(MenuMode.MainMenu); void Start() { if (titleOptions.Length == 0) { Debug.LogWarning("No titles listed."); return; } if (PlayerPrefs.HasKey(GameTimer.HIGH_SCORE_KEY) && PlayerPrefs.GetFloat(GameTimer.HIGH_SCORE_KEY) > 0f) titleLabel.text = titleOptions[Random.Range(0, titleOptions.Length)]; else titleLabel.text = titleOptions[0]; } public void SetMenuMode(MenuMode mode) { //meh titleLabel.gameObject.SetActive(mode == MenuMode.MainMenu); startMenuParent.gameObject.SetActive(mode == MenuMode.MainMenu); pauseParent.gameObject.SetActive(mode == MenuMode.PauseMenu); optionsParent.gameObject.SetActive(mode == MenuMode.Options); if (mode == MenuMode.Options) { if (this.mode != MenuMode.Options) preOptionsMode = this.mode; } else { preOptionsMode = null; } this.mode = mode; ResetMenuState(); } public void SetOptions() => SetMenuMode(MenuMode.Options); public void ExitOptions() { if (preOptionsMode != null) SetMenuMode(preOptionsMode.Value); } public void ResetMenuState() { GameObject selected = mode switch { MenuMode.MainMenu => startMenuParent.GetChild(0).gameObject, MenuMode.PauseMenu => pauseParent.GetChild(0).gameObject, MenuMode.Options => optionsParent.GetChild(0).gameObject, _ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null) }; eventSystem.firstSelectedGameObject = selected; eventSystem.SetSelectedGameObject(selected); } public void GoToScene(int sceneNb) => SceneManager.LoadScene(sceneNb); public void QuitGame() => Application.Quit(0); public void ResetHighScore() => gameTimer.ResetHighScore(); }