-1

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?

Joe
  • 1
  • 1

1 Answers1

0

I recommend you to install Newtonsoft.Json for Unity package and use this code

using Newtonsoft.Json;


var json = File.ReadAllText(Application.persistentDataPath + "/save"));

SaveGameData data = JsonConvert.DeserializeObject<SaveGameData>(json);

public partial class SaveGameData
{
    [JsonProperty("Items")]
    public List<Item> Items { get; set; }
}

public partial class Item
{
    [JsonProperty("ItemData")]
    public ItemData ItemData { get; set; }

    [JsonProperty("ID")]
    public long Id { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }

    [JsonProperty("Value")]
    public long Value { get; set; }
}

public partial class ItemData : ScriptableObject
{
    [JsonProperty("m_FileID")]
    public long MFileId { get; set; }

    [JsonProperty("m_PathID")]
    public long MPathId { get; set; }
}

of if you want to keep JsonUtility fix the class property names

[Serializable]
public partial class SaveGameData
{
    public List<Item> Items;
}
[Serializable]
public partial class Item
{
    public ItemData ItemData;

    public long ID;
 
    public string Name;

    public long Value;
}
[Serializable]
[CreateAssetMenu(fileName = "ItemData", menuName = "Menu/ItemData")]
public partial class ItemData : ScriptableObject
{
    public long m_FileID;
  
    public long m_PathID;
}
Serge
  • 40,935
  • 4
  • 18
  • 45