I'm trying to deserialize my json items from a file using UnityEngine.JsonUtility
. It works fine but my enum types are not getting properly converted. I tried using the EnumMember
attribute but still had no luck.
How can I fix that ?
Note
I'm using this solution to read multiple files and store them in array.
[Serializable]
public class EquipementItem
{
public enum ItemTypes
{
None,
Armor,
Weapon
}
public enum SlotTypes
{
Head,
Shoulders,
Chest,
Bracers,
Gloves,
Waist,
Legs,
Boots,
Weapon
}
public int ID;
public string Name;
public ItemTypes ItemType;
public SlotTypes SlotType;
}
And the json file
{
"Items": [
{
"ID": "1",
"Name": "Basic Sword",
"ItemType": "Weapon",
"SlotType": "Weapon"
},
{
"ID": "2",
"Name": "Advanced Sword",
"ItemType": "Weapon",
"SlotType": "Weapon"
},
{
"ID": "3",
"Name": "Leather Chest",
"ItemType": "Armor",
"SlotType": "Chest"
}
]}
This is the class where I load the json file:
public class Items : MonoBehaviour
{
public static EquipementItem[] EquipableItems;
private void Awake()
{
string jsonFile = File.ReadAllText(Application.dataPath + "/Scripts/Databases/EquipableItemsDB.json");
EquipableItems = JsonHelper.FromJson<EquipementItem>(jsonFile);
}
}