77 lines
1.9 KiB
C#
77 lines
1.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
public class RaftHealthUI : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private RaftHealth raftHealth;
|
|
[SerializeField] private Image hpFillImage;
|
|
[SerializeField] private TMP_Text hpText;
|
|
|
|
[Header("Text")]
|
|
[SerializeField] private string hpPrefix = "HP";
|
|
|
|
[Header("Options")]
|
|
[SerializeField] private bool autoFindHealth = true;
|
|
[SerializeField] private bool hideWhenNoHealth = false;
|
|
|
|
[Header("Debug")]
|
|
[SerializeField] private bool showDebugLog = true;
|
|
|
|
private void Awake()
|
|
{
|
|
if (autoFindHealth && raftHealth == null)
|
|
{
|
|
raftHealth = FindFirstObjectByType<RaftHealth>();
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (raftHealth != null)
|
|
{
|
|
raftHealth.onHealthChanged.AddListener(UpdateHealthUI);
|
|
UpdateHealthUI(raftHealth.CurrentHealth, raftHealth.MaxHealth);
|
|
}
|
|
else
|
|
{
|
|
if (hideWhenNoHealth)
|
|
gameObject.SetActive(false);
|
|
|
|
if (showDebugLog)
|
|
Debug.LogWarning("[RaftHealthUI] RaftHealth가 연결되지 않았습니다.", this);
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (raftHealth != null)
|
|
{
|
|
raftHealth.onHealthChanged.RemoveListener(UpdateHealthUI);
|
|
}
|
|
}
|
|
|
|
public void UpdateHealthUI(int currentHealth, int maxHealth)
|
|
{
|
|
if (maxHealth <= 0)
|
|
maxHealth = 1;
|
|
|
|
float ratio = Mathf.Clamp01((float)currentHealth / maxHealth);
|
|
|
|
if (hpFillImage != null)
|
|
{
|
|
hpFillImage.fillAmount = ratio;
|
|
}
|
|
|
|
if (hpText != null)
|
|
{
|
|
hpText.text = $"{currentHealth} / {maxHealth}";
|
|
}
|
|
|
|
if (showDebugLog)
|
|
{
|
|
Debug.Log($"[RaftHealthUI] 체력 UI 갱신: {currentHealth}/{maxHealth}");
|
|
}
|
|
}
|
|
} |