- Home /
Question by
baybarss · Apr 05, 2021 at 04:43 PM ·
serializationsaveloadbinaryformatterbinary
SerializationException: End of Stream encountered before parsing was completed.
When I try to save and load this error shows. I tried to delete "playerEdu" line and its work great. I think "int" and "string" doesnt work together. Any idea?
using UnityEngine;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class SaveLoadSystem
{
public static void Save()
{
BinaryFormatter bf = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.bin";
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
bf.Serialize(fs, SaveData.playerMoney);
bf.Serialize(fs, SaveData.playerBankMoney);
bf.Serialize(fs, SaveData.playerHealth);
bf.Serialize(fs, SaveData.playerHunger);
bf.Serialize(fs, SaveData.playerDay);
bf.Serialize(fs, SaveData.playerYear);
bf.Serialize(fs, SaveData.playerEdu);
fs.Close();
}
}
public static void Load()
{
if (!File.Exists("/player.bin"))
return;
BinaryFormatter bf = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.bin";
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
SaveData.playerMoney = (int)bf.Deserialize(fs);
SaveData.playerBankMoney = (int)bf.Deserialize(fs);
SaveData.playerHealth = (int)bf.Deserialize(fs);
SaveData.playerHunger = (int)bf.Deserialize(fs);
SaveData.playerDay = (int)bf.Deserialize(fs);
SaveData.playerYear = (int)bf.Deserialize(fs);
SaveData.playerEdu = (string)bf.Deserialize(fs);
fs.Close();
}
}
}
[System.Serializable]
public class SaveData
{
public static int playerMoney;
public static int playerBankMoney;
public static int playerHealth;
public static int playerHunger;
public static int playerDay;
public static int playerYear;
public static string playerEdu;
}
Comment