Files
TutorialRTS/Assets/Scripts/UnitHealthBar.cs

32 lines
787 B
C#

using UnityEngine;
using UnityEngine.UI;
public class UnitHealthBar : MonoBehaviour
{
public GameObject healthContainer;
public RectTransform healthFill;
private float maxSize;
void Awake()
{
maxSize = healthFill.sizeDelta.x;
healthContainer.SetActive(false);
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void UpdateHealthBar(int curHp, int maxHp)
{
healthContainer.SetActive(true);
float healthPercentage = (float)curHp / (float)maxHp;
healthFill.sizeDelta = new Vector2(maxSize * healthPercentage, healthFill.sizeDelta.y);
}
}