오늘 한 일
나머지 4개는 나중에 추가 생각을 했습니다. 필요한 기능들이라
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class PlayerUIStatus : MonoBehaviour
{
[Header("Bars")]
public Image hpBar;
public Image staminaBar;
public Image expBar;
[Header("Bars의Text")]
public TMP_Text hpText;
public TMP_Text staminaText;
public TMP_Text expText;
public TMP_Text levelText;
public void UpdateHp(float current, float max)
{
hpBar.fillAmount = current / max;
if (hpText != null)
hpText.text = $"{Mathf.FloorToInt(current)} / {Mathf.FloorToInt(max)}";
}
// 스태미나 갱신
public void UpdateStamina(float current, float max)
{
staminaBar.fillAmount = current / max;
if (staminaText != null)
staminaText.text = $"{Mathf.FloorToInt(current)} / {Mathf.FloorToInt(max)}";
}
// EXP 갱신
public void UpdateExp(float current, float max)
{
expBar.fillAmount = current / max;
if (expText != null)
expText.text = $"{Mathf.FloorToInt(current)} / {Mathf.FloorToInt(max)}";
}
// 레벨 갱신
public void UpdateLevel(int level)
{
if (levelText != null)
levelText.text = $"Lv.{level}";
}
}
우선 PlayerStatusUI를 연결해주기 위해 UI를 스크립트에 연결해주었습니다.
// 플레이어/몬스터 공통 사용 기본 클래스
// 체력, 이동속도, 피격/사망 로직 기본형 제공
public abstract class CharacterBase : MonoBehaviour, IDamageable
{
[Header("공통 스탯")]
public float maxHp = 100f;
public float currentHp;
public float moveSpeed = 3f;
캐릭터베이스에 공동 스탯이 있습니다. 이걸 연결해주기 위해
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCharacter : CharacterBase
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
이 스크립트에서 받았습니다.
using UnityEngine;
public class PlayerStat : MonoBehaviour
{
[SerializeField] private PlayerUIStatus ui;
private CharacterBase character;
public int level = 1;
public float maxStamina = 100f;
public float currentStamina = 100f;
public float maxExp = 100f;
public float currentExp = 0f;
private void Awake()
{
character = GetComponent<CharacterBase>();
}
private void Start()
{
UpdateAllUI();
}
private void Update()
{
ui.UpdateHp(character.currentHp, character.maxHp);
ui.UpdateStamina(currentStamina, maxStamina);
ui.UpdateExp(currentExp, maxExp);
ui.UpdateLevel(level);
}
private void UpdateHpUI()
{
if (character == null) return;
ui.UpdateHp(character.currentHp, character.maxHp);
}
private void UpdateAllUI()
{
ui.UpdateHp(character.currentHp, character.maxHp);
ui.UpdateLevel(level);
}
} 후에 PlaeyrStatu를 만들어 직접 연결을 했습니다. 이상입니다