原始库存数据

Primitive inventory data

我一直在为一款 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不起作用

除此之外,我认为您可以很快看出游戏拥有的项目越多,时间就会越长。

我一直在想办法进一步淡化它,比如为每个我想不到的项目槽都有一个具有自己功能的脚本。

我在想也许,有一个循环遍历数组的小函数,检查第一个带有 "freeSlot" 标签的项目是什么,然后将项目放在那个插槽中,依此类推。

但主要问题确实是 else 和脚本底部没有触发。

我建议稍微重新设计一下。首先,我会为插槽制作一个 class。我更喜欢数据对象 class(但它也可以是 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);
    }
}

有了这个,您可能不必经常重复自己。 希望你明白了。