fgm24/Assets/Scripts/Misc/HealthBar.cs

31 lines
688 B
C#

using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
[SerializeField]
private HealthComponent attachedHealth;
[SerializeField]
private Image healthBarImg;
[SerializeField]
private Color healthLeftColor, unusedHealth;
private void OnEnable()
{
attachedHealth.OnHealthChange.AddListener(UpdateHealthBar);
}
private void OnDisable()
{
attachedHealth.OnHealthChange.RemoveListener(UpdateHealthBar);
}
private void UpdateHealthBar(float prevHealth, float newHealth)
{
float percent = newHealth / attachedHealth.getMaxHealth();
healthBarImg.fillAmount = percent;
}
}