32 lines
716 B
C#
32 lines
716 B
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class EnemyHealthBarUI : MonoBehaviour
|
|
{
|
|
[SerializeField] private Health enemyHealth;
|
|
[SerializeField] private Slider healthSlider;
|
|
|
|
void Start()
|
|
{
|
|
if (enemyHealth == null) return;
|
|
|
|
healthSlider.maxValue = enemyHealth.maxHealth;
|
|
healthSlider.value = enemyHealth.currentHealth;
|
|
|
|
enemyHealth.OnHealthChanged += UpdateHealthBar;
|
|
}
|
|
|
|
void UpdateHealthBar(float current, float max)
|
|
{
|
|
healthSlider.maxValue = max;
|
|
healthSlider.value = current;
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (enemyHealth != null)
|
|
{
|
|
enemyHealth.OnHealthChanged -= UpdateHealthBar;
|
|
}
|
|
}
|
|
} |