我一直在为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");
}
}
}
}
第一个问题是底部的else不起作用。
除此之外,我认为你可以很快看到游戏所拥有的物品会越来越长。
我一直试图想出一种方法来更好地解决这个问题,有一种脚本具有自己的功能,每个项目插槽我都想不到一个。
我想的可能是,有一个小函数循环遍历一个数组,该数组检查第一个项目是什么,标签是&#34; freeSlot&#34;并将项目放在那个插槽中等等。
但主要问题是脚本的else和底部不会触发。
答案 0 :(得分:3)
我建议稍微重新设计一下。首先,我会为老虎机做一个班级。我更喜欢数据对象类(但也可能是MonoBehaviour):
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);
}
}
有了这个,你可能不必经常重复自己。 希望你明白这一点。