First project upload
This commit is contained in:
58
Assets/New Lowpoly Models/Scripts/SegmentedStaminaUI.cs
Normal file
58
Assets/New Lowpoly Models/Scripts/SegmentedStaminaUI.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user