File size: 1,742 Bytes
40c0886 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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;
}
}
}
|