Files
2025TapTapGameJam/Assets/Script/Gameplay/UI/PlayerHpViewer.cs

37 lines
891 B
C#
Raw Normal View History

2025-10-15 21:31:13 +08:00
using System;
using Core;
using Gameplay.Player;
using TMPro;
2025-10-15 21:31:13 +08:00
using UnityEngine;
using UnityEngine.UI;
namespace UI
{
public class PlayerHpViewer : UIBase
{
private PlayerController _playerController;
public TMP_Text hpText;
2025-10-15 21:31:13 +08:00
private void Awake()
2025-10-15 21:31:13 +08:00
{
ControllerLocator.Instance.TryGetWait<PlayerController>(OnGet);
2025-10-15 21:31:13 +08:00
}
private void OnGet(PlayerController playerController)
2025-10-15 21:31:13 +08:00
{
_playerController = playerController;
_playerController.OnHealthChanged += UpdateHpText;
UpdateHpText(_playerController.CurrentHealth);
2025-10-15 21:31:13 +08:00
}
private void UpdateHpText(int currentHealth)
2025-10-15 21:31:13 +08:00
{
hpText.text = "HP: " + currentHealth.ToString();
}
private void OnDestroy()
{
_playerController.OnHealthChanged -= UpdateHpText;
2025-10-15 21:31:13 +08:00
}
}
}