public void updateSlotAmount()
{
if (prefabSlot == null)
prefabSlot = Resources.Load("Prefabs/Slot - Inventory") as GameObject;
if (SlotContainer == null)
{
SlotContainer = (GameObject)Instantiate(prefabSlotContainer);
SlotContainer.transform.SetParent(PanelRectTransform.transform);
SlotContainerRectTransform = SlotContainer.GetComponent<RectTransform>();
SlotGridRectTransform = SlotContainer.GetComponent<RectTransform>();
SlotGridLayout = SlotContainer.GetComponent<GridLayoutGroup>();
}
if (SlotContainerRectTransform == null)
SlotContainerRectTransform = SlotContainer.GetComponent<RectTransform>();
SlotContainerRectTransform.localPosition = Vector3.zero;
List<Item> itemsToMove = new List<Item>();
List<GameObject> slotList = new List<GameObject>();
foreach (Transform child in SlotContainer.transform)
{
if (child.tag == "Slot") { slotList.Add(child.gameObject); }
}
while (slotList.Count > width * height)
{
GameObject go = slotList[slotList.Count - 1];
ItemOnObject itemInSlot = go.GetComponentInChildren<ItemOnObject>();
if (itemInSlot != null)
{
itemsToMove.Add(itemInSlot.item);
ItemsInInventory.Remove(itemInSlot.item);
}
slotList.Remove(go);
DestroyImmediate(go);
}
if (slotList.Count < width * height)
{
for (int i = slotList.Count; i < (width * height); i++)
{
GameObject Slot = (GameObject)Instantiate(prefabSlot);
Slot.name = (slotList.Count + 1).ToString();
Slot.transform.SetParent(SlotContainer.transform);
slotList.Add(Slot);
}
}
if (itemsToMove != null && ItemsInInventory.Count < width * height)
{
foreach (Item i in itemsToMove)
{
addItemToInventory(i.itemID);
}
}
setImportantVariables();
}