I have this method that returns an array from JSON
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
And the Wrapper class:
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
This is the JSON file structure:
{
"Tiles":
[
{
"id": "air",
"name": "Air",
"itemID": "air",
"texture": {
"x": 0.0,
"y": 0.0
},
"properties": {
"hardness": 0,
"undestructible": true
}
},
...
]
}
And this Tile class:
[Serializable]
public class Tile {
public string id;
public string name;
public string itemID;
public Vector2 texture;
public Properties properties;
}
[Serializable]
public class Properties {
public double hardness = 0;
public bool undestructible = false;
}
When I run the FromJson method it returns null for the Tile class but for the following JSON structure it works:
{
"Items":
[
{
"id": "dirt",
"name": "Dirt",
"description": "Just some dirt.. might be useful some day."
},
...
]
}