51 lines
1.9 KiB
C#
51 lines
1.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro; // ⭐ TextMeshPro 사용을 위해 필수!
|
|
|
|
public class HPUibar : MonoBehaviour
|
|
{
|
|
[SerializeField] private MonoBehaviour healthSource;
|
|
[SerializeField] private Slider hpSlider;
|
|
[SerializeField] private TextMeshProUGUI hpText; // ⭐ 유저님이 만든 그 텍스트를 여기에 연결하세요!
|
|
|
|
private void Start()
|
|
{
|
|
// 1. 플레이어, 더미, 몬스터, 적 체력 스크립트 모두 대응 가능하도록 연결
|
|
if (healthSource is PlayerHealth ph) ph.OnHealthChanged += UpdateUI;
|
|
else if (healthSource is TrainingDummy td) td.OnHealthChanged += UpdateUI;
|
|
else if (healthSource is EnemyHealth eh) eh.OnHealthChanged += UpdateUI; // 추가
|
|
else if (healthSource is MonsterClass mc) mc.OnHealthChanged += UpdateUI; // 추가
|
|
|
|
if (hpSlider != null) hpSlider.value = 1f;
|
|
}
|
|
|
|
private void UpdateUI(float current, float max)
|
|
{
|
|
// 슬라이더 바 업데이트
|
|
if (hpSlider != null && max > 0)
|
|
{
|
|
hpSlider.value = current / max;
|
|
}
|
|
|
|
// ⭐ 글자 업데이트: "현재 체력 / 최대 체력" 형식
|
|
// Mathf.CeilToInt를 써서 소수점 없이 정수로 예쁘게 보여줍니다.
|
|
if (hpText != null)
|
|
{
|
|
hpText.text = $"{Mathf.CeilToInt(current)} / {Mathf.CeilToInt(max)}";
|
|
}
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (Camera.main != null)
|
|
transform.LookAt(transform.position + Camera.main.transform.forward);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (healthSource is PlayerHealth ph) ph.OnHealthChanged -= UpdateUI;
|
|
else if (healthSource is TrainingDummy td) td.OnHealthChanged -= UpdateUI;
|
|
else if (healthSource is EnemyHealth eh) eh.OnHealthChanged -= UpdateUI;
|
|
else if (healthSource is MonsterClass mc) mc.OnHealthChanged -= UpdateUI;
|
|
}
|
|
} |