100 lines
2.6 KiB
C#
100 lines
2.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
using UnityEngine.InputSystem;
|
|
#endif
|
|
|
|
public class PauseMenu : MonoBehaviour
|
|
{
|
|
[Header("UI")]
|
|
[SerializeField] private CanvasGroup panel;
|
|
[SerializeField] private Button resumeButton;
|
|
//[SerializeField] private Button restartButton;
|
|
[SerializeField] private Button quitButton;
|
|
|
|
//[SerializeField] private KeyCode toggleKey = KeyCode.Escape; // Esc
|
|
|
|
private void Awake()
|
|
{
|
|
if (resumeButton) resumeButton.onClick.AddListener(OnResumeClicked);
|
|
//if (restartButton) restartButton.onClick.AddListener(OnRestartClicked);
|
|
if (quitButton) quitButton.onClick.AddListener(OnQuitClicked);
|
|
|
|
// Изначально скрыто
|
|
SetVisible(false, instant: true);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (GameManager.Instance != null)
|
|
GameManager.Instance.PauseStateChanged += HandlePauseChanged;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (GameManager.Instance != null)
|
|
GameManager.Instance.PauseStateChanged -= HandlePauseChanged;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
if (Keyboard.current != null && Keyboard.current.escapeKey.wasPressedThisFrame)
|
|
GameManager.Instance.TogglePause();
|
|
#else
|
|
if (Input.GetKeyDown(toggleKey))
|
|
GameManager.Instance.TogglePause();
|
|
#endif
|
|
}
|
|
|
|
private void HandlePauseChanged(bool paused)
|
|
{
|
|
SetVisible(paused);
|
|
}
|
|
|
|
private void SetVisible(bool visible, bool instant = false)
|
|
{
|
|
if (!panel) return;
|
|
|
|
panel.interactable = visible;
|
|
panel.blocksRaycasts = visible;
|
|
|
|
if (instant)
|
|
{
|
|
panel.alpha = visible ? 1f : 0f;
|
|
}
|
|
else
|
|
{
|
|
StopAllCoroutines();
|
|
StartCoroutine(Fade(panel, visible ? 1f : 0f, 0.15f));
|
|
}
|
|
}
|
|
|
|
private System.Collections.IEnumerator Fade(CanvasGroup cg, float target, float duration)
|
|
{
|
|
float start = cg.alpha;
|
|
float t = 0f;
|
|
while (t < duration)
|
|
{
|
|
t += Time.unscaledDeltaTime;
|
|
cg.alpha = Mathf.Lerp(start, target, t / duration);
|
|
yield return null;
|
|
}
|
|
cg.alpha = target;
|
|
}
|
|
|
|
public void OnResumeClicked() => GameManager.Instance.SetPause(false);
|
|
|
|
//public void OnRestartClicked()
|
|
//{
|
|
// GameManager.Instance.SetPause(false);
|
|
// GameManager.Instance.ResetLevel();
|
|
//}
|
|
|
|
public void OnQuitClicked()
|
|
{
|
|
GameManager.Instance.SetPause(false);
|
|
GameManager.Instance.LoadScene("MainMenu", false);
|
|
}
|
|
}
|