簡體   English   中英

Unity - 如何保存和加載列表(二進制保存和加載)

[英]Unity - How to save and load lists (Binary Saving and Loading)

我試圖弄清楚你將如何保存和加載數據列表。 我已經讓我的代碼可以處理單個數據,但不確定列表如何工作。 我有一個角色 class 這意味着我可以有多個角色,我想保存每個角色的 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 class 是我用來保存字符數據的。

[System.Serializable]
public class PartyData
{
    public int hp;

    public PartyData(Character cParty)
    {
        hp = cParty.HP;       
    }
}

包含統計信息等的字符 class

public class Character
{
   public int HP { get; set; }

   public int Mana { get; set; }
}

最后,我有附加到游戲對象的 CharacterParty class,這個 class 包含多個字符並調用保存和加載函數:

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 class - 這是 Unity 用於 JSON 的內置實用程序。

此外,您可以在沒有 [System.Serializable] 的情況下序列化任何 class

節省:

// 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM