首页 > 解决方案 > Unity - 在方法中添加多个数组参数(用于添加多个项目)

问题描述

我正在尝试实现一种可以调用来创建多个项目的方法

这是我试图让它工作的方法

public void AddMultipleItems(string[] itemKey, int[] amount)
{

    for (int i = 0; i < itemKey.Length; i++)
    {
        Item item;
        item = ItemCollection[itemKey[i]];

        for (int x = 0; x < amount.Length; x++)
        {
            if (inventory.CanAddItem(item, amount[x]) == true)
            {
                inventory.AddItem(item.GetCopy());
            }
            else if (inventory.CanAddItem(item, amount[x]) == false)
            {
                Debug.Log("Inventory Full");
                break;
            }

        }
    }

}

然后将调用此方法以添加如下项目:

itemDB.AddMultipleItems(new string[] { "Boots", "Gold Coin", "Apple" }, new int[] {1, 2, 3 });

结果:我得到 3 个靴子、3 个金币和 3 个苹果。当我应该得到 1 个靴子、2 个金币和 3 个苹果时,

我已经完成了类似的方法,除了它不需要数组参数并且它完美地工作:

 public void AddItems(string itemKey, int amount)
{
    //First check if entered itemKey parameter exists in ItemCollection Dictionary
    if (ItemCollection.ContainsKey(itemKey))
    {
        Item item;
        item = ItemCollection[itemKey];
        if (item != null)
        {
            //check if we can add the item and the amount of it
            if (inventory.CanAddItem(item, amount))
            {
                //loop through the total amount
                for (int i = 0; i < amount; i++)
                {
                    //add the item
                    inventory.AddItem(item.GetCopy());
                }
                Debug.Log(itemKey + " Added Successfully!");
            }
            else
            {
                Debug.Log("Not enough space in Inventory");
            }
        }
        else
        {
            Debug.Log("Null Item");
        }
    }
    else
    {
        Debug.Log(itemKey + " does not exist in ItemDatabase's Dictionary");
    }
}

所以基本上另一种看待它的方式是如何将AddItems(string itemKey, int amount)变成AddItems(string[] itemKey, int[] amount)。它的 for 循环、foreach 循环和数组有点让我绊倒,因为我不太擅长这些。

感谢任何帮助,谢谢!

标签: arraysfor-loopunity3dforeachinventory

解决方案


对于每一个item您都在迭代amount具有 3 个条目的整个数组。我实际上希望为每个1+2+3 = 6项目添加项目。

难道您不想只从与给定 itemKey 具有相同索引的一个条目中获取要添加的金额:amountamount[i]

public void AddMultipleItems(string[] itemKey, int[] amount)
{
    for (int i = 0; i < itemKey.Length; i++)
    {
        Item item;
        item = ItemCollection[itemKey[i]];

        if (inventory.CanAddItem(item, amount[i]))
        {
            inventory.AddItem(item.GetCopy());
        }
        else // your if condition here was redundant
        {
            Debug.Log("Inventory Full");
        }
    }
}

一般来说,你已经有一个添加单个项目的方法,所以我不会为多个项目重新实现它,而是从循环中调用它,比如

public void AddMultipleItems(string[] itemKey, int[] amount)
{
    for (int i = 0; i < itemKey.Length; i++)
    {
        // Instead of re-implement rather call your existing method
        // and treat each value pair as if ti was a single add call
        AddItems(itemKey[i], amount[i]);
    }
}

然后传入两个单独的数组很容易出错并且难以维护。我可能宁愿通过一个Dictionary<string, int>类似的例子

public void AddMultipleItems(Dictionary<string, int> itemsToAdd)
{
    foreach (var kvp in itemsToAdd)
    {
        AddItems(kvp.Key, kvp.Value);
    }
}

然后像这样称呼它

 itemDB.AddMultipleItems(new Dictionary<string, int>{ {"Boots", 1}, {"Gold Coin", 2}, {"Apple", 3}});

推荐阅读