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