您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
80 行
1.5 KiB
80 行
1.5 KiB
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
// Created with collaboration from:
|
|
// https://forum.unity.com/threads/inventory-system.980646/
|
|
[CreateAssetMenu(fileName = "Inventory", menuName = "Inventory/Inventory", order = 51)]
|
|
public class Inventory : ScriptableObject
|
|
{
|
|
[Tooltip("The collection of items and their quantities.")]
|
|
[SerializeField]
|
|
private List<ItemStack> _items = new List<ItemStack>();
|
|
|
|
public List<ItemStack> Items => _items;
|
|
|
|
public void Add(Item item, int count = 1)
|
|
{
|
|
if (count <= 0)
|
|
return;
|
|
|
|
for (int i = 0; i < _items.Count; i++)
|
|
{
|
|
ItemStack currentItemStack = _items[i];
|
|
if (item == currentItemStack.Item)
|
|
{
|
|
currentItemStack.Amount += count;
|
|
return;
|
|
}
|
|
}
|
|
|
|
_items.Add(new ItemStack(item, count));
|
|
}
|
|
|
|
public void Remove(Item item, int count = 1)
|
|
{
|
|
if (count <= 0)
|
|
return;
|
|
|
|
for (int i = 0; i < _items.Count; i++)
|
|
{
|
|
ItemStack currentItemStack = _items[i];
|
|
|
|
if (currentItemStack.Item == item)
|
|
{
|
|
currentItemStack.Amount -= count;
|
|
|
|
if (currentItemStack.Amount <= 0)
|
|
_items.Remove(currentItemStack);
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool Contains(Item item)
|
|
{
|
|
for (int i = 0; i < _items.Count; i++)
|
|
{
|
|
if (item == _items[i].Item)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public int Count(Item item)
|
|
{
|
|
for (int i = 0; i < _items.Count; i++)
|
|
{
|
|
ItemStack currentItemStack = _items[i];
|
|
if (item == currentItemStack.Item)
|
|
{
|
|
return currentItemStack.Amount;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|