如何解决库存中的无限物品
我一直在关注本系列教程:https://www.youtube.com/watch?v=LcizwQ7ogGA&list=PLJWSdH2kAe_Ij7d7ZFR2NIW8QCJE74CyT&index=3 我在 10 部分中的第 3 部分。
到目前为止还不错的系列,但是我遇到了一个非常大的问题。当我过去拿起一个项目时,它重新创建了该项目,毫无疑问地导致整个游戏非常快地滞后,Figure 1。 我不确定还能做什么,因为我所做的任何事情都无法解决它。有没有任何人可以发现错误或导致这种情况?我得到的唯一错误是第 48 行的对象引用未设置为对象的实例 Figure 2。这可能是因为我在 Inspector 中有一个新的预制件,Figure 3。
这是展示广告资源的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DisplayInventory : MonoBehaviour
{
public GameObject inventoryPrefab;
public InventoryObject inventory;
public int xStart;
public int yStart;
public int xSpaceBetweenItem;
public int numberOfColumns;
public int ySpaceBetweenItem;
Dictionary<InventorySlot,GameObject> itemsDisplayed = new Dictionary<InventorySlot,GameObject>();
// Start is called before the first frame update
void Start()
{
CreateDisplay();
}
// Update is called once per frame
void Update()
{
UpdateDisplay();
}
public void UpdateDisplay()
{
for (int i = 0; i < inventory.Container.Items.Count; i++)
{
InventorySlot slot = inventory.Container.Items[i];
if (itemsDisplayed.ContainsKey(slot))
{
itemsDisplayed[slot].GetComponentInChildren<Text>().text = inventory.Container.Items[i].amount.ToString("n0");
}
else
{
var obj = Instantiate(inventoryPrefab,Vector3.zero,Quaternion.identity,transform);
obj.transform.GetChild(0).GetComponentInChildren<Image>().sprite = inventory.database.GetItem[slot.item.id].uiDisplay;
obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
obj.GetComponentInChildren<Text>().text = slot.amount.ToString("n0");
itemsDisplayed.Add(inventory.Container.Items[i],obj);
}
}
}
public void CreateDisplay()
{
for (int i = 0; i < inventory.Container.Items.Count; i++)
{
InventorySlot slot = inventory.Container.Items[i];
var obj = Instantiate(inventoryPrefab,transform);
obj.transform.GetChild(0).GetComponentInChildren<Image>().sprite = inventory.database.GetItem[slot.item.id].uiDisplay;
obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
obj.GetComponentInChildren<Text>().text = slot.amount.ToString("n0");
itemsDisplayed.Add(slot,obj);
}
}
public Vector3 GetPosition(int i)
{
return new Vector3(xStart + (xSpaceBetweenItem * (i % numberOfColumns)),yStart + (-ySpaceBetweenItem * (i / numberOfColumns)),0f);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。