| 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; } | |
| [] | |
| public TMP_Text scoreText; | |
| public TMP_Text healthText; | |
| public TMP_Text messageText; | |
| public Image healthBarFill; | |
| [] | |
| 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; | |
| } | |
| } | |
| } | |