我想将多个不相关ScriptableObjects
的对象保存到一个保存文件中。这样,我可以保存/加载我可能想要跟踪的任何数据。IE。门解锁,库存,高分,找到宝箱等。这样我仍然可以使用ScriptableObjects
,保存和/或重新加载任何受DataManager
. 我只想DataManager
保存并重新加载ScriptableObjects
他们保存的数据。
ScriptableObjects
在我看来,将不同的文件保存到一个文件似乎可以正常工作。如果我打开data_editor.sav
文件,我可以看到数据已经序列化并放入文件中。
{"serializedObjects":["{\"GUID\":\"e27e7e59111210141bb70cbdff58f4f6\",\"Prefab\":{\"instanceID\":3648}}","{\"GUID\":\"52abd33f0dd972246b5eaa87a938331e\",\"Prefab\":{\"instanceID\":3190}}","{\"GUID\":\"78d783846b8abb3488ee1e274773aec6\",\"Prefab\":{\"instanceID\":3080}}"]}
但是,读回数据并尝试重新填充 myDataManager
的managedData
属性是不断破坏的原因。我一生都无法弄清楚如何将 JSON 字符串反序列化回ScriptableObjects
.
我得到错误:
ArgumentException:无法将 JSON 反序列化为“ScriptableObject”类型的新实例。
我已经查看了几个 SO 问题,包括这个问题,但我似乎找不到任何可以将多个不同的ScriptableObjects
问题合二为一的方法。
有什么帮助吗?
我已经尝试过各种变化JsonUtility.FromJsonOverwrite
,JsonUtility.FromJson<ScriptableObject>
但我无法让它发挥作用。
DataStore 这是写入磁盘的类。
[Serializable]
public class DataStore : ISerializationCallbackReceiver
{
public List<string> serializedObjects;
public DataStore()
{
serializedObjects = new List<string>();
}
#region ISerializationCallbackReceiver_Methods
/// <summary>
/// Serialization implementation from ISerializationCallbackReceiver
/// </summary>
public void OnBeforeSerialize() {}
/// <summary>
/// Deserialization implementation from ISerializationCallbackReceiver
/// </summary>
public void OnAfterDeserialize()
{
Debug.Log("OnAfterDeserialize Loaded Count: " + serializedObjects.Count);
foreach (string so in serializedObjects)
{
Debug.Log(so);
}
}
#endregion
public void AddSerializedObjects(List<ScriptableObject> scriptableObjects)
{
foreach(ScriptableObject so in scriptableObjects)
{
serializedObjects.Add(JsonUtility.ToJson(so));
}
}
public List<ScriptableObject> GetStoredSerializedObjects()
{
//return JsonUtility.FromJsonOverwrite(serializedObjects.ToString(), new List<ScriptableObject>());
List<ScriptableObject> temp = new List<ScriptableObject>();
foreach (string so in serializedObjects)
{
//ScriptableObject file = ScriptableObject.CreateInstance(JsonUtility.FromJson<ScriptableObject>(so));
var json = JsonUtility.FromJson<ScriptableObject>(so);
//JsonUtility.FromJsonOverwrite(so, stemp);
ScriptableObject file = ScriptableObject.CreateInstance((ScriptableObject)json);
temp.Add(file);
}
return temp;
}
}
从保存文件中读回数据后,我可以输出查看 SO 的数据。
这是 SO 的外观
数据管理器
public class DataManager : MonoBehaviour
{
public List<ScriptableObject> managedData = new List<ScriptableObject>();
private DataStore m_Data;
[NonSerialized]
private IDataSaver m_Saver;
private void Awake()
{
m_Saver = new JsonSaver();
m_Data = new DataStore();
LoadData();
}
public void SaveData()
{
m_Data.AddSerializedObjects(managedData);
m_Saver.Save(m_Data);
}
public void LoadData()
{
Debug.Log("LoadData()");
m_Saver.Load(m_Data);
//Here is where I am trying to put the scriptable objects back
//managedData.Clear();
//managedData.AddRange(m_Data.GetStoredSerializedObjects()));
//managedData = m_Data.GetStoredSerializedObjects();
}
}
IDataSaver
public interface IDataSaver {
void Save(DataStore data);
bool Load(DataStore data);
}
JsonSaver
public class JsonSaver : IDataSaver {
private static readonly string s_Filename = "data_editor.sav";
public static string GetSaveFilename()
{
return string.Format("{0}/{1}", Application.persistentDataPath, s_Filename);
}
protected virtual StreamWriter GetWriteStream() {
return new StreamWriter(new FileStream(GetSaveFilename(), FileMode.Create));
}
protected virtual StreamReader GetReadStream() {
return new StreamReader(new FileStream(GetSaveFilename(), FileMode.Open));
}
public void Save(DataStore data) {
string json = JsonUtility.ToJson(data);
using (StreamWriter writer = GetWriteStream()) {
writer.Write(json);
}
}
public bool Load(DataStore data) {
string loadFilename = GetSaveFilename();
if (File.Exists(loadFilename)) {
using (StreamReader reader = GetReadStream()) {
JsonUtility.FromJsonOverwrite(reader.ReadToEnd(), data);
}
return true;
}
return false;
}
}