You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RatAttack2D/Assets/Scripts/SaveLoad.cs

36 lines
806 B

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
// Example class to save and load
[Serializable]
class PlayerData
{
public int score;
public string name;
}
class SaveLoad
{
// Save method
public void Save(PlayerData data, string fileName)
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream(fileName, FileMode.Create))
{
formatter.Serialize(stream, data);
}
}
// Load method
public PlayerData Load(string fileName)
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
return (PlayerData)formatter.Deserialize(stream);
}
}
}