【问题标题】:Unity - How to save and load lists (Binary Saving and Loading)Unity - 如何保存和加载列表(二进制保存和加载)
【发布时间】:2021-02-13 03:00:56
【问题描述】:

我正试图弄清楚您将如何保存和加载数据列表。我已经让我的代码可以处理单个数据,但不确定列表如何工作。我有一个 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 和法力等列表。我只是不知道该列表如何与序列化一起使用保存和加载。我的代码可能需要一些更改才能使列表正常工作,所以我希望有人可以帮助我举个例子。

【问题讨论】:

  • 请使用正确的标签...unityscript 是或更好曾经是一种 JavaScript 风格,类似于早期 Unity 版本中使用的自定义语言,现在早已弃用...你的代码显然是c#

标签: c# unity3d serialization deserialization


【解决方案1】:

使用 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);

【讨论】:

  • Json 是我目前所见的不安全的保存类型。人们可以通过编辑文本来编辑他们的保存文件。
  • Kira,二进制文件也是不安全的,因为每个人都可以编辑任何文件,就像 JSON afaik 一样简单
  • 我对两者都进行了测试,使用 JSON,您可以清楚地看出所显示的值是什么。但是对于二进制,如果你用记事本打开你会看到奇怪的符号和像 PartyData 这样的类名和来自 stat 的像 hp 这样的词,但你看不到任何值。它可能并不完美,我确信有办法解决它,但与二进制相比,JSON 仍然过于简单和不安全。
  • 我读到如果你确实使用 JSON,最好的做法是稍微加密一下。然后,您必须将其转换为字节。我只是不知道任何显示保存为 JSON 然后转换为字节的示例。但无论如何,如果我要使用 JSON,那么我将不得不这样做,转换为字节,我真的不知道该怎么做。
  • 再次检查答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 2016-12-22
相关资源
最近更新 更多