i am trying to practice on loading data from json files my problem is about when i try to load the sprite path from the json file to load the image icon from the floder that contain it, i manged to do that but by hard code the path but when i try to use variable to hold the path its failed to load any one can help me in that please
i post the code here
public class InventoryItemDataBase : MonoBehaviour
{
public allitemData itemsData = new allitemData();
// Start is called before the first frame update
void Start()
{
TextAsset asset = Resources.Load("ItemData/ItemDataBase") as TextAsset;
itemsData = JsonUtility.FromJson<allitemData>(asset.text);
}
public ItemBase itembyid(int id)
{
for (int i = 0; i < itemsData.ItemBase.Count; i++)
{
if (itemsData.ItemBase[i].id == id)
return itemsData.ItemBase[i];
}
return null;
}
}
[System.Serializable]
public class allitemData
{
public List<ItemBase> ItemBase = new List<ItemBase>();
}
[System.Serializable]
public class ItemBase
{
public int id, Value;
public string name, description;
public static string path;
public static Sprite Icon = Resources.Load<Sprite>(path);
public Sprite sprite = Icon;
public ItemBase()
{
this.id = -1;
}
}
&& json Data
{
"ItemBase": [
{
"id": "0",
"Value": "7",
"name": "Teddy Bear",
"description": "Teddy",
"path": "IconS/Teddy/"
},
{
"id": "1",
"Value": "5",
"name": "UFO",
"description": "ufoos"
}]
}
allitemData
is a terrible name for a class. First, it should have an initial capital letter to distinguish it from a variable name,AllitemData
. Second, you don't need it. You can serialize and deserialize lists directly,JsonUtility.FromJson<List<ItemBase>>(asset.text);
\$\endgroup\$