Я пытаюсь сохранить некоторые элементы в виде файла JSON и извлечь их во время выполнения.Я пытаюсь создать список объектов ItemDescription.Но список возвращается пустым.Я не получаю никакой ошибки, но когда я поставил точку останова, я увидел, что listOfItems
пусто после listOfItems = JsonUtility.FromJson<ItemList>(jsonString);
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class ItemDictionary : MonoBehaviour
{
private Dictionary<ushort, ItemDescription> itemDictionary = new Dictionary<ushort, ItemDescription>();
private ItemList listOfItems;
private string path;
void Start()
{
path = Application.streamingAssetsPath + "/itemDescription.json";
if (File.Exists(path))
{
string jsonString = File.ReadAllText(path);
listOfItems = JsonUtility.FromJson<ItemList>(jsonString);
//Populate dictionary via JSON
foreach (ItemDescription item in listOfItems.itemList)
{
Debug.Log(item.itemName);
//itemDictionary.Add(item.id, item);
}
}
else
{
Debug.LogError("ITEMDESCRIPTION FILE CANNOT BE FOUNDD");
}
}
}
[System.Serializable]
public class ItemList
{
public List<ItemDescription> itemList;
}
[System.Serializable]
public class ItemDescription
{
public string itemName;
public ushort id;
public string description;
public ushort stack;
public uint cost;
}
JSON FILE
{
"Items": [
{
"itemName": "Regular Dirt",
"id": 1,
"description": "Dirty dirt",
"stack": 500,
"cost": 0
},
{
"itemName": "Regular Stone",
"id": 2,
"description": "It's hard",
"stack": 500,
"cost": 0
},
{
"itemName": "Regular Sand",
"id": 4,
"description": "It gets everywhere.",
"stack": 500,
"cost": 0
}
]
}
Я уже пробовалпереключение всех типов в строки, переименование класса ItemDescription
в Items
, ItemList
класса в Items
.