1.2.0
This commit is contained in:
1 parent
c3bb8d1a9e
commit
fd0312270b
26 files changed
+3310
-87
No files matched your search
@@ -8,6 +8,10 @@ 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 參照")]
|
||||
@@ -23,12 +27,28 @@ public class PauseMenu : MonoBehaviour
|
||||
[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();
|
||||
@@ -40,9 +60,13 @@ public class PauseMenu : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
// 先鎖存這一幀的狀態,之後的 Esc 從下一幀才生效
|
||||
IsPaused = paused;
|
||||
|
||||
if (inPrompt) return;
|
||||
if (Keyboard.current != null && Keyboard.current.escapeKey.wasPressedThisFrame)
|
||||
{
|
||||
if (IsPaused) Resume();
|
||||
if (paused) Resume();
|
||||
else Pause();
|
||||
}
|
||||
}
|
||||
@@ -50,16 +74,38 @@ public class PauseMenu : MonoBehaviour
|
||||
// 遊戲視窗失去焦點 (Alt+Tab、點到別的視窗) : 自動暫停,回來時要按「繼續」
|
||||
void OnApplicationFocus(bool hasFocus)
|
||||
{
|
||||
if (!hasFocus && !IsPaused) Pause();
|
||||
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()
|
||||
@@ -80,7 +126,7 @@ public class PauseMenu : MonoBehaviour
|
||||
|
||||
void Pause()
|
||||
{
|
||||
IsPaused = true;
|
||||
paused = true;
|
||||
PauseStartTime = Time.realtimeSinceStartupAsDouble;
|
||||
Time.timeScale = 0f;
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
@@ -90,7 +136,7 @@ public class PauseMenu : MonoBehaviour
|
||||
|
||||
void Resume()
|
||||
{
|
||||
IsPaused = false;
|
||||
paused = false;
|
||||
Time.timeScale = 1f;
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
@@ -99,6 +145,8 @@ public class PauseMenu : MonoBehaviour
|
||||
|
||||
void BackToMenu()
|
||||
{
|
||||
paused = false;
|
||||
inPrompt = false;
|
||||
IsPaused = false;
|
||||
Time.timeScale = 1f;
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
|
||||
Reference in new issue
Block a user