如何反序列化类以添加信息然后保存



好吧,我得到了一个Serializable类,这是由另一个类实例化,但我真正想要做的是首先打开以前保存的文件,并添加一个新元素到列表中,但我得到的只是覆盖以前的信息,然后保存(我不想,所以我从文件模式改变。打开到文件模式。附加)。因此,每次游戏结束时都会调用反序列化类我希望它打开一个hilicores并添加一个新元素

我的问题开始在反序列化

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.UI;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class LeaderboardManager : MonoBehaviour, IPointerDownHandler, IPointerUpHandler  {
    public GameObject leaderboardart;
    public static LeaderboardManager leaderboardmanager;
    void Awake() 
    {
        if (leaderboardmanager == null) 
        {
            DontDestroyOnLoad (gameObject);
            leaderboardmanager = this;
        } else if (leaderboardmanager!= this) {
            Destroy (gameObject);
        }
        if (File.Exists (Application.persistentDataPath + "/highscores.dat")) 
        {
            print("THE HIGHSCORES HAS BEEN CREATED");
        } 
        else 
        {
            PlayerData playdata = new PlayerData();
            FileStream file = File.Create(Application.persistentDataPath +"/highscores.dat");
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(file,playdata);
            file.Close();
        }
    }

    public void  Deserialize(int score) 
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Open(Application.persistentDataPath + "/highscores.dat", FileMode.Append);
        PlayerData playdata = (PlayerData)bf.Deserialize(file);
        playdata.highscorelist.Add (score);
        bf.Serialize (file, playdata);
        file.Close ();
    }
    [Serializable]
    class PlayerData 
    {
        public List<int> highscorelist = new List<int>();
    }
}
错误消息

> ArgumentException: The stream doesn't support reading.
> System.IO.BinaryReader..ctor (System.IO.Stream input,
> System.Text.Encoding encoding) (at
> /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/BinaryReader.cs:68)
> System.IO.BinaryReader..ctor (System.IO.Stream input)
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize
> (System.IO.Stream serializationStream,
> System.Runtime.Remoting.Messaging.HeaderHandler handler) (at
> /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:158)
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
> (System.IO.Stream serializationStream) (at

您使用. append标志打开流,这是一个写模式,因此BinaryFormatter正确地抱怨它无法从仅为写而打开的流中读取。试试这段代码。它首先以读模式打开hilicore文件,然后在更新类中的hilicore之后,以写模式第二次打开以序列化所有数据。

不要使用追加。二进制格式不像CSV格式那样可以附加额外的行。您必须重写整个文件!

        BinaryFormatter bf = new BinaryFormatter();
        PlayerData playdata;
        using (FileStream file = File.Open("highscores.dat", FileMode.Open))
        {
            playerData = (PlayerData) bf.Deserialize(file);
        }
        playdata.highscorelist.Add(score);
        using (FileStream file = File.Open("highscores.dat", FileMode.Create))
        {
            bf.Serialize(file, playdata);
        }

相关内容

  • 没有找到相关文章

最新更新