Files
zeling_v2/Assets/_Game/Scripts/UI/MenuPauseScope.cs
2026-06-08 11:26:17 +08:00

44 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using BaseGames.Input;
namespace BaseGames.UI
{
/// <summary>
/// 挂在"游戏内全屏菜单"根节点(如 InventoryHubRoot
/// 打开OnEnable时冻结游戏Time.timeScale=0并切到 UI 输入关闭OnDisable时恢复时间并切回 Gameplay 输入。
///
/// 解决的 bug游戏内菜单统一背包屏打开时本不改 GameState导致 Gameplay 输入仍激活、
/// 按 Esc 触发的是"暂停"而非"关闭菜单",且关闭后状态错乱使 Esc 不再暂停。
/// 切到 UI 输入后:菜单内 Esc=关闭UINavigator Cancel关闭后 Esc=暂停Gameplay
/// 同时冻结时间符合"打开背包暂停游戏"的常规手感。
///
/// 仅作用于本菜单的开/关与暂停菜单PausedState的真实暂停相互独立、互不冲突。
/// </summary>
[DisallowMultipleComponent]
public class MenuPauseScope : MonoBehaviour
{
[SerializeField] private InputReaderSO _inputReader;
[Tooltip("打开时是否冻结游戏时间Time.timeScale=0。菜单动画须用 unscaledDeltaTime。")]
[SerializeField] private bool _freezeTime = true;
private float _prevTimeScale = 1f;
private void OnEnable()
{
if (_freezeTime)
{
_prevTimeScale = Time.timeScale;
Time.timeScale = 0f;
}
_inputReader?.EnableUIInput();
}
private void OnDisable()
{
if (_freezeTime)
Time.timeScale = _prevTimeScale; // 恢复打开前的时间倍率(若打开前已是 0 则维持暂停,正确)
_inputReader?.EnableGameplayInput();
}
}
}