58 lines
1.2 KiB
C#
58 lines
1.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SegmentedStaminaUI : MonoBehaviour
|
|
{
|
|
public Image[] staminaBoxes;
|
|
|
|
private Stamina playerStamina;
|
|
|
|
private void Start()
|
|
{
|
|
InvokeRepeating(nameof(FindPlayerStamina), 0f, 0.5f);
|
|
}
|
|
|
|
void FindPlayerStamina()
|
|
{
|
|
PlayerControllerNewRohan player =
|
|
FindObjectOfType<PlayerControllerNewRohan>();
|
|
|
|
if (player == null)
|
|
return;
|
|
|
|
playerStamina = player.GetComponent<Stamina>();
|
|
|
|
if (playerStamina == null)
|
|
return;
|
|
|
|
playerStamina.OnStaminaChanged += UpdateUI;
|
|
|
|
UpdateUI(
|
|
playerStamina.currentStamina,
|
|
playerStamina.maxStamina
|
|
);
|
|
|
|
CancelInvoke(nameof(FindPlayerStamina));
|
|
}
|
|
|
|
void UpdateUI(float current, float max)
|
|
{
|
|
float percent = current / max;
|
|
|
|
int active =
|
|
Mathf.FloorToInt(percent * staminaBoxes.Length);
|
|
|
|
for (int i = 0; i < staminaBoxes.Length; i++)
|
|
{
|
|
staminaBoxes[i].enabled = i < active;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (playerStamina != null)
|
|
{
|
|
playerStamina.OnStaminaChanged -= UpdateUI;
|
|
}
|
|
}
|
|
} |