I have no Idea why my data isn’t being saved/loaded properly.
I’m trying to store a float variable, and somewhere along the save and load it’s being changed.
I’ve set up a test scene that prints to the debug log, which shows the problem I’m facing.
using UnityEngine; // For Debug.Log, etc.
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Runtime.Serialization;
using System.Reflection;
// === This is the info container class ===
[System.Serializable]
public class SaveData : ISerializable
{
// === Values ===
// Edit these during gameplay
public float rand;
// === /Values ===
// The default constructor. Included for when we call it during Save() and Load()
public SaveData() {
}
// This constructor is called automatically by the parent class, ISerializable
// We get to custom-implement the serialization process here
public SaveData(SerializationInfo info, StreamingContext ctxt)
{
// Get the values from info and assign them to the appropriate properties. Make sure to cast each variable.
// Do this for each var defined in the Values section above
rand = GameObject.Find("Main Camera").GetComponent<DataHandler>().Rand;
}
// Required by the ISerializable class to be properly serialized. This is called automatically
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
// Repeat this for each var defined in the Values section
info.AddValue("rand", rand);
}
}
// === This is the class that will be accessed from scripts ===
//[Serializable()]
public class SaveAndLoad : MonoBehaviour
{
public static string currentFilePath = "./SaveData.agt"; // Edit this for different save files
// Call this to write data
public static void Save(SaveData data) // Overloaded
{
if(data==null)
data = new SaveData();
Save(currentFilePath,data);
}
public static void Save(string filePath, SaveData data)
{
if(data==null)
data = new SaveData();
Debug.Log ("Saving: " +data.rand);
Stream stream = File.Open(filePath, FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
bformatter.Serialize(stream, data);
stream.Close();
}
// Call this to load from a file into "data"
public static void Load() { Load(currentFilePath); } // Overloaded
public static void Load(string filePath)
{
if(File.Exists(filePath)){
SaveData data = new SaveData();
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Binder = new VersionDeserializationBinder();
data = (SaveData)bformatter.Deserialize(stream);
stream.Close();
Debug.Log ("Loading: " + data.rand);
GameObject.Find("Main Camera").GetComponent<DataHandler>().LoadData(data);
//level.GetComponent<SlotSelection>();
// Now use "data" to access your Values
}
else Debug.LogError("File Not Found");
}
}
// === This is required to guarantee a fixed serialization assembly name, which Unity likes to randomize on each compile
// Do not change this
public sealed class VersionDeserializationBinder : SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName )
{
if ( !string.IsNullOrEmpty( assemblyName ) !string.IsNullOrEmpty( typeName ) )
{
Type typeToDeserialize = null;
assemblyName = Assembly.GetExecutingAssembly().FullName;
// The following line of code returns the type.
typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) );
return typeToDeserialize;
}
return null;
}
}
using UnityEngine;
using System.Collections;
public class DataHandler : MonoBehaviour {
private bool btnDwn;
[SerializeField]
private SaveData data;
[SerializeField]
private float rand;
public float Rand
{
get { return rand; }
}
// private static int slot = 1;
// Use this for initialization
void Start () {
btnDwn = false;
}
// Update is called once per frame
void Update () {
rand = Random.value * 10f;
}
void FixedUpdate()
{
if(!btnDwn){
if(Input.GetAxis("Action") != 0f){
ProcessInput (Input.GetAxis("Action"));
}
} else {
if(Input.GetAxis("Action") == 0f){
btnDwn = false;
}
}
}
void ProcessInput(float i){
btnDwn = true;
if(i > 0){
DataSaver();
}
else if(i < 0){
SaveAndLoad.Load();
}
}
public void LoadData(SaveData loadData) {
// SaveData data = loadData;
}
public void DataSaver() {
data = new SaveData();
data.rand = rand;
SaveAndLoad.Save (data);
}
}
1597841–96152–$Serialization_Test.zip (7.46 KB)