ryzerrr's picture
Upload folder using huggingface_hub
6cae93f verified
Raw
History Blame Contribute Delete
1.74 kB
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace UnityAgent
{
/// <summary>
/// Lightweight HUD manager using Unity UI + TextMeshPro. Exposes methods
/// for other systems to update score / health / messages.
/// </summary>
public class UIManager : MonoBehaviour
{
public static UIManager Instance { get; private set; }
[Header("HUD")]
public TMP_Text scoreText;
public TMP_Text healthText;
public TMP_Text messageText;
public Image healthBarFill;
[Header("Message")]
public float messageDuration = 2f;
private float _messageTimer;
private void Awake()
{
if (Instance != null && Instance != this) { Destroy(gameObject); return; }
Instance = this;
SetMessage(string.Empty);
}
private void Update()
{
if (_messageTimer > 0f)
{
_messageTimer -= Time.deltaTime;
if (_messageTimer <= 0f && messageText != null) messageText.text = string.Empty;
}
}
public void SetScore(int score)
{
if (scoreText != null) scoreText.text = $"Score: {score}";
}
public void SetHealth(float current, float max)
{
if (healthText != null) healthText.text = $"HP: {Mathf.CeilToInt(current)} / {Mathf.CeilToInt(max)}";
if (healthBarFill != null) healthBarFill.fillAmount = Mathf.Clamp01(current / max);
}
public void SetMessage(string msg)
{
if (messageText != null) messageText.text = msg;
_messageTimer = string.IsNullOrEmpty(msg) ? 0f : messageDuration;
}
}
}