我一直在为一款RPG游戏构建一个非常基本的库存,我正在尝试作为一个小项目。
库存工作正常,唯一真正的问题是,一旦玩家装满袋子,脚本就会继续尝试收集更多的东西。
我遇到的另一个问题是修改代码,使其工作得更优雅。
这是完整的脚本,很长
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class inventoryRedo : MonoBehaviour
{
public GameObject fullWarning;
public Sprite coalSprite;
public Sprite manaPotionSprite;
public Sprite healthPotionSprite;
public Sprite shrimpSprite;
private bool isPickingUp = false;
public Button[] itemSlots;
private bool isFull = false;
private int itemNum = 0;
void Update()
{
if (playerPickUp.hasPickedUpDroppedCoal == true)
{
itemNum = 1;
if (itemNum == 1)
{
CheckForFreeSlot();
playerPickUp.hasPickedUpDroppedCoal = false;
}
}
if (playerPickUp.hasPickedUpHealthPot == true)
{
itemNum = 2;
if (itemNum == 2)
{
CheckForFreeSlot();
playerPickUp.hasPickedUpHealthPot = false;
}
}
if (playerPickUp.hasPickedUpManaPot == true)
{
itemNum = 3;
if (itemNum == 3)
{
CheckForFreeSlot();
playerPickUp.hasPickedUpManaPot = false;
}
}
}
void CheckForFreeSlot()
{
if (isFull == false)
{
if (itemSlots[0].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[0].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[0].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[0].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[0].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[0].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[0].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[1].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[1].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[1].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[1].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[1].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[1].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[1].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[2].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[2].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[2].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[2].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[2].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[2].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[2].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[3].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[3].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[3].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[3].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[3].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[3].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[3].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[4].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[4].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[4].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[4].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[4].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[4].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[4].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[5].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[5].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[5].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[5].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[5].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[5].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[5].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[6].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[6].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[6].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[6].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[6].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[6].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[6].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[7].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[7].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[7].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[7].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[7].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[7].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[7].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[8].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[8].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[8].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[8].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[8].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[8].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[8].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[9].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[9].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[9].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[9].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[9].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[9].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[9].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[10].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[10].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[10].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[10].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[10].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[10].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[10].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[11].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[11].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[11].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[11].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[11].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[11].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[11].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[12].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[12].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[12].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[12].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[12].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[12].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[12].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[13].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[13].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[13].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[13].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[13].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[13].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[13].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[14].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[14].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[14].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[14].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[14].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[14].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[14].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[15].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[15].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[15].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[15].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[15].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[15].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[15].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}
else if (itemSlots[16].transform.tag == "slotFree")
{
if (itemNum == 1)
{
itemSlots[16].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[16].gameObject.GetComponent<Image>().sprite = coalSprite;
itemNum = 0;
}
else if (itemNum == 2)
{
itemSlots[16].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[16].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
itemNum = 0;
}
else if (itemNum == 3)
{
itemSlots[16].transform.tag = "slotTaken";
Sprite slotOne = itemSlots[16].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
itemNum = 0;
}
}else
{
print("No Room");
}
}
}
}
第一个问题是底部的其他部分不起作用。
除此之外,我想从中你可以很快看到,游戏中的物品越多,这会变得很长。
我一直在想一种方法来进一步淡化这一点,比如为我想不出的每个项目槽都有一个有自己功能的脚本。
我在想,也许有一个小函数,它可以循环通过一个数组,检查第一个标签为"freeSlot"的项目是什么,并将该项目放入该插槽,等等
但真正的主要问题是剧本的其他部分和底部没有开火。
我建议重新设计一下。首先,我会为老虎机上课。我更喜欢一个数据对象类(但它也可以是MonoBehavior):
public class CSlot
{
public GameObject item;
...
}
然后制作一个选择大小的数组:
public class inventoryRedo : MonoBehaviour
{
public CSlot[] inventory;
private int FindIndexOfNextFreeSlot()
{
int nIndex = -1;
for (int i=0; i < inventory.Length; i++)
{
if (inventory[i].item == null)
{
nIndex = i;
}
}
return nIndex;
}
private void PutIntoInventory(int nIndex, GameObject gameObject)
{
inventory[nIndex].item == gameObject;
}
}
然后我会做一些更改为playerPickUp
,这样你就可以做:
if (playerPickUp.PickedUpGameObject != null)
{
int nIndex = FindIndexOfNextFreeSlot();
if (nIndex > -1)
{
PutIntoInventory(nIndex, playerPickUp.PickedUpGameObject);
}
}
有了这个,你可能就不必那么频繁地重复自己了。希望你明白。