using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; namespace DDD { public class InventoryView : MonoBehaviour, IEventHandler { [SerializeField] private Transform _slotParent; [SerializeField] private GameObject _slotPrefab; private readonly Dictionary _slotLookup = new(); private void OnEnable() { EventBus.Register(this); } private void OnDisable() { EventBus.Unregister(this); } public void ShowItems(Func predicate) { Clear(); var models = ItemViewModelFactory.CreateRestaurantManagementInventoryItem(predicate); foreach (var model in models) { var go = Instantiate(_slotPrefab, _slotParent); var slot = go.GetComponent(); slot.Initialize(model); // 슬롯 참조 저장 _slotLookup[model.Id] = slot; } if (_slotParent.childCount > 0) { EventSystem.current.SetSelectedGameObject(_slotParent.GetChild(0).gameObject); } } private void Clear() { foreach (Transform child in _slotParent) { Destroy(child.gameObject); } } public void Invoke(InventoryChangedEvent evt) { if (_slotLookup.TryGetValue(evt.ItemId, out var slot)) { slot.UpdateCount(evt.NewCount); } } } }