Skip to main content
public class InventoryItemDataBase : MonoBehaviour

{ public allitemData itemsData = new allitemData(); // Start is called before the first frame update void Start() {

{
    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 = new List(); }

[System.Serializable] public class ItemBase { public int id, Value; public string name, description; public static string path; public static Sprite Icon = Resources.Load(path); public Sprite sprite = Icon;

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"
    }]
}

}

&& json Data

{ "ItemBase": [ { "id": "0", "Value": "7", "name": "Teddy Bear", "description": "Teddy", "path": "IconS/Teddy/" }, { "id": "1", "Value": "5", "name": "UFO", "description": "ufoos" }] }

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 = new List(); }

[System.Serializable] public class ItemBase { public int id, Value; public string name, description; public static string path; public static Sprite Icon = Resources.Load(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" }] }

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"
    }]
}
Source Link
Times
  • 11
  • 2

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

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 = new List(); }

[System.Serializable] public class ItemBase { public int id, Value; public string name, description; public static string path; public static Sprite Icon = Resources.Load(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" }] }

enter image description here

enter image description here

enter image description here