Beispiel #1
0
    //called upon creation of this UI element.
    public void SetupFor <T>(AbsInventory inventory) where T : AbsItemSlot
    {
        if (slotParent.childCount != 0)
        {
            DestroyAllChildren();
        }
        for (int i = 0; i < inventory.inventorySize; i++)
        {
            var slot = Instantiate(slotPrefab, slotParent).GetComponent <T>();
            slot.partInventory    = inventory;
            slot.indexInInventory = i;
            slots.Add(slot);
        }
        var trans = transform as RectTransform;
        var size  = (Mathf.Ceil(inventory.inventorySize / 5f) * 55) + 30;

        trans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size);
        nameText.text = inventory.name;
        Refresh(inventory.containedItems);
    }
    //Combine two stacks of the same item. < >
    void CombineStacks(AbsInventory inv, int targetIndex)
    {
        var itemInfo = inv.containedItems[targetIndex];

        if (itemInfo.stackSize >= itemInfo.MaxStack)
        {
            return;
        }

        //the potential amount of items that could get added
        var maxAdd = itemInfo.MaxStack - itemInfo.stackSize;

        //the real amount that will get added to the existing stack (itemInfo)
        var realAdd = Mathf.Clamp(maxAdd, 0, partInventory.containedItems[indexInInventory].stackSize);

        //remove the real amount.
        partInventory.containedItems[indexInInventory].stackSize -= realAdd;
        if (partInventory.containedItems[indexInInventory].stackSize <= 0)
        {
            partInventory.containedItems[indexInInventory] = null;
        }
        //finally, ADD the amount.
        inv.containedItems[targetIndex].stackSize += realAdd;
    }