/** * Add a StackableInventoryItem to this item * Will move all children up to the max count allowed * including the parent item * * @param StackableInventoryItem items The item stack to add * @returns StackableInventoryItem the remaining items, or null * **/ public StackableInventoryItem AddStackableInventoryItems(StackableInventoryItem items) { if (count >= maxCount || items.itemName != this.itemName) { return(items); } // iterating through transforms as // `GetComponentsInChildren` does return the // top level component, which we don't want // so we'll create a transform list so we can // mutate the transforms in items.transform // and not break the loop List <Transform> tmpTransforms = new List <Transform>(); foreach (Transform t in items.transform) { tmpTransforms.Add(t); } foreach (Transform t in tmpTransforms) { StackableInventoryItem child = t.GetComponent <StackableInventoryItem>(); AddStackableInventoryItem(child); items.DecrementCount(); if (count >= maxCount) { return(items); } } // add the top level stackable item AddStackableInventoryItem(items); return(null); }
private void AddStackableInventoryItem(StackableInventoryItem item) { item.transform.parent = this.transform; IncrementCount(); item.DecrementCount(); }