I am pretty new to Unity and c# and I was trying to save a list of the struct that contains attributes including a Scriptable Object using JsonUtility; however after serialization when I check the JSON file and it did not seem to record the attributes of the Scriptable Objects but only m_FileID and m_PathID, and after when deserializing the data it only returns null
Serialized file:
"Items":[ { "ItemData":{ "m_FileID": -794, "m_PathID": 0 }, "ID":1, "Name": "Cube", "Value": 140.0 } ]
Here is my code:
using System;
using System.Collections.Generic;
using System.IO;
[Serializable]
public class Manager{
public List<ItemInfo> Items;
public void Save()
{
string path = Application.persistentDataPath;
if (!Directory. path))
{
Directory.CreateDirectory(path);
}
string text = JsonUtility.ToJson(new SaveGameData
{
Items = this.Items
}, true);
File.WriteAllText(path + "/save", text);
}
public void Load()
{
SaveGameData data = JsonUtility.FromJson<SaveGameData>(File.ReadAllText(Application.persistentDataPath + "/save"));
this.Items = data.Items;
}
}
SaveGameData class:
using System;
using System.Collections.Generic;
[Serializable]
public class SaveGameData
{
public List<ItemInfo> ShopItems;
}
using System;
[Serializable]
public struct ItemInfo
{
public ItemData Data;
public uint ID;
public string Name;
public float Value;
}
using System;
using UnityEngine;
[CreateAssetMenu(fileName = "ItemData", menuName = "Menu/ItemData")]
public class ItemData: ScriptableObject
{
//getters and setters
public string description;
public int level;
public string rarity;
}
It seems to have something to do with the scriptable object...is there any way I can also serialize it just like the other object?