Hi, I’m trying to write a save/load class through BinaryWriter and BinaryReader. (I hope that method is secure enough.) I successfully saved the file, but I could not load it. Here is the relevant code:
SaveManager.cs:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SaveManager : MonoBehaviour
{
public static string directory = @"\SaveLoad";
public static string fileName = @"\saveFile.sav";
public static string savePath;
public static string saveDirectory;
public static void Save(Player player)
{
saveDirectory = Application.persistentDataPath + directory;
savePath = saveDirectory + fileName;
if (!DirectoryExists())
Directory.CreateDirectory(saveDirectory);
FileStream sr = File.Create(savePath);
BinaryWriter bw = new BinaryWriter(sr);
PlayerData data = PlayerData.FromPlayer(player);
bw.Write(data.Health);
bw.Write(data.Lives);
bw.Write(data.Score);
if (data.Position == null)
{
bw.Write(0);
}
else
{
bw.Write(data.Position.Length);
foreach (var value in data.Position)
{
bw.Write(value);
}
}
sr.Close();
Debug.Log("Your game has been saved.");
}
public static PlayerData Load()
{
saveDirectory = Application.persistentDataPath + directory;
savePath = saveDirectory + fileName;
if (!File.Exists(savePath))
{
Debug.LogError("Save file not found in " + savePath);
return null;
}
FileStream sr = File.OpenRead(savePath);
BinaryReader br = new BinaryReader(sr);
PlayerData data = new PlayerData();
data.Health = br.ReadInt32();
data.Lives = br.ReadInt32();
data.Score = br.ReadInt32();
int length = br.ReadInt32();
data.Position = new float[length];
for (int index = 0; index < length; index++)
{
data.Position[index] = br.ReadSingle();
}
sr.Close();
Debug.Log("Your game has been loaded.");
return data;
}
private static bool SaveExists()
{
return File.Exists(GetFullPath());
}
private static bool DirectoryExists()
{
saveDirectory = Application.persistentDataPath + directory;
return Directory.Exists(saveDirectory);
}
private static string GetFullPath()
{
saveDirectory = Application.persistentDataPath + directory;
savePath = saveDirectory + fileName;
return savePath;
}
}
PlayerData.cs:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PauseScreen : MonoBehaviour
{
public GameObject PauseCanvas;
public static bool GameIsPaused;
public PlayerController ball;
public Player player;
void Start()
{
GameIsPaused = false;
ball = FindObjectOfType<PlayerController>();
player = FindObjectOfType<Player>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Cancel"))
{
if (GameIsPaused)
{
Resume();
}
else
{
Pause();
}
}
}
public void Resume()
{
Time.timeScale = 1;
GameIsPaused = false;
PauseCanvas.SetActive(false);
}
public void Pause()
{
Time.timeScale = 0;
GameIsPaused = true;
PauseCanvas.SetActive(true);
}
public void MainMenu()
{
Resume();
SceneManager.LoadScene("MainMenu");
}
public void StartScreen()
{
Resume();
SceneManager.LoadScene("Intro");
}
public void Save()
{
player.Save();
}
}
Player.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public int score = 0;
public int lives = 9;
public int health = 3;
public void Save()
{
SaveManager.Save(this);
}
public void Load()
{
PlayerData data = SaveManager.Load();
score = data.Score;
lives = data.Lives;
health = data.Health;
Vector2 position;
position.x = data.Position[0];
position.y = data.Position[1];
transform.position = position;
}
}
Can someone please tell me what I did wrong and/or what I am missing? Any assistance would be appreciated.
Sincerely,
burchland2