Files
gpd-924-en/EditorCustomization/Assets/Scripts/UI/HealthBar.cs
T
david a6dbfb224f add scriptable object examples
add editor window example
add attribute + drawer example
2025-06-13 18:07:18 +02:00

53 lines
1.4 KiB
C#

using System;
using Data;
using Events;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace UI
{
public class HealthBar : MonoBehaviour
{
[Header("Events")]
[SerializeField] private EventBroker eventBroker;
[Header("Values")]
[SerializeField] private FloatValue maxHealth;
[SerializeField] private FloatValue health;
[Header("References")]
[SerializeField] private Image fillingImage;
[SerializeField] private TextMeshProUGUI deadText;
private void Awake()
{
fillingImage.type = Image.Type.Filled;
fillingImage.fillMethod = Image.FillMethod.Horizontal;
deadText.gameObject.SetActive(false);
}
private void OnEnable()
{
eventBroker.PlayerDied += OnPlayerDied;
}
private void OnDisable()
{
eventBroker.PlayerDied -= OnPlayerDied;
}
private void Update()
{
fillingImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,
Mathf.Clamp01(health.Value / maxHealth.Value) * 200.0f);
}
private void OnPlayerDied()
{
fillingImage.fillAmount = 0.0f;
fillingImage.color = Color.red;
deadText.gameObject.SetActive(true);
}
}
}