Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aris · Feb 21, 2012 at 10:20 PM · c#serializationsaveloadserializable

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bhatiarohan156 · Mar 09, 2016 at 07:34 AM 1
Share

Can you please explain how you actually did it? I am not able to get it.

avatar image Cherno bhatiarohan156 · Mar 09, 2016 at 09:16 AM 0
Share

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.

avatar image
0

Answer by cupsster · Jul 18, 2012 at 09:52 PM

Maybe you can try again with

 Texture2D.EncodeToPNG

which return byte[] and serialize that.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bhatiarohan156 · Mar 09, 2016 at 07:32 AM 0
Share
 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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges