1. ExpBar를 UIBar 로 변경해서 UIButton과 같이 여러곳에서 공용으로 사용할 수 있게 변경 2. EventBus 를 만들어서 Button같이 자기 스스로 동작을 시작할 수 있는 오브젝트들 말고 수동적인 오브젝트들이 값을 받는 방식을 관리하게 설정 - PlayerController, UIManager, UIBar, EventBus를 연결함 3. Events 아래에 Player의 이벤트에 연관된 코드를 PlayerEvents 로 만들어서 관리함 - 경험치 전달 양식이 여기 존재 - 체력바나 마나바 같은 양식도 여기서 만들 예정
150 lines
3.6 KiB
C#
150 lines
3.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UIBar : MonoBehaviour
|
|
{
|
|
[SerializeField] private BarType _type;
|
|
|
|
private GameObject _icon;
|
|
private Image _valueBar;
|
|
private GameObject _barText;
|
|
|
|
private TMPro.TextMeshProUGUI _currentText;//= _barText.transform.Find("CurrentText").GetComponent<TMPro.TextMeshProUGUI>();
|
|
private TMPro.TextMeshProUGUI _maxText;// = _barText.transform.Find("MaxText").GetComponent<TMPro.TextMeshProUGUI>();
|
|
private TMPro.TextMeshProUGUI _iconText;
|
|
|
|
|
|
private bool _onIcon;
|
|
|
|
public bool OnIcon
|
|
{
|
|
get => _onIcon;
|
|
set
|
|
{
|
|
_onIcon = value;
|
|
if (_icon != null)
|
|
_icon.SetActive(_onIcon);
|
|
}
|
|
}
|
|
|
|
private bool _onText;
|
|
public bool OnText
|
|
{
|
|
get => _onText;
|
|
set
|
|
{
|
|
_onText = value;
|
|
if (_barText != null)
|
|
_barText.SetActive(_onText);
|
|
}
|
|
}
|
|
|
|
private Action<string, float, float> _onUpdateAction;
|
|
|
|
enum BarType
|
|
{
|
|
Exp,
|
|
Health,
|
|
Mana,
|
|
Stamina
|
|
}
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
_valueBar = transform.Find("Stack/ImageStack/ValueBar").GetComponent<Image>();
|
|
|
|
_icon = transform.Find("Stack/Icon").gameObject;
|
|
if (_icon != null)
|
|
_iconText = _icon.transform.Find("BackGround/Text").GetComponent<TMPro.TextMeshProUGUI>();
|
|
|
|
_barText = transform.Find("BarText").gameObject;
|
|
if (_barText != null)
|
|
{
|
|
_currentText = _barText.transform.Find("CurrentText").GetComponent<TMPro.TextMeshProUGUI>();
|
|
_maxText = _barText.transform.Find("MaxText").GetComponent<TMPro.TextMeshProUGUI>();
|
|
}
|
|
|
|
RectTransform fillRect = _valueBar.rectTransform;
|
|
fillRect.anchorMax = new Vector2(0, 1);
|
|
}
|
|
|
|
|
|
private void OnEnable()
|
|
{
|
|
switch (_type)
|
|
{
|
|
case BarType.Exp:
|
|
EventBus.Subscribe<PlayerExpEvent>(OnExpChanged);
|
|
break;
|
|
case BarType.Health:
|
|
break;
|
|
case BarType.Mana:
|
|
break;
|
|
case BarType.Stamina:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
switch (_type)
|
|
{
|
|
case BarType.Exp:
|
|
EventBus.Unsubscribe<PlayerExpEvent>(OnExpChanged);
|
|
break;
|
|
case BarType.Health:
|
|
break;
|
|
case BarType.Mana:
|
|
break;
|
|
case BarType.Stamina:
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void OnExpChanged(PlayerExpEvent ev)
|
|
{
|
|
UpdateIconValue(ev.Level);
|
|
UpdateBarState(ev.CurrentExp, ev.MaxExp);
|
|
}
|
|
|
|
|
|
|
|
public void SwitchObject(bool onIcon, bool onText)
|
|
{
|
|
OnIcon = onIcon;
|
|
OnText = onText;
|
|
}
|
|
|
|
public void ChangeBar()
|
|
{
|
|
// 추후 바 타입에 따른 아이콘 및 색상 변경 구현
|
|
}
|
|
|
|
private void UpdateIconValue(string value)
|
|
{
|
|
if (_icon == null) return;
|
|
if (OnIcon) _iconText.text = value;
|
|
}
|
|
|
|
private void UpdateBarState(float current, float max)
|
|
{
|
|
if (_valueBar == null) return;
|
|
if (OnText)
|
|
{
|
|
if (_currentText != null) _currentText.text = ((int)current).ToString();
|
|
if (_maxText != null) _maxText.text = ((int)max).ToString();
|
|
}
|
|
|
|
float ratio = (max > 0) ? current / max : 0f;
|
|
RectTransform fillRect = _valueBar.rectTransform;
|
|
fillRect.anchorMax = new Vector2(ratio, 1);
|
|
}
|
|
|
|
|
|
|
|
}
|