142 lines
3.9 KiB
C#
142 lines
3.9 KiB
C#
using UnityEngine;
|
||
using UnityEngine.InputSystem;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine.UI;
|
||
|
||
public class MainMenuController : MonoBehaviour
|
||
{
|
||
[Header("Root UI")]
|
||
[SerializeField] private CanvasGroup rootCanvas;
|
||
[SerializeField] private GameObject mainPanel;
|
||
[SerializeField] private GameObject optionsPanel;
|
||
|
||
[Header("Input")]
|
||
[SerializeField] private InputActionReference openMenuAction;
|
||
|
||
[Header("Restart")]
|
||
[SerializeField] private int restartSceneIndex = 0;
|
||
|
||
[Header("Option Toggles (optional)")]
|
||
[SerializeField] private Toggle sfxToggle;
|
||
[SerializeField] private Toggle vfxToggle;
|
||
[SerializeField] private Toggle musicToggle;
|
||
[SerializeField] private Toggle scoreToggle;
|
||
|
||
private bool isOpen;
|
||
|
||
private void Awake()
|
||
{
|
||
// IMPORTANT: Object stays ACTIVE in hierarchy.
|
||
// We only hide visually here.
|
||
if (rootCanvas != null)
|
||
{
|
||
rootCanvas.alpha = 0f;
|
||
rootCanvas.interactable = false;
|
||
rootCanvas.blocksRaycasts = false;
|
||
}
|
||
|
||
if (mainPanel) mainPanel.SetActive(true);
|
||
if (optionsPanel) optionsPanel.SetActive(false);
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
// keep internal state in sync
|
||
if (rootCanvas != null)
|
||
isOpen = rootCanvas.alpha >= 0.95f;
|
||
|
||
var s = GameSettingsManager.Instance;
|
||
if (s != null)
|
||
{
|
||
if (sfxToggle) sfxToggle.isOn = s.SfxEnabled;
|
||
if (vfxToggle) vfxToggle.isOn = s.VfxEnabled;
|
||
if (musicToggle) musicToggle.isOn = s.MusicEnabled;
|
||
if (scoreToggle) scoreToggle.isOn = s.ScoreEnabled;
|
||
}
|
||
}
|
||
|
||
private void OnEnable()
|
||
{
|
||
if (openMenuAction != null)
|
||
{
|
||
var a = openMenuAction.action;
|
||
a.actionMap?.Enable(); // ensure map is on
|
||
a.started += OnOpenMenu; // toggle on key down
|
||
a.Enable();
|
||
}
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
if (openMenuAction != null)
|
||
{
|
||
var a = openMenuAction.action;
|
||
a.started -= OnOpenMenu; // match subscription
|
||
a.Disable();
|
||
}
|
||
}
|
||
|
||
private void OnOpenMenu(InputAction.CallbackContext ctx)
|
||
{
|
||
if (isOpen) CloseMenu();
|
||
else OpenMenu();
|
||
}
|
||
|
||
public void OpenMenu()
|
||
{
|
||
isOpen = true;
|
||
if (rootCanvas != null)
|
||
{
|
||
rootCanvas.alpha = 1f;
|
||
rootCanvas.interactable = true;
|
||
rootCanvas.blocksRaycasts = true;
|
||
}
|
||
if (mainPanel) mainPanel.SetActive(true);
|
||
if (optionsPanel) optionsPanel.SetActive(false);
|
||
}
|
||
|
||
public void CloseMenu()
|
||
{
|
||
isOpen = false;
|
||
if (rootCanvas != null)
|
||
{
|
||
rootCanvas.alpha = 0f;
|
||
rootCanvas.interactable = false;
|
||
rootCanvas.blocksRaycasts = false;
|
||
}
|
||
}
|
||
|
||
public void OnOptionsPressed()
|
||
{
|
||
if (mainPanel) mainPanel.SetActive(false);
|
||
if (optionsPanel) optionsPanel.SetActive(true);
|
||
}
|
||
|
||
public void OnRestartPressed()
|
||
{
|
||
// if (Time.timeScale == 0f) Time.timeScale = 1f;
|
||
SceneManager.LoadScene(restartSceneIndex);
|
||
}
|
||
|
||
public void OnQuitPressed()
|
||
{
|
||
#if UNITY_EDITOR
|
||
UnityEditor.EditorApplication.isPlaying = false;
|
||
#else
|
||
Application.Quit();
|
||
#endif
|
||
}
|
||
|
||
public void OnBackFromOptions()
|
||
{
|
||
if (mainPanel) mainPanel.SetActive(true);
|
||
if (optionsPanel) optionsPanel.SetActive(false);
|
||
}
|
||
|
||
// Toggles<65>
|
||
public void OnSfxToggleChanged(bool v) { GameSettingsManager.Instance?.SetSfxEnabled(v); }
|
||
public void OnVfxToggleChanged(bool v) { GameSettingsManager.Instance?.SetVfxEnabled(v); }
|
||
public void OnMusicToggleChanged(bool v) { GameSettingsManager.Instance?.SetMusicEnabled(v); }
|
||
public void OnScoreToggleChanged(bool v) { GameSettingsManager.Instance?.SetScoreEnabled(v); }
|
||
}
|