evershade/Assets/Scripts/Managers/BinarySaveManager.cs

35 lines
894 B
C#

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
[Serializable]
public class GameData
{
public string playerName;
public int score;
}
public class BinarySaveManager : MonoBehaviour
{
public GameData gameData;
public void SaveData()
{
var bf = new BinaryFormatter();
var file = File.Create(Application.persistentDataPath + "/gameData.dat");
bf.Serialize(file, gameData);
file.Close();
}
public void LoadData()
{
if (File.Exists(Application.persistentDataPath + "/gameData.dat"))
{
var bf = new BinaryFormatter();
var file = File.Open(Application.persistentDataPath + "/gameData.dat", FileMode.Open);
gameData = (GameData)bf.Deserialize(file);
file.Close();
}
}
}