Hello everyone:
I have this problem: I try to save the current status of a game I’m developing, and this “progress” is written in a Texture2D. Think of it like a MS Paint canvas where you draw things, only it’s not
Even after doing as I was told looking in other answers around here, the final save file is around 1kb, when it should be around 3MB (the texture is quite large).
Anyway, I follow this thread’s path, or this different simpler path, and it’s still not working for me… There’s something wrong with the serializing part, that’s for sure.
Here’s my code:
[System.Serializable]
public class SaveData {
public Texture2D normalMap;
}
public class SaveLoad {
public static string currentFileName = "SaveGame.hur"; // Edit this for different save files
public static string currentFilePath = Application.persistentDataPath + "/Saves/";
// Call this to write data
public static void Save (Texture2D norm) // Overloaded
{
Save (currentFilePath + currentFileName, norm);
}
public static void Save (string filePath, Texture2D norm)
{
SaveData data = new SaveData ();
data.normalMap = new Texture2D(norm.width, norm.height);
data.normalMap.SetPixels(norm.GetPixels());
data.normalMap.Apply();
FileStream stream = new FileStream(filePath, FileMode.Create);
try {
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, data);
}
catch (SerializationException e) {
Debug.LogError("Excepcion al serializar el savegame. Datos: " + e.Message);
}
finally {
stream.Close();
}
}
// Call this to load from a file into "data"
public static SaveData Load () { // Overloaded
return Load(currentFilePath + currentFileName);
}
public static SaveData Load(string filePath)
{
SaveData data = new SaveData ();
FileStream stream = new FileStream(filePath, FileMode.Open);
try {
BinaryFormatter bformatter = new BinaryFormatter();
data = (SaveData)bformatter.Deserialize(stream);
}
catch (SerializationException e) {
Debug.LogError("Excepcion al deserializar el savegame. Datos: " + e.Message);
}
finally {
stream.Close();
}
return data;
}
That’s only a sample, it’s actually bigger but that’s the part that’s failing, I’m quite sure of that. For more information, right now I’m getting this error:
Excepcion al serializar el savegame. Datos: Type UnityEngine.Texture2D is not marked as Serializable.
This is captured in this “try”:
try {
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, data);
}
…while saving in the method Save.
I’ve read (here) that Texture2D is a Serializable type of object, so this is weird to me…
I’m using unity 3.4, windows 7 x64 and this is written in c#, but the script that makes the call to “Save” and “Load” functions is in JS.
Any insight in this please? Any help would be GREATLY appreciated!
Thanks!
Can you please explain how you actually did it? I am not able to get it.
– bhatiarohan156