我在Unity 5中使用JSON和LitJSON来填充游戏的库存用户界面。问题是,我的代码除了来自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),但两个插槽都填充了&#34; stun_gun&#34;。任何帮助将非常感激。我是Unity / C#的新手。
答案 0 :(得分:1)
不应该是
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;
}