Есть класс
Classusing UnityEngine;
using System;
using System.Runtime.Serialization;
[Serializable]
public class Item : MonoBehaviour{
public string Name;
[Multiline(5)]
public string Describe;
public int Count = 1;
public bool isStacktable;
public string HandPrefab;
public string WorldPrefab;
public string icon;
public int Slot;
}
Пытаюсь сериализовать получается но обратно загрузить нет
Вот код
public void Load()
{
string Filepath = Path.Combine(Application.dataPath + "/Inventory");
int fileCount = Directory.GetFiles(Filepath).Length;
path = Path.Combine(Application.dataPath + "/Inventory", "Item0" + ".json");
string json = File.ReadAllText(path);
Debug.Log(json);
Item it = JsonUtility.FromJson<Item>(json);
list.Add(it);
Debug.Log("Loading suc");
}
private void Save()
{
string Filepath = Path.Combine(Application.dataPath + "/Inventory");
if (Directory.Exists(Filepath))
{
Item it = list[i];
string json = JsonUtility.ToJson(it);
path = Path.Combine(Application.dataPath + "/Inventory", "Item0" + ".json");
File.WriteAllText(path, json);
}
}
Вот ошибка
ArgumentException: Cannot deserialize JSON to new instances of type 'Item.'
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:40)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:30)
Inventory.Load () (at Assets/Scripts/Inventory.cs:128)
Inventory.Start () (at Assets/Scripts/Inventory.cs:18)
Вот выходной json:
{"Name":"Cube","Describe":"Good very good cube","Count":1,"isStacktable":true,"HandPrefab":"prefabs/Cube","WorldPrefab":"prefabs/Cube","icon":"icons/2","Slot":0}