157 lines
5.0 KiB
C#
157 lines
5.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.InputSystem.UI;
|
|
|
|
// 遊戲中按 Esc 暫停 : 時間停止、放開滑鼠,選「繼續」或「回到主畫面」
|
|
// 切出遊戲視窗 (Alt+Tab 等) 時也會自動暫停
|
|
// 暫停期間記錄器不記錄,回合照樣有效
|
|
// v1.2.0 : 暫停狀態在每一幀開頭鎖存,同一幀裡所有腳本 (視角、輔助、感知器、武器、記錄器) 看到的都一樣
|
|
// 幀中途發生的暫停 / 繼續 (Esc、按鈕、切出視窗) 從下一幀才生效
|
|
// 執行順序 -100 : 在 RawMouseInput (-10000) 之後、AimAssist (-10) / PlayerLook (0) / RoundRecorder (100) 之前
|
|
[DefaultExecutionOrder(-100)]
|
|
public class PauseMenu : MonoBehaviour
|
|
{
|
|
[Header("UI 參照")]
|
|
[Tooltip("暫停視窗 (Panel),平常隱藏")]
|
|
public GameObject pausePanel;
|
|
|
|
[Tooltip("繼續 Button")]
|
|
public Button resumeButton;
|
|
|
|
[Tooltip("回到主畫面 Button")]
|
|
public Button menuButton;
|
|
|
|
[Header("場景")]
|
|
public string startSceneName = "StartUI";
|
|
|
|
// 給其他腳本判斷 (視角、輔助、感知器、武器、記錄器) : 「這一幀是否暫停」,整幀內不會變
|
|
public static bool IsPaused { get; private set; }
|
|
|
|
// 開始暫停的時間 (跟記錄器同一條時間軸) : 切出視窗時遊戲會停住,記錄器要靠這個知道真正的暫停時間
|
|
public static double PauseStartTime { get; private set; }
|
|
|
|
// 即時的暫停狀態 : 下一幀開頭才複製到 IsPaused
|
|
private static bool paused;
|
|
|
|
// 提示視窗 (例如熱身 FPS 提示) 開著 : 也算暫停,但不顯示暫停視窗,Esc 不作用
|
|
private static bool inPrompt;
|
|
|
|
private static PauseMenu instance;
|
|
|
|
void Awake()
|
|
{
|
|
instance = this;
|
|
paused = false;
|
|
inPrompt = false;
|
|
IsPaused = false;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
EnsureButtonsClickable();
|
|
|
|
if (pausePanel != null) pausePanel.SetActive(false);
|
|
if (resumeButton != null) resumeButton.onClick.AddListener(Resume);
|
|
if (menuButton != null) menuButton.onClick.AddListener(BackToMenu);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// 先鎖存這一幀的狀態,之後的 Esc 從下一幀才生效
|
|
IsPaused = paused;
|
|
|
|
if (inPrompt) return;
|
|
if (Keyboard.current != null && Keyboard.current.escapeKey.wasPressedThisFrame)
|
|
{
|
|
if (paused) Resume();
|
|
else Pause();
|
|
}
|
|
}
|
|
|
|
// 遊戲視窗失去焦點 (Alt+Tab、點到別的視窗) : 自動暫停,回來時要按「繼續」
|
|
void OnApplicationFocus(bool hasFocus)
|
|
{
|
|
if (!hasFocus && !paused) Pause();
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
// 離開場景時一定要恢復,不然下一個場景會停住
|
|
if (instance == this) instance = null;
|
|
paused = false;
|
|
inPrompt = false;
|
|
IsPaused = false;
|
|
Time.timeScale = 1f;
|
|
}
|
|
|
|
// ===== 提示視窗用 (FpsWarning) =====
|
|
|
|
// 暫停但不顯示暫停視窗 ; 場景裡沒有 PauseMenu 時回傳 false
|
|
public static bool PauseForPrompt()
|
|
{
|
|
if (instance == null) return false;
|
|
inPrompt = true;
|
|
if (!paused) instance.Pause();
|
|
if (instance.pausePanel != null) instance.pausePanel.SetActive(false);
|
|
return true;
|
|
}
|
|
|
|
public static void ResumeFromPrompt()
|
|
{
|
|
if (instance == null) return;
|
|
inPrompt = false;
|
|
instance.Resume();
|
|
}
|
|
|
|
// 按鈕要能點,需要 : 場景裡有 EventSystem、Canvas 上有 Graphic Raycaster
|
|
// 地圖場景不一定有 (例如沙漠是從 Demo 場景改的),缺的話這裡自動補上
|
|
void EnsureButtonsClickable()
|
|
{
|
|
if (EventSystem.current == null && FindAnyObjectByType<EventSystem>() == null)
|
|
{
|
|
GameObject es = new GameObject("EventSystem");
|
|
es.AddComponent<EventSystem>();
|
|
es.AddComponent<InputSystemUIInputModule>();
|
|
}
|
|
|
|
Canvas canvas = pausePanel != null ? pausePanel.GetComponentInParent<Canvas>(true) : null;
|
|
if (canvas != null && canvas.GetComponent<GraphicRaycaster>() == null)
|
|
{
|
|
canvas.gameObject.AddComponent<GraphicRaycaster>();
|
|
}
|
|
}
|
|
|
|
void Pause()
|
|
{
|
|
paused = true;
|
|
PauseStartTime = Time.realtimeSinceStartupAsDouble;
|
|
Time.timeScale = 0f;
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
if (pausePanel != null) pausePanel.SetActive(true);
|
|
}
|
|
|
|
void Resume()
|
|
{
|
|
paused = false;
|
|
Time.timeScale = 1f;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
if (pausePanel != null) pausePanel.SetActive(false);
|
|
}
|
|
|
|
void BackToMenu()
|
|
{
|
|
paused = false;
|
|
inPrompt = false;
|
|
IsPaused = false;
|
|
Time.timeScale = 1f;
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
SceneManager.LoadScene(startSceneName);
|
|
}
|
|
}
|