2026-06-19 UI, UI로직
This commit is contained in:
115
Assets/My project/Inventory/Scripts/InventoryUI.cs
Normal file
115
Assets/My project/Inventory/Scripts/InventoryUI.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 전체 인벤토리 UI를 갱신하는 스크립트입니다.
|
||||
/// InventoryManager의 변경 이벤트를 받아 각 InventorySlotUI에 전달합니다.
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
public class InventoryUI : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
[SerializeField] private InventoryManager inventoryManager;
|
||||
[SerializeField] private GameObject inventoryPanel;
|
||||
[SerializeField] private InventorySlotUI[] slots;
|
||||
|
||||
[Header("Settings")]
|
||||
[SerializeField] private bool autoFindManager = true;
|
||||
[SerializeField] private bool refreshOnEnable = true;
|
||||
[SerializeField] private bool showNewBadgeOnIncrease = true;
|
||||
|
||||
private bool subscribed;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (inventoryPanel == null)
|
||||
inventoryPanel = gameObject;
|
||||
|
||||
if (inventoryManager == null && autoFindManager)
|
||||
inventoryManager = FindFirstObjectByType<InventoryManager>();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
Subscribe();
|
||||
|
||||
if (refreshOnEnable)
|
||||
RefreshAllSlots();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
Unsubscribe();
|
||||
}
|
||||
|
||||
public void SetVisible(bool visible)
|
||||
{
|
||||
if (inventoryPanel != null)
|
||||
inventoryPanel.SetActive(visible);
|
||||
}
|
||||
|
||||
public void ToggleVisible()
|
||||
{
|
||||
if (inventoryPanel != null)
|
||||
inventoryPanel.SetActive(!inventoryPanel.activeSelf);
|
||||
}
|
||||
|
||||
public void SetManager(InventoryManager manager)
|
||||
{
|
||||
if (inventoryManager == manager)
|
||||
return;
|
||||
|
||||
Unsubscribe();
|
||||
inventoryManager = manager;
|
||||
Subscribe();
|
||||
RefreshAllSlots();
|
||||
}
|
||||
|
||||
public void RefreshAllSlots()
|
||||
{
|
||||
if (inventoryManager == null || slots == null)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < slots.Length; i++)
|
||||
{
|
||||
InventorySlotUI slot = slots[i];
|
||||
if (slot == null)
|
||||
continue;
|
||||
|
||||
int count = inventoryManager.GetItemCount(slot.ItemType);
|
||||
slot.SetCount(count, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshSlot(InventoryItemType itemType, int count)
|
||||
{
|
||||
if (slots == null)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < slots.Length; i++)
|
||||
{
|
||||
InventorySlotUI slot = slots[i];
|
||||
if (slot == null || slot.ItemType != itemType)
|
||||
continue;
|
||||
|
||||
slot.SetCount(count, showNewBadgeOnIncrease);
|
||||
}
|
||||
}
|
||||
|
||||
private void Subscribe()
|
||||
{
|
||||
if (subscribed || inventoryManager == null)
|
||||
return;
|
||||
|
||||
inventoryManager.ItemCountChanged += RefreshSlot;
|
||||
subscribed = true;
|
||||
}
|
||||
|
||||
private void Unsubscribe()
|
||||
{
|
||||
if (!subscribed || inventoryManager == null)
|
||||
return;
|
||||
|
||||
inventoryManager.ItemCountChanged -= RefreshSlot;
|
||||
subscribed = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user