能提供一个完整的脚本代码吗
时间: 2024-03-21 07:39:23 浏览: 20
以下是一个简单的背包管理类脚本示例,可以添加、移除、更新和显示物品:
```csharp
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InventoryManager : MonoBehaviour
{
public GameObject inventoryUI;
public GameObject itemSlotPrefab;
public Transform itemSlotContainer;
public List<Item> items = new List<Item>();
public Dictionary<string, int> itemCounts = new Dictionary<string, int>();
private bool isInventoryOpen = false;
private void Start()
{
inventoryUI.SetActive(false);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
ToggleInventory();
}
}
public void AddItem(Item item)
{
items.Add(item);
if (itemCounts.ContainsKey(item.name))
{
itemCounts[item.name]++;
}
else
{
itemCounts[item.name] = 1;
CreateItemSlot(item);
}
UpdateItemSlot(item);
}
public void RemoveItem(Item item)
{
items.Remove(item);
if (itemCounts.ContainsKey(item.name))
{
itemCounts[item.name]--;
if (itemCounts[item.name] == 0)
{
itemCounts.Remove(item.name);
DestroyItemSlot(item);
}
}
UpdateItemSlot(item);
}
public void UpdateItemCount(Item item)
{
if (itemCounts.ContainsKey(item.name))
{
itemCounts[item.name]--;
UpdateItemSlot(item);
}
}
public void ToggleInventory()
{
isInventoryOpen = !isInventoryOpen;
inventoryUI.SetActive(isInventoryOpen);
}
private void CreateItemSlot(Item item)
{
GameObject itemSlot = Instantiate(itemSlotPrefab, itemSlotContainer);
itemSlot.name = item.name;
itemSlot.GetComponent<Image>().sprite = item.icon;
}
private void DestroyItemSlot(Item item)
{
Transform itemSlot = itemSlotContainer.Find(item.name);
Destroy(itemSlot.gameObject);
}
private void UpdateItemSlot(Item item)
{
Transform itemSlot = itemSlotContainer.Find(item.name);
Text itemText = itemSlot.GetComponentInChildren<Text>();
itemText.text = itemCounts[item.name].ToString();
}
}
```
其中,Item类表示背包中的物品,包括名称、描述、图标等信息:
```csharp
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
public new string name;
public string description;
public Sprite icon;
}
```
在UI界面中,需要创建一个包含物品栏位的容器,并将其关联到背包管理类的itemSlotContainer变量上。此外,还需要将物品栏位的预制体关联到背包管理类的itemSlotPrefab变量上。在UI界面中,可以使用Grid Layout Group组件将栏位自动排列。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)