Files
WhaleAdventure_VR/Assets/My project/Inventory/Scripts/InventoryUI.cs
skrwns304@gmail.com b1e85a5b89 2026-06-19 UI, UI로직
2026-06-19 14:27:40 +09:00

116 lines
2.8 KiB
C#

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;
}
}