unity3d 如何修复unity中的库存错误2021.3.13f1

dldeef67  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(164)

编辑了更多显示的脚本!我正在从This YouTube Video制作一个库存系统,但似乎无法让它工作。
错误1:资产\脚本\库存脚本\库存系统. cs(31,33):错误CS1579:foreach语句无法对类型为"InventorySlot"的变量进行操作,因为"InventorySlot"不包含"GetEnumerator"的公共示例或扩展定义
错误2:
资产\脚本\库存脚本\库存系统. cs(55,19):错误CS0029:无法将类型"System. Collections. Generic. List"隐式转换为"InventorySlot"
我的密码是:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;

[System.Serializable]
public class InventorySystem
{
    [SerializeField] private List<InventorySlot> inventorySlots;

    public int InventorySize => InventorySlots.Count;
    public List<InventorySlot> InventorySlots => inventorySlots;

    public UnityAction<InventorySlot> OnInventorySlotChanged;

    public InventorySystem(int size)
    {
        inventorySlots = new List<InventorySlot>(size);

        for(int i = 0; i < size; i++) 
        {
            inventorySlots.Add(new InventorySlot());
        }
    }

    public bool AddToInventory(InventoryItemData itemToAdd, int amountToAdd)
    {
        if(ContainsItem(itemToAdd, out InventorySlot invSlot))
        {
            foreach(var slot in invSlot) 
            {
                if(slot.RoomLeftInStack(amountToAdd))
                {
                    slot.AddToStack(amountToAdd);
                    OnInventorySlotChanged?.Invoke(invSlot);
                    return true;
                }
            }
        }

        if(HasFreeSlot(out InventorySlot freeSlot))
        {
            freeSlot.UpdateInvSlot(itemToAdd, amountToAdd);
            OnInventorySlotChanged?.Invoke(freeSlot);
            return true;
        }
        
        return false;
    }

    public bool ContainsItem(InventoryItemData itemToAdd, out InventorySlot invSlot)
    {
        invSlot = InventorySlots.Where(i => i.ItemData == itemToAdd).ToList();
        return invSlot == null ? false : true;
    }

    public bool HasFreeSlot(out InventorySlot freeSlot)
    {
        freeSlot = InventorySlots.FirstOrDefault(i => i.ItemData == null);
        return freeSlot == null ? false : true;
    }
}

持有人脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

[System.Serializable]
public class InventoryHolder : MonoBehaviour
{
    [SerializeField] private int inventorySize;
    [SerializeField] protected InventorySystem inventorySystem;

    public InventorySystem InventorySystem => inventorySystem;

    public static UnityAction<InventorySystem> OnDynamicInventoryDisplayRequested;

    private void Awake()
    {
        inventorySystem = new InventorySystem(inventorySize);
    }
}

插槽脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class InventorySlot
{
    [SerializeField] private InventoryItemData itemData;
    [SerializeField] private int stackSize;

    public InventoryItemData ItemData => itemData;
    public int StackSize => stackSize;

    public InventorySlot(InventoryItemData source, int amount)
    {
        itemData = source;
        stackSize = amount;
    }

    public InventorySlot()
    {
        ClearSlot();
    }

    public bool RoomLeftInStack(int amountToAdd, out int amountRemaining)
    {
        amountRemaining = ItemData.maxStackSize - stackSize;
        return RoomLeftInStack(amountToAdd);
    }

    public bool RoomLeftInStack(int amountToAdd)
    {
        if(stackSize + amountToAdd <= itemData.maxStackSize) return true;
        else return false;
    }

    public void ClearSlot()
    {
        itemData = null;
        stackSize = -1;
    }

    public void UpdateInvSlot(InventoryItemData data, int amount)
    {
        itemData = data;
        stackSize = amount;
    }

    public void AddToStack(int amount)
    {
        stackSize += amount;
    }

    public void RemoveFromStack(int amount)
    {
        stackSize -= amount;
    }
}

我只是想删除这些错误,以便进行测试。

xqk2d5yq

xqk2d5yq1#

List类的Where方法返回一个List而不是单个InventorySlot对象。
您可以将函数“ContainsItem”重写为:

public bool ContainsItem(InventoryItemData itemToAdd, out InventorySlot invSlot)
{
    invSlot = InventorySlots.FirstOrDefault(i => i.ItemData == itemToAdd);
    return invSlot == null ? false : true;
}

相关问题