- Home /
unity 3d loading data from json problem?!
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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);
Debug.Log(fetishitembyid(0).name);
}
public ItemBase fetishitembyid(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>("IconS/Teddy"); // this way the image load
public static Sprite Icon = Resources.Load<Sprite>(path); // this way the image not load
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"
}]
}
images below
Answer by Bunny83 · Jun 21, 2019 at 05:45 PM
static fields do not belong to any instance and therefore are never serialized or deserialized by pretty much all serialization systems. Either make it an instance field, or if the path should be the same for all instances you may want to move that field into your "allitemData" class instead. Also you should avoid using the field initializers to access any Unity API. Resources.Load calls should be done from a method that is called at the appropriate time.
Either make it an instance field, or if the path should be the same for all instances you may want to move that field into your "allitemData" class ins$$anonymous$$d
can you help me and explain how i did it in the right way pls i dont get how i move the field to allitemdata i create this class to fill the list as i learned from online tutorial
i just wan to get the path from the json file to store the image in the list database;
thank you
you mead me think in other way i was focusing on one way but i mead something foreach in the start method and that solved the problem i just load the sprite Separately here is my final code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryItemDataBase : $$anonymous$$onoBehaviour
{
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);
foreach (var item in itemsData.Items)
{
item.Icon = Resources.Load<Sprite>("IconS/" + item.description);
}
Debug.Log(fetishitembyid(0).name);
}
public ItemBase fetishitembyid(int id)
{
for (int i = 0; i < itemsData.Items.Count; i++)
{
if (itemsData.Items[i].id == id)
return itemsData.Items[i];
}
return null;
}
}
[System.Serializable]
public class allitemData
{
public List<ItemBase> Items = new List<ItemBase>();
}
[System.Serializable]
public class ItemBase
{
public int id, Value;
public string name, description;
public Sprite Icon;
public ItemBase()
{
this.id = -1;
}
}
Your answer
Follow this Question
Related Questions
How to protect JSON file game data? 3 Answers
JsonUtility doesn't serialize nested mixed var 1 Answer
Can we create new variables or properties from the inspector? 0 Answers
Stop specific fields from being serialized by JSON utility 3 Answers
Json Serialization documentation official Unity website - marking it [Serializable] caused error 1 Answer