x


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!

more ▼

asked Feb 21 '12 at 10:20 PM

Aris gravatar image

Aris
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Apr 05 '12 at 07:07 PM

Aris gravatar image

Aris
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

Maybe you can try again with

Texture2D.EncodeToPNG

which return byte[] and serialize that.

more ▼

answered Jul 18 '12 at 09:52 PM

cupsster gravatar image

cupsster
385 6 8 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4152
x437
x436
x178
x31

asked: Feb 21 '12 at 10:20 PM

Seen: 941 times

Last Updated: Jul 18 '12 at 09:52 PM