在 Unity 中保存/加载数据 [英] Saving/loading data in Unity
问题描述
我一直在忙于在 Unity 中保存和加载,我将序列化的类保存到文件中.我有一个可序列化的类:
I have been messing around with saving and loading in Unity in which I save a serialized class to a file. I have a Serializable class :
[Serializable]
class Save
{
public List<int> ID = new List<int>();
public List<int> Amounts = new List<int>();
}
并将其保存到文件 A-OK.我可以毫无错误地加载它,但如果我想稍后添加:
and save it to a file A-OK. I can load it with no errors but if I wanted to add later :
[Serializable]
class Save
{
public List<int> ID = new List<int>();
public List<int> Amounts = new List<int>();
public int extra = 0;
}
并且我运行我的脚本它给了我一个反序列化错误我完全理解当我将反序列化文件转换为我的新类已保存"时,我添加的新变量不存在,它给了我错误.
and I run my scripts it gives me a deserialization error which I completely understand as when I cast the deserialized file to my new class 'Saved' the new variable I added doesn't exist and it gives me the error.
我在商店中修复资产时发现了这个错误,我知道一个修复方法是更改文件名以便创建一个新文件,但我不只是想擦除之前保存的内容.
I found this error when I was fixing an Asset in the store and I know one fix can be just change the filename so a new file is created but I don't just want to wipe the contents of what was saved before.
所以我的问题是,如果我想向我的序列化类添加更多变量,如果人们要更新资产,我将如何捕捉并适应它的旧版本?
So my question is, if I wanted to add more variables to my serialized class how would I catch and adjust to old versions of it if people were to update the asset?
谢谢!
推荐答案
这个问题在使用 C# 序列化程序时是已知的.使用 JsonUtility
将数据转换为 Json,然后使用 PlayerPrefs
保存它.加载时,使用 PlayerPrefs
加载,然后使用 JsonUtility
将 json 转换回类.
This problem is known when using C# serializer. Convert the data to Json with JsonUtility
then save it with the PlayerPrefs
. When loading, load with the PlayerPrefs
then convert the json back to class with JsonUtility
.
要保存的示例类:
[Serializable]
public class Save
{
public List<int> ID = new List<int>();
public List<int> Amounts = new List<int>();
public int extra = 0;
public float highScore = 0;
}
保存数据:
void Save()
{
Save saveData = new Save();
saveData.extra = 99;
saveData.highScore = 40;
//Convert to Json
string jsonData = JsonUtility.ToJson(saveData);
//Save Json string
PlayerPrefs.SetString("MySettings", jsonData);
PlayerPrefs.Save();
}
加载数据:
void Load()
{
//Load saved Json
string jsonData = PlayerPrefs.GetString("MySettings");
//Convert to Class
Save loadedData = JsonUtility.FromJson<Save>(jsonData);
//Display saved data
Debug.Log("Extra: " + loadedData.extra);
Debug.Log("High Score: " + loadedData.highScore);
for (int i = 0; i < loadedData.ID.Count; i++)
{
Debug.Log("ID: " + loadedData.ID[i]);
}
for (int i = 0; i < loadedData.Amounts.Count; i++)
{
Debug.Log("Amounts: " + loadedData.Amounts[i]);
}
}
JsonUtility.FromJson
和 JsonUtility.FromJsonOverwrite
的区别:
A.JsonUtility.FromJson
从 Json 创建新对象并返回它.它分配内存.
A.JsonUtility.FromJson
creates new Object from Json and returns it. It allocates memory.
string jsonData = PlayerPrefs.GetString("MySettings");
//Convert to Class. FromJson creates new Save instance
Save loadedData = JsonUtility.FromJson<Save>(jsonData);
B.JsonUtility.FromJsonOverwrite
不会不创建新对象.它与向类中添加更多数据类型无关.它只是覆盖传入其中的数据.它有利于节省内存和减少 GC.如果您有 array
、string
和 List
等字段,它会分配内存的唯一时间.
B.JsonUtility.FromJsonOverwrite
does not create new Object. It has nothing to do with adding more datatype to your class. It just overwrite the data that is passed in it. It's good for memory conservation and less GC. The only time it will allocate memory if when you have fields such as array
, string
and List
.
应该使用 JsonUtility.FromJsonOverwrite
的示例是在使用 Json 执行常量数据传输时.它将提高性能.
Example of where JsonUtility.FromJsonOverwrite
should be used is when performing constant data transfer with Json. It will improve performance.
//Create Save instance **once** in the Start or Awake function
Save loadedData = null;
void Start()
{
//loadedData instance is created once
loadedData = new Save();
}
void Load()
{
string jsonData = PlayerPrefs.GetString("MySettings");
//Convert to Class but don't create new Save Object. Re-use loadedData and overwrite old data in it
JsonUtility.FromJsonOverwrite(jsonData, loadedData);
Debug.Log("High Score: " + loadedData.highScore);
}
这篇关于在 Unity 中保存/加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!