Files
zeling_v2/Assets/_Game/Scripts/UI/Menus/DeathScreenController.cs
2026-05-25 11:54:37 +08:00

71 lines
2.2 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
using BaseGames.Core.Events;
namespace BaseGames.UI.Menus
{
public class DeathScreenController : MonoBehaviour
{
[SerializeField] private TMP_Text _deathMessage;
[SerializeField] private Button _btnRespawn;
[Header("时序")]
[Tooltip("死亡画面出现前的缓冲延迟(秒)。调整此值可匹配死亡动画时长。")]
[SerializeField] private float _showDelay = 1.5f;
[Tooltip("死亡文字默认显示内容,会被本地化系统覆盖(如果已配置)。")]
[SerializeField] private string _defaultDeathText = "決死";
[Header("Event Channels")]
[SerializeField] private VoidEventChannelSO _onDeathScreenConfirmed;
private WaitForSecondsRealtime _showDelayWait;
private void Awake()
{
_showDelayWait = new WaitForSecondsRealtime(_showDelay);
}
private void OnEnable()
{
// 死亡界面由 UIManager 在游戏状态变为 Dead 时通过 SetActive(true) 激活。
// _onPlayerDied 事件此时已经触发完毕,订阅它不会收到回调。
// 直接在 OnEnable 启动延迟显示协程即可保证 1.5s 缓冲。
StartCoroutine(ShowAfterDelay(_showDelay));
}
private void OnDisable()
{
StopAllCoroutines();
}
private IEnumerator ShowAfterDelay(float delay)
{
yield return _showDelayWait;
Show();
}
private void Show()
{
if (_deathMessage != null) _deathMessage.text = _defaultDeathText;
if (_btnRespawn != null)
{
_btnRespawn.onClick.RemoveAllListeners();
_btnRespawn.onClick.AddListener(Confirm);
}
// 手柄导航:死亡界面显示后将焦点置于复活按钮
EventSystem.current?.SetSelectedGameObject(_btnRespawn?.gameObject);
}
private void Confirm()
{
gameObject.SetActive(false);
_onDeathScreenConfirmed?.Raise();
}
}
}