我有2个相同类型的武器插槽,但是当我单击武器类型的项目时,只有1个插槽被占用

时间:2019-03-20 20:58:05

标签: c# unity3d

我正在使用

用4种设备创建库存系统
public enum EquipmentType
{
    Weapon,
    Head,
    Body,
    Feet,
}

但是我有5个插槽(武器2个插槽)。我已经实现了RightClick事件(右键单击库存中的项目会将这些项目装备到设备面板中的相应插槽中)。因此,当我右键单击武器类型的项目时,只会占用一个插槽。 另外,我已经实现了拖放方法,似乎对于两个插槽都可以正常工作。以下是我使用的脚本。

EquipmentSlot.cs

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

public class EquipmentSlot : ItemSlot
{ 
    public EquipmentType EquipmentType;
    protected override void OnValidate()
    {
        base.OnValidate();
        gameObject.name = EquipmentType.ToString() + " Slot";
    }

    public override bool CanRecieveItem(Item item)
    {
        if (item == null)
            return true;

        EquippableItem equippableItem = item as EquippableItem;
        return equippableItem != null && equippableItem.EquipmentType == EquipmentType;
    }

EquipmentPanel.cs

using System;
using UnityEngine;

public class EquipmentPanel : MonoBehaviour
{
    public EquipmentSlot[] EquipmentSlots;
    [SerializeField] Transform equipmentSlotsParent;

    public event Action<ItemSlot> OnRightClickEvent;
    public event Action<ItemSlot> OnPointerEnterEvent;
    public event Action<ItemSlot> OnPointerExitEvent;
    public event Action<ItemSlot> OnBeginDragEvent;
    public event Action<ItemSlot> OnEndDragEvent;
    public event Action<ItemSlot> OnDragEvent;
    public event Action<ItemSlot> OnDropEvent;


    public void Start()
    {
        for (int i = 0; i < EquipmentSlots.Length; i++)
        {
            EquipmentSlots[i].OnRightClickEvent += OnRightClickEvent;
            EquipmentSlots[i].OnPointerEnterEvent += OnPointerEnterEvent;
            EquipmentSlots[i].OnPointerExitEvent += OnPointerExitEvent;
            EquipmentSlots[i].OnBeginDragEvent += OnBeginDragEvent;
            EquipmentSlots[i].OnEndDragEvent += OnEndDragEvent;
            EquipmentSlots[i].OnDragEvent += OnDragEvent;
            EquipmentSlots[i].OnDropEvent += OnDropEvent;
        }
    }
    private void OnValidate()
    {
        EquipmentSlots = equipmentSlotsParent.GetComponentsInChildren<EquipmentSlot>();
    }
    public bool AddItem(EquippableItem item, out EquippableItem previousItem)
    {
        for(int i=0; i < EquipmentSlots.Length; i++)
        {
            if(EquipmentSlots[i].EquipmentType == item.EquipmentType)
            {
                previousItem = (EquippableItem)EquipmentSlots[i].Item;
                EquipmentSlots[i].Item = item;
                return true;
            }
        }
        previousItem = null;
        return false;
    }
    public bool RemoveItem(EquippableItem item)
    {
        for (int i = 0; i < EquipmentSlots.Length; i++)
        {
            if (EquipmentSlots[i].Item == item)
            {
                EquipmentSlots[i].Item = null;
                return true;
            }
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

您的AddItem()方法找到类型为EquipmentType.Weapon first 插槽,并将其替换为您要添加的项目。

如果您有2个类型为EquipmentType.Weapon的插槽,这意味着AddItem()始终替换第一个找到的插槽,并且甚至都不要触摸第二个插槽

这可能是一种可行的方式:

public bool AddItem(EquippableItem item, out EquippableItem previousItem)
{
    int lastFoundSlotIndex = -1;
    for(int i=0; i < EquipmentSlots.Length; i++)
    {
        if(EquipmentSlots[i].EquipmentType == item.EquipmentType)
        {
            lastFoundSlotIndex = i;

            if (EquipmentSlots[i].Item == null)
                break;
        }
    }

    if (lastFoundSlotIndex >= 0)
    {
        previousItem = (EquippableItem)EquipmentSlots[lastFoundSlotIndex].Item;
        EquipmentSlots[lastFoundSlotIndex].Item = item;
        return true;
    }

    previousItem = null;
    return false;
}