public void UpdateInventory() { // we have this Item for (int i = 0; i < _itemUI.Count; i++) { if (i < _InventoryID.Count) { Inventory_ID InvID = _InventoryID[i]; _itemUI[i].m_Sprite.sprite = myData.Data[InvID.id].Sprite; _itemUI[i].m_Quantity.text = InvID.quantity.ToString(); _itemUI[i].m_ID = InvID.id; } } // we don't have this Item then Addit to List if (_InventoryID.Count > _itemUI.Count) { for (int i = _itemUI.Count; i < _InventoryID.Count; i++) { ItemUI itemUI = Instantiate(_itemPrefab.GetComponent <ItemUI>(), inventory_UI); _itemUI.Add(itemUI); Inventory_ID InvID = _InventoryID[i]; _itemUI[i].m_Sprite.sprite = myData.Data[InvID.id].Sprite; _itemUI[i].m_Quantity.text = InvID.quantity.ToString(); _itemUI[i].m_ID = InvID.id; print(" Create New ItemUI! "); } } }
// Add Item public void AddItem(int _id, int _Quantity) { for (int i = 0; i < _InventoryID.Count; i++) { if (_InventoryID[i].id == _id) { _InventoryID[i] = new Inventory_ID(_InventoryID[i].id, _InventoryID[i].quantity + _Quantity); UpdateInventory(); return; } } _InventoryID.Add(new Inventory_ID(_id, _Quantity)); UpdateInventory(); }
// Remove Item public void RemoveItem(int _id, int _Quantity) { for (int i = 0; i < _InventoryID.Count; i++) { if (_InventoryID[i].id == _id) { _InventoryID[i] = new Inventory_ID(_InventoryID[i].id, _InventoryID[i].quantity - _Quantity); if (_InventoryID[i].quantity <= 0) { _InventoryID.Remove(_InventoryID[i]); Destroy(_itemUI[i].gameObject); _itemUI.Remove(_itemUI[i]); } UpdateInventory(); return; } } }