fgm24/Assets/Scripts/Misc/HealthBar.cs

31 lines
703 B
C#
Raw Normal View History

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(int prevHealth, int newHealth)
{
float percent = ((float) newHealth) / ((float)attachedHealth.getMaxHealth());
healthBarImg.fillAmount = percent;
}
}