Project_WL/Assets/WL/Scripts/UI/PlayerLevelHud.cs

59 lines
2.0 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using WL.Player;
namespace WL.UI
{
/// <summary>
/// 좌상단 플레이어 레벨 표기 (PD #727 · 레퍼런스 XP Hero: 레벨 배지 + 경험치 바 "1.08M/1.19M").
/// PlayerProgress.Changed 를 구독해 갱신한다. 표기 형식: 1,000 이상 K · 1,000,000 이상 M.
/// </summary>
public class PlayerLevelHud : MonoBehaviour
{
[Header("참조 (비우면 씬에서 찾는다)")]
[SerializeField] private PlayerProgress progress;
[Tooltip("레벨 숫자")]
[SerializeField] private Text levelText;
[Tooltip("경험치 텍스트 (현재/필요)")]
[SerializeField] private Text xpText;
[Tooltip("경험치 바 채움 이미지 (Image Type = Filled · Horizontal)")]
[SerializeField] private Image xpFill;
private void OnEnable()
{
Resolve();
if (progress != null) progress.Changed += Refresh;
Refresh();
}
private void OnDisable()
{
if (progress != null) progress.Changed -= Refresh;
}
private void Start() { Refresh(); }
private void Resolve()
{
if (progress == null) progress = Object.FindFirstObjectByType<PlayerProgress>();
}
public void Refresh()
{
Resolve();
if (progress == null) return;
if (levelText != null) levelText.text = progress.Level.ToString();
if (xpText != null) xpText.text = Format(progress.Xp) + "/" + Format(progress.XpToNext);
if (xpFill != null) xpFill.fillAmount = progress.XpToNext > 0 ? Mathf.Clamp01((float)progress.Xp / progress.XpToNext) : 0f;
}
/// <summary>1.08M · 4.31K · 121 형식.</summary>
public static string Format(long v)
{
if (v >= 1000000L) return (v / 1000000f).ToString("0.00") + "M";
if (v >= 1000L) return (v / 1000f).ToString("0.00") + "K";
return v.ToString();
}
}
}