53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
|
using UnityEngine;
|
|||
|
using System;
|
|||
|
using Core;
|
|||
|
|
|||
|
namespace Script.Gameplay.Global
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 全局时间暂停管理器。
|
|||
|
/// 单例,挂载到场景里任意常驻 GameObject(如 Managers)。
|
|||
|
/// </summary>
|
|||
|
public class TimePauseManager : MonoSingleton<TimePauseManager>
|
|||
|
{
|
|||
|
|
|||
|
/// <summary> 当前是否处于暂停状态 </summary>
|
|||
|
public bool IsPaused { get; private set; }
|
|||
|
|
|||
|
/// <summary> 暂停/恢复时触发,参数 true=暂停,false=恢复 </summary>
|
|||
|
public event Action<bool> OnPauseStateChanged;
|
|||
|
|
|||
|
[Header("调试按钮")]
|
|||
|
[SerializeField] private bool _pauseToggle;
|
|||
|
|
|||
|
private void OnValidate()
|
|||
|
{
|
|||
|
// Inspector 里打钩立即生效(仅 Editor)
|
|||
|
if (Application.isPlaying && Instance == this)
|
|||
|
SetPaused(_pauseToggle);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 外部调用:设置暂停状态
|
|||
|
/// </summary>
|
|||
|
public void SetPaused(bool pause)
|
|||
|
{
|
|||
|
if (IsPaused == pause) return;
|
|||
|
|
|||
|
IsPaused = pause;
|
|||
|
Time.timeScale = pause ? 0f : 1f;
|
|||
|
OnPauseStateChanged?.Invoke(pause);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 快捷暂停
|
|||
|
/// </summary>
|
|||
|
public static void Pause() => Instance.SetPaused(true);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 快捷恢复
|
|||
|
/// </summary>
|
|||
|
public static void Resume() => Instance.SetPaused(false);
|
|||
|
}
|
|||
|
}
|