0
我在Unity 5中使用JSON和LitJSON來填充遊戲的廣告資源UI。問題是,我的代碼沒有返回任何東西,除了來自id:0項目的信息。在這一點上,我只有兩個項目在我的JSON。我在Unity中的代碼會認識到,只有id:0和id:1的項目,因爲如果我在其中放入其他內容,我會在參數外獲得一個錯誤,但它仍然只將信息打印到控制檯的id: 0。使用JSON填充廣告資源界面,我的代碼出了什麼問題?
這裏是我的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#的新手。
非常感謝! :)這解決了它的權利。 – SailingonBroats