3DTD/Assets/Scripts/Utilities/HealthBar.cs

33 lines
701 B
C#

using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
[SerializeField]
private Image healthBar;
[SerializeField]
private HealthComponent attachHealth;
private void Awake()
{
attachHealth.OnHealthChange.AddListener(UpdateBar);
}
private void Start()
{
UpdateBar(0f, attachHealth.currentHealth); // initial check
}
private void OnDestroy()
{
attachHealth.OnHealthChange.RemoveListener(UpdateBar);
}
private void UpdateBar(float prev, float curr)
{
float percent = curr / attachHealth.StartHealth;
healthBar.fillAmount = percent;
}
}