using UnityEngine;
using System.Collections;
using LitJson;
using System.Collections.Generic;
using System.IO;
public class ItemDatabase : MonoBehaviour {
private List database = new List();
private JsonData itemData;
void Start(){
itemData = JsonMapper.ToObject (File.ReadAllText(Application.dataPath + "/StreamingAssets/items.json"));
ConstructItemDatabase();
Debug.Log (FetchItemByID(0).Defence);
}
public Item FetchItemByID(int id){
for (int i = 0; i < database.Count; i++)
if (database *.ID == id)*
_ return database ;_
* return null;*
* }*
* void ConstructItemDatabase(){*
* for (int i = 0; i < itemData.Count; i++) {*
database.Add(new Item((int)itemData[“id”], itemData_[“title”].ToString(), (int)itemData*[“value”],
(int)itemData[“stats”][“power”], (int)itemData[“stats”][“defence”], (int)itemData[“stats”][“vitality”],
itemData[“description”].ToString(), (bool)itemData[“Stackable”], (int)itemData[“rarity”],
itemData[“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 Defence { 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 Item(int id, string title, int value, int power, int defence, int vitality, string description, bool stackable, int rarity, string slug){
this.ID = id;
this.Title = title;
this.Value = value;
this.Power = power;
this.Defence = defence;
this.Vitality = vitality;
this.Description = description;
this.Stackable = stackable;
this.Rarity = rarity;
this.Slug = slug;
}
public Item(){
this.ID = -1;
}
}
this is json data
[
{
“id”: 0,
“title”: “Steel Gloves”,
“value”: 6,
“stats”: {
“power”: 1,
“defence”: 4,
“vitality”: 2*
* },
“description”: “gloves of steel… yay”,
“stackable”: false,
“rarity”: 6,_
“slug”: “Steel_gloves”
_ },
{
“id”: 1,
“title”: “The Great Stick”,
“value”: 5,
“stats”: {
“power”: 2,
“defence”: 2,
“vitality”: 2*
* },
“description”: “a stick that great”,
“stackable”: false,
“rarity”: 1,_
“slug”: “the_great_stick”
_ }
]*
i have no idea what is wrong with it. i am using unity 5_