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; using UnityEngine; [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")] public class Item : ScriptableObject { public new string name; public string description; public Sprite icon; } 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(); } }
时间: 2024-02-10 22:15:03 浏览: 84
unity_script.docx
这段代码存在一些问题,其中一个问题是:
```
using UnityEngine;
```
这行代码应该放在文件开头的using语句之后,而不是在类定义之间。在C#中,using语句必须放在命名空间内的所有其他元素定义之前。
以下是修改后的代码:
```
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;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
public new string name;
public string description;
public Sprite icon;
}
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();
}
}
```
希望这个修改能够解决你的问题!
阅读全文