【发布时间】:2019-09-06 06:11:31
【问题描述】:
我已经完成了库存槽位,并且想锁定和解锁一些槽位以便在......之后进行扩展(按 m 进行测试)
我现在按M,解锁槽增加,但增加解锁槽数量后不能使用扩展槽。
已完成所有库存槽和物品以在槽之间移动
public abstract class ItemContainer : MonoBehaviour, IItemContainer
{
public List<ItemSlot> ItemSlots;
public int unlockSlot = 1;
public event Action<BaseItemSlot> OnPointerEnterEvent;
public event Action<BaseItemSlot> OnPointerExitEvent;
public event Action<BaseItemSlot> OnRightClickEvent;
public event Action<BaseItemSlot> OnBeginDragEvent;
public event Action<BaseItemSlot> OnEndDragEvent;
public event Action<BaseItemSlot> OnDragEvent;
public event Action<BaseItemSlot> OnDropEvent;
protected virtual void OnValidate()
{
GetComponentsInChildren(includeInactive: true, result: ItemSlots);
}
protected virtual void Start()
{
for (int i = 0; i < ItemSlots.Count/5* unlockSlot; i++)
{
ItemSlots[i].OnPointerEnterEvent += slot => EventHelper(slot, OnPointerEnterEvent);
ItemSlots[i].OnPointerExitEvent += slot => EventHelper(slot, OnPointerExitEvent);
ItemSlots[i].OnRightClickEvent += slot => EventHelper(slot, OnRightClickEvent);
ItemSlots[i].OnBeginDragEvent += slot => EventHelper(slot, OnBeginDragEvent);
ItemSlots[i].OnEndDragEvent += slot => EventHelper(slot, OnEndDragEvent);
ItemSlots[i].OnDragEvent += slot => EventHelper(slot, OnDragEvent);
ItemSlots[i].OnDropEvent += slot => EventHelper(slot, OnDropEvent);
}
}
private void EventHelper(BaseItemSlot itemSlot, Action<BaseItemSlot> action)
{
if (action != null)
action(itemSlot);
}
public virtual bool CanAddItem(Item item, int amount = 1)
{
int freeSpaces = 0;
foreach (ItemSlot itemSlot in ItemSlots)
{
if (itemSlot.Item == null || itemSlot.Item.ID == item.ID)
{
freeSpaces += item.MaxStacks - itemSlot.Amount;
}
}
return freeSpaces >= amount;
}
public virtual bool AddItem(Item item)
{
for (int i = 0; i < ItemSlots.Count; i++)
{
if (ItemSlots[i].CanAddStack(item))
{
ItemSlots[i].Item = item;
ItemSlots[i].Amount++;
return true;
}
}
for (int i = 0; i < ItemSlots.Count; i++)
{
if (ItemSlots[i].Item == null)
{
if (item.ID == item.ID)
{
ItemSlots[i].Item = item;
ItemSlots[i].Amount++;
return true;
}
}
}
return false;
}
public virtual bool RemoveItem(Item item)
{
for (int i = 0; i < ItemSlots.Count; i++)
{
if (ItemSlots[i].Item == item)
{
ItemSlots[i].Amount--;
return true;
}
}
return false;
}
public virtual Item RemoveItem(string itemID)
{
for (int i = 0; i < ItemSlots.Count; i++)
{
Item item = ItemSlots[i].Item;
if (item != null && item.ID == itemID)
{
ItemSlots[i].Amount--;
return item;
}
}
return null;
}
public virtual int ItemCount(string itemID)
{
int number = 0;
for (int i = 0; i < ItemSlots.Count; i++)
{
Item item = ItemSlots[i].Item;
if (item != null && item.ID == itemID)
{
number += ItemSlots[i].Amount;
}
}
return number;
}
private void Update()
{
if (Input.GetKeyUp(KeyCode.M))
{
unlockSlot++;
Debug.Log(unlockSlot);
}
}
}
解锁槽不增加(调试不显示),其余槽禁用使用
【问题讨论】:
-
固定:只需将启动功能复制到固定更新。