2024-02-03 22:18:28 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-02-04 01:25:59 +01:00
|
|
|
private void UpdateHealthBar(float prevHealth, float newHealth)
|
2024-02-03 22:18:28 +01:00
|
|
|
{
|
2024-02-04 01:25:59 +01:00
|
|
|
float percent = newHealth / attachedHealth.getMaxHealth();
|
2024-02-03 22:18:28 +01:00
|
|
|
healthBarImg.fillAmount = percent;
|
|
|
|
}
|
|
|
|
}
|