2025-02-11 18:00:25 +08:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace BeiBao
|
|
|
|
|
{
|
|
|
|
|
public class BeiBaoTest : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
Inventory inventory;
|
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
// 创建背包,容量为 10 格子
|
|
|
|
|
inventory = new Inventory(10);
|
|
|
|
|
|
|
|
|
|
// 显示背包内容
|
|
|
|
|
Debug.Log("背包内容:");
|
|
|
|
|
inventory.ShowInventory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.A))
|
|
|
|
|
{
|
|
|
|
|
// 创建物品
|
|
|
|
|
Item healthPotion = new Item("Health Potion", 1, 10);
|
|
|
|
|
Item manaPotion = new Item("Mana Potion", 2, 5);
|
|
|
|
|
|
|
|
|
|
// 添加物品到背包
|
2025-02-13 10:24:05 +08:00
|
|
|
|
inventory.AddItem(healthPotion, 15, out var count); // 添加 15 个 Health Potion
|
|
|
|
|
inventory.AddItem(manaPotion, 7, out var count1); // 添加 7 个 Mana Potion
|
2025-02-11 18:00:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.D))
|
|
|
|
|
{
|
|
|
|
|
// 从背包中取出物品
|
2025-02-13 10:24:05 +08:00
|
|
|
|
inventory.RemoveItem(1, 5, out var count);
|
2025-02-11 18:00:25 +08:00
|
|
|
|
Debug.Log("取出 5 个 Health Potion");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.S))
|
|
|
|
|
{
|
|
|
|
|
// 显示更新的背包内容
|
|
|
|
|
Debug.Log("更新的背包内容:");
|
|
|
|
|
inventory.ShowInventory();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|