Hey guys,
I am quite new to programming and especially saving, so I watched a couple of Tutorials and tried to rebuild a binary save / load system which seems to work fine for some variables, however an arrasy containing variables from another class does not seem to get saved. Would appriciate if anyone could tell me whihc mistake I made.
Here is my Save/Load script:
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class SerializationManager
{
public static bool Save(string saveName, object saveData)
{
BinaryFormatter formatter = GetBinaryFormatter();
if (!Directory.Exists(Application.persistentDataPath + "/TapJoy"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/TapJoy");
}
string path = Application.persistentDataPath + "/TapJoy/" + saveName + ".save";
FileStream file = File.Create(path);
formatter.Serialize(file, saveData);
file.Close();
return true;
}
public static object Load(string path)
{
if (!File.Exists(path))
{
return null;
}
BinaryFormatter formatter = GetBinaryFormatter();
FileStream file = File.Open(path, FileMode.Open);
try
{
object save = formatter.Deserialize(file);
file.Close();
return save;
}
catch
{
Debug.LogErrorFormat("Failed to load file at {0}", path);
file.Close();
return null;
}
}
public static BinaryFormatter GetBinaryFormatter()
{
BinaryFormatter formatter = new BinaryFormatter();
return formatter;
}
}
And then:
using System.Collections;
using UnityEngine;
[System.Serializable]
public class SaveData
{
private static SaveData _current;
public static SaveData current
{
get
{
if (_current == null)
{
_current = new SaveData();
}
return _current;
}
set
{
if (value != null)
{
_current = value;
}
}
}
public PageValues pageValues = new PageValues();
}
And Finally my custom classes:
using System;
using UnityEngine;
[System.Serializable]
public class PageValues
{
public Tiles[] tiles;
public float timer;
public int starRating;
public int failCounter;
[Serializable]
public class Tiles
{
public bool isColored = false;
public Color color;
}
}
Somehow the Tiles tiles; array seems not to get saved.