- Home /
Error serializing a class to save / load
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!
Answer by Aris · Apr 05, 2012 at 07:07 PM
Finally I solved the problem... It turns out you can't serialize Texture2D (even if it's supposed to work), or at least you can't do it that way. I surrendered and did it with a float[] array with the color values of the pixels and it all worked out with the same code.
So you know it now, don't serialize Texture2D :D
Can you please explain how you actually did it? I am not able to get it.
He most likely converted the data from the Texture 2D, which is length, width, and colors, into a form that can be serialized, like a float array.
This can also be done internally with an ISerializationSurrogate.
Answer by cupsster · Jul 18, 2012 at 09:52 PM
Maybe you can try again with
Texture2D.EncodeToPNG
which return byte[] and serialize that.
Texture2D textFb2 = new Texture2D(url.texture.width, url.texture.height, TextureFormat.ARGB32, false);
byte[] bytes = textFb2.EncodeToJPG();
string s = Convert.ToBase64String(bytes);
I am using this to encode an image to base64 but the when i check the resultant string by covering it to an image i get a grey image of dimension 50x50.
$$anonymous$$y motive is to get user's profile image from Facebook and store it to backend in base64 format
Your answer
Follow this Question
Related Questions
Coding help: How to use xml serialization 1 Answer
Save and load serialization problem 0 Answers
Can't get serialization Load/Save to work!C# 3 Answers
My save function saves the wrong values!! 1 Answer
Easy way to save rigidbody state. 1 Answer