코딩의 신 50

김동관·2025년 12월 11일

오늘 한 일

  • InventoryUI구현(뼈대)
  • Status UI연결
  • M키 누르면 전체지도 (스크롤로 확대 및 축소가능. 아마 마우스 포인트 지점을 기준으로 스크롤 확대축소가능 하게? 할듯) 전체 맵 보게
  • 인벤토리 UI 추가작업(UI 다 구현이 안됨. 아이템 장비창 따로 만들어서 아이템 Type을 나누어서 인벤토리에 들어가게 해야하는지(?))
  • Status수치들 업뎃( 레벨, 체력, 스테미나 레벨마다 몇씩 증가하는지)
  • 왼쪽 하단 장착무기 연결, 오른쪽 충격파, 체력 물약, 구르기?였나 연결

나머지 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를 만들어 직접 연결을 했습니다. 이상입니다
profile
아이디어 뱅크

0개의 댓글