c# - Unity - 如何保存和加载列表(二进制保存和加载)
问题描述
我试图弄清楚你将如何保存和加载数据列表。我已经让我的代码可以处理单个数据,但不确定列表如何工作。我有一个 Character 类,这意味着我可以拥有多个角色,并且我想保存每个角色的 hp、mana 等。
//SaveManager class that I can call from anywhere to save and load
public static class SaveManager
{
//SAVE
public static void Save(Character c)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/Save.save";
FileStream stream = new FileStream(path, FileMode.Create);
PartyData data = new PartyData(c);
formatter.Serialize(stream, data);
stream.Close();
}
//LOAD
public static PartyData Load()
{
string path = Application.persistentDataPath + "/Save.save";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PartyData data = formatter.Deserialize(stream) as PartyData;
stream.Close();
return data;
}
else
{
Debug.Log("Save FIle not Found");
return null;
}
}
}
PartyData 类是我用来保存字符数据的类。
[System.Serializable]
public class PartyData
{
public int hp;
public PartyData(Character cParty)
{
hp = cParty.HP;
}
}
包含统计信息等的字符类
public class Character
{
public int HP { get; set; }
public int Mana { get; set; }
}
最后我有一个附加到游戏对象的 CharacterParty 类,这个类包含多个字符并调用 Save 和 Load 函数:
public class CharacterParty : MonoBehaviour
{
[SerializeField] List<Character> characters;
public List<Character> Characters
{
get
{
return characters;
}
}
public void SaveParty()
{
SaveManager.SaveMC(characters[0]);
}
public void LoadParty()
{
PartyData data = SaveManager.LoadMC();
characters[0].HP = data.hp;
}
}
现在出于测试目的,我尝试仅在索引 0 处保存和加载角色的 hp,它可以工作,但现在我想保存多个角色的 hp 和法力等列表。我只是不知道该列表如何与序列化保存和加载一起使用. 我的代码可能需要一些更改才能使列表正常工作,所以我希望有人可以帮助我举个例子。
解决方案
使用JsonUtility类 - 这是 Unity 内置的 JSON 实用程序。
同样有了这个,您可以序列化任何没有 [System.Serializable] 的类
节省:
// Parties list
List<PartyData> parties = new List<PartyData>();
// Add your parties here
// First argument is an instance of a class or any other object
// If second argument set to true, it makes JSON more human-readable
string json = JsonUtility.ToJson(parties, false);
// .. saving the file
加载:
// .. loading the file
// First argument is the JSON
List<PartyData> parties = JsonUtility.FromJson<List<PartyData>>(json);
// .. do stuff with the list
编辑:
如果你真的想使用 BinaryFormatter,那么这是为了保存:
List<PartyData> parties = new List<PartyData>();
// .. add parties
formatter.Serialize(stream, parties)
加载:
List<PartyData> parties = formatter.Deserialize(stream) as List<PartyData>;
// .. do stuff
第二次编辑:
这个应该可以的。你会做这样的事情:
// Save class
[Serializable]
public class Save
{
public List<PartyData> parties;
// .. add other stuff here
}
// For loading
try
{
// Deserializing the save
Save save = (Save)formatter.Deserialize(stream);
// .. do stuff with other data if you added some
foreach (PartyData data in save.parties)
{
// .. do stuff with data
}
}
catch (Exception e)
{
// .. can you just acnowledge... E
// Oopsie! An error occured. The save file is probably COWWUPTED
Debug.LogWarning($"An error occured while loading the file: {e}");
}
// For saving
List<PartyData> parties = new List<PartyData>();
// .. add parties by calling parties.Add(party_here);
// Creating a save
Save save = new Save();
// Adding parties to save
save.parties = parties;
// Serialize to a file
formatter.Serialize(stream, parties);
推荐阅读
- android - 如何在 ListView 中使用 EditText
- azure - Azure Functions:部署到暂存槽重新启动生产槽
- javascript - 如何让 VS Code 终端运行 Ubuntu Mac?
- iframe - 如何在 index.html 处显示 footer.html?
- flutter - 应用程序终止时如何打开对话框 - Flutter?
- javascript - Chart.js 厚标签被三个点替换
- sql - 使用 Oracle 合并语句合并组中的特定行
- amazon-web-services - Lambda 无权执行:dynamodb:GetItem
- javascript - React HOC 是纯函数吗?
- google-cloud-platform - 每当 GCP 存储桶被更新时,都会触发或通知 Angular