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(); private TMPro.TextMeshProUGUI _maxText;// = _barText.transform.Find("MaxText").GetComponent(); 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 _onUpdateAction; enum BarType { Exp, Health, Mana, Stamina } private void Awake() { _valueBar = transform.Find("Stack/ImageStack/ValueBar").GetComponent(); _icon = transform.Find("Stack/Icon").gameObject; if (_icon != null) _iconText = _icon.transform.Find("BackGround/Text").GetComponent(); _barText = transform.Find("BarText").gameObject; if (_barText != null) { _currentText = _barText.transform.Find("CurrentText").GetComponent(); _maxText = _barText.transform.Find("MaxText").GetComponent(); } RectTransform fillRect = _valueBar.rectTransform; fillRect.anchorMax = new Vector2(0, 1); } private void OnEnable() { switch (_type) { case BarType.Exp: EventBus.Subscribe(OnExpChanged); break; case BarType.Health: break; case BarType.Mana: break; case BarType.Stamina: break; } } private void OnDisable() { switch (_type) { case BarType.Exp: EventBus.Unsubscribe(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); } }