Save and Load Using BinaryFormatter HELP!!!
So I am trying to make a save and load system using BinaryFormatter/Custom Binary and I am trying to figure out how to save a float and a bool using it, so far I have made a class SaveManager which has all the save and load functions, and then I have script which access the static function in the SaveManger class. Here the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveManager {
public static string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public static void SavePlayer(SavePos player)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream stream = new FileStream(path + "/PlayerPos.txt", FileMode.Create);
PlayerData data = new PlayerData(player);
bf.Serialize(stream, data);
stream.Close();
}
public static float[] LoadPlayer()
{
if (File.Exists(path + "/PlayerPos.txt"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream stream = new FileStream(path + "/PlayerPos.txt", FileMode.Open);
PlayerData data = bf.Deserialize(stream) as PlayerData;
stream.Close();
return data.stats;
}
return null;
}
}
[Serializable]
public class PlayerData
{
public float[] stats;
public PlayerData(SavePos player)
{
stats = new float[3];
stats[0] = SavePos.posX;
stats[1] = SavePos.posY;
stats[2] = SavePos.posZ;
}
}
The Class I am accessing the SaveManager From:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class SavePos : MonoBehaviour {
public static float posX;
public static float posY;
public static float posZ;
public void save()
{
float xx = transform.position.x;
float yy = transform.position.y;
float zz = transform.position.z;
}
public void load()
{
float[] loadedStats = SaveManager.LoadPlayer();
float x = loadedStats[0];
float y = loadedStats[1];
float z = loadedStats[2];
transform.position = new Vector3(x,y,z);
}
}
Answer by agray427 · Jan 15, 2018 at 08:57 PM
I'm not sure why you have all the references set up the way that you do, it seems strange, but this should work fine, though I haven't tested it (but I will if I must):
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public static class SaveManager
{
private static readonly string FILE_PATH = Path.Combine(Application.persistentDataPath, "example.txt");
private static PlayerData m_playerData;
public static void Save()
{
BinaryFormatter bf = new BinaryFormatter();
SavePlayer(bf);
}
private static void SavePlayer (BinaryFormatter bf)
{
if (m_playerData != null)
{
using (FileStream stream = new FileStream(FILE_PATH, FileMode.Open, FileAccess.Read))
{
bf.Serialize(stream, m_playerData);
}
}
}
public static void Load ()
{
BinaryFormatter bf = new BinaryFormatter();
if (!LoadPlayer(bf))
{
m_playerData = new PlayerData();
}
}
private static bool LoadPlayer (BinaryFormatter bf)
{
if (!File.Exists(FILE_PATH))
{
return false;
}
using (FileStream stream = new FileStream(FILE_PATH, FileMode.Open, FileAccess.Read))
{
m_playerData = bf.Deserialize(stream) as PlayerData;
}
return true;
}
[Serializable]
public class PlayerData
{
[SerializeField]
private Pos3D m_position;
}
[Serializable]
public struct Pos3D
{
public static readonly Pos3D zero = new Pos3D(0.0f, 0.0f, 0.0f);
public static readonly Pos3D one = new Pos3D(1.0f, 1.0f, 1.0f);
[SerializeField]
private float m_x;
[SerializeField]
private float m_y;
[SerializeField]
private float m_z;
public float x { get { return m_x; } set { m_x = value; } }
public float y { get { return m_y; } set { m_y = value; } }
public float z { get { return m_z; } set { m_z = value; } }
public Pos3D (float x, float y, float z)
{
m_x = x;
m_y = y;
m_z = z;
}
}
}
Quick side note, I created it this way because it is a Save$$anonymous$$anager. I assumed that there would be more than just the player being created. In that case, you will want more of the specific private methods like SavePlayer and LoadPlayer. Likewise, you will also want to have different file paths. Best of luck!
Your answer
Follow this Question
Related Questions
[Error:] Cannot Deserialize JSON to new instances of type ' X ' 1 Answer
How do i Make this saving system apply the save data in my game. 0 Answers
How to SAVE/LOAD Unity Game in Mac/WebGL? 0 Answers
how to save a lot of character data? 1 Answer
Vroid Opening Incorrect Save,Vroid opens the model above the one I click 0 Answers