用JSON填充库存UI,我的代码有什么问题?

本文关键字:代码 什么 问题 我的 填充 JSON UI | 更新日期: 2023-09-27 18:06:53

我在Unity 5中使用JSON和LitJSON来填充游戏的库存UI。问题是,我的代码除了id:0的项目信息之外没有返回任何东西。此时,我的JSON中只有两项。我在Unity中的代码将识别出只有id:0和id:1的项目,因为如果我在那里放入任何其他内容,我将在参数之外得到一个错误,但它仍然只将id:0的信息打印到控制台。

这是我的JSON:
    [
    {
        "id": 0,
        "title": "Stun Gun",
        "value": 6,
        "stats": {
            "power": 100,
            "defense": 4,
            "vitality": 2
        },
        "description": "Testing the Stun Gun.",
        "stackable": false,
        "rarity": 2,
        "slug": "stun_gun"
    },
    {
    "id": 1,
        "title": "The Great Gun",
        "value": 500,
        "stats": {
            "power": 700,
            "defense": 10,
            "vitality": 10
        },
        "description": "The Great Gun is great.",
        "stackable": true,
        "rarity": 6,
        "slug": "the_great_gun"
    }
]

这是我的库存脚本:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
    GameObject inventoryPanel;
    GameObject slotPanel;
    ItemDatabase database;
    public GameObject inventorySlot;
    public GameObject inventoryItem;
    int slotAmount;
    public List<Item> items = new List<Item>();
    public List<GameObject> slots = new List<GameObject>();
    void Start ()
    {
        database = GetComponent<ItemDatabase>();
        slotAmount = 8;
        inventoryPanel = GameObject.Find ("Inventory Panel");
        slotPanel = inventoryPanel.transform.FindChild ("Slot Panel").gameObject;
        for (int i = 0; i < slotAmount; i++) 
        {
            items.Add(new Item());
            slots.Add(Instantiate(inventorySlot));
            slots[i].transform.SetParent(slotPanel.transform);
        }
        AddItem(0);
        AddItem(1);
        Debug.Log(items[1].Slug);
    }
    public void AddItem (int id)
    {
        Item itemToAdd = database.FetchItemByID(id);
        for (int i = 0; i < items.Count; i++) 
        {
            if (items[i].ID == -1)
            {
                items[i] = itemToAdd;
                GameObject itemObj = Instantiate(inventoryItem);
                itemObj.transform.SetParent(slots[i].transform);
                itemObj.transform.position = Vector2.zero;
                itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
                itemObj.name = itemToAdd.Title;
                break;
            }
        }
    }
}
下面是我的ItemDatabase脚本:
using UnityEngine;
using System.Collections;
using LitJson;
using System.Collections.Generic;
using System.IO;
public class ItemDatabase : MonoBehaviour {
    private List<Item> database = new List<Item>();
    private JsonData itemData;
    void Start ()
    {
        itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
        ConstructItemDatabase();
        Debug.Log (FetchItemByID(0).Description);
    }
    public Item FetchItemByID (int id)
    {
        for (int i = 0; i < database.Count; i++)
            if(database[id].ID == id)
                return database[i];
        return null;
    }
    void ConstructItemDatabase ()
    {
        for (int i = 0; i < itemData.Count; i++) 
        {
            database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"],
                (int)itemData[i]["stats"]["power"], (int)itemData[i]["stats"]["defense"], (int)itemData[i]["stats"]["vitality"], 
                itemData[i]["description"].ToString(), (bool)itemData[i]["stackable"], (int)itemData[i]["rarity"], 
                itemData[i]["slug"].ToString()));
        }
    }
}
public class Item {
    public int ID { get; set; }
    public string Title { get; set; }
    public int Value { get; set; }
    public int Power { get; set; }
    public int Defense { get; set; }
    public int Vitality { get; set; }
    public string Description { get; set; }
    public bool Stackable { get; set; }
    public int Rarity { get; set; }
    public string Slug { get; set; }
    public Sprite Sprite { get; set; }
    public Item (int id, string title, int value, int power, int defense, int vitality, string description, bool stackable, int rarity, string slug)
    {
        this.ID = id;
        this.Title = title;
        this.Value = value;
        this.Power = power;
        this.Vitality = vitality;
        this.Description = description;
        this.Stackable = stackable;
        this.Rarity = rarity;
        this.Slug = slug;
        this.Sprite = Resources.Load<Sprite>("Sprites/Items/" + slug);
    }
    public Item ()
    {
        this.ID = -1;
    }
}

我得到了正确的插槽数量,以及正确的填充与空插槽数量(2),但两个插槽都填满了"stun_gun"。任何帮助都将非常感激。我是Unity/c#新手

用JSON填充库存UI,我的代码有什么问题?

应该是

public Item FetchItemByID (int id)
{
    for (int i = 0; i < database.Count; i++)
        if(database[i].ID == id)  // i rather than id
            return database[i];
    return null;
}