Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by mgrenier · Jun 26, 2011 at 04:52 PM · serialization

ISerialization madness

I'm having a hard time with ISerialization process. Related to my post on the forum, I built a simple example to reproduce my problem.

Steps to reproduce:

  1. Create a script TestSerializer.cs anywhere on your project

  2. Assign script to an Empty GameObject

  3. Press play

Console output:

Serialization Test: myTest (12.0, 12.0, 12.0) (12, 12, 12)
Serialization Vec3: (12, 12, 12)
Deserialization Test: myTest (0.0, 0.0, 0.0) (0 0, 0, 0)
Deserialization Vec3: (12, 12, 12)
Result: myTest (0.0, 0.0, 0.0) (12, 12, 12)

Should have outputted:

Serialization Test: myTest (12.0, 12.0, 12.0) (12, 12, 12)
Serialization Vec3: (12, 12, 12)
Deserialization Vec3: (12, 12, 12)
Deserialization Test: myTest (12.0, 12.0, 12.0) (12, 12, 12)
Result: myTest (12.0, 12.0, 12.0) (12, 12, 12)

Problem:

I do understand that this process seems to be asynchronous since my t.vec3 is modified internally to the correct value at a different time. But since I'm converting Vec3 to Vector3 before that internal magic happen, my Vector3 is always (0,0,0).

To me, deserialization process seems to be reversed: Vec3 should be deserialized before Test. So I must did something wrong?

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
1
Best Answer

Answer by BigBulle · Jun 26, 2011 at 08:46 PM

I've just tried your code in Visual Studio in a console application and it works perfectly :-)

From my past experience with Unity, do only use the internal serialization system of Unity.

If you want to serialize a personal object, you can also define a byte array as a public member of a MonoBehaviour. Then just serialize/desarialize to/from this byte array with the binary formatter. Do not implement the ISerialize interface, just use the Serialize attribute it works fine. The byte array itself will be saved by UNity because its a public member.

Here is an example of implementation:

 using System.ComponentModel;
 using ICSharpCode.SharpZipLib.Zip;
 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour 
     {
     // Keep it private instead of public to hide it in the inspector and to other scripts
     [SerializeField, HideInInspector]
         private byte[] m_BackupArray;
     
         private MyClassToSerialize m_MyClassToSerialize;
     
         // Restore your class
         void Awake () 
         {
             RestoreChanges();
         }
     
         public void StoreChanges()
         {
             // Save your class with compression
             var saveStream = new System.IO.MemoryStream();
             using (var stream = new ZipOutputStream(saveStream))
             {
                 stream.PutNextEntry(new ZipEntry("My backup"));
                 var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                 binaryFormatter.Serialize(stream, m_MyClassToSerialize);
                 stream.CloseEntry();
             }
             saveStream.Close();
             m_BackupArray = saveStream.GetBuffer();
         }
     
         public void RestoreChanges()
         {
             if (m_MyClassToSerialize != null)
             {
                 using (var stream = new ZipInputStream(new System.IO.MemoryStream(m_BackupArray)))
                 {
                     stream.GetNextEntry();
                     var binaryReader = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                     m_MyClassToSerialize = (MyClassToSerialize) binaryReader.Deserialize(stream);
                 }
             } 
             else
             {
                 m_MyClassToSerialize = new MyClassToSerialize();
             }
     
             // One way to keep track of the changes
             m_MyClassToSerialize.PropertyChanged += OnMyClassToSerialize_PropertyChanged;
         }
     
         void OnMyClassToSerialize_PropertyChanged(object sender, PropertyChangedEventArgs e)
         {
             StoreChanges();
         }
     }


Comment
Add comment · Show 5 · 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 mgrenier · Jun 26, 2011 at 08:57 PM 0
Share

$$anonymous$$y objects (Network of Node and Links) are a bit more complicated than the attached example and I did not want to put everything in public members (that why I found the ISerializable to be useful).

I'll try to use a Proxy object that will be serialized using your method. Definitely better than saving/reading to file/resource.

avatar image BigBulle · Jun 26, 2011 at 09:05 PM 1
Share

In my example you have nothing in public and I've already used this method to save networks with nodes and links... The binary formatter is very powerfull, you only have to add the Serialize attribute above your class. Your class can be as complex as you want.

Please share if you get a nice implementation with Proxy

avatar image mgrenier · Jun 26, 2011 at 09:17 PM 0
Share

I'm playing with this as I'm writing this and I must say that I over-thought it. I still have to by-pass the Vector3 (since it "Type UnityEngine.Vector3 is not marked as Serializable.") with a dummy-object.

On a side note, where is the package for ZipOutputStream? Is it bundle with $$anonymous$$ono?

avatar image BigBulle · Jun 26, 2011 at 09:22 PM 0
Share

Why do you reference Vector3 in your class? Just use your method toVector3() when you need it. ZipOutputStream comes from here http://sharpdevelop.net/OpenSource/SharpZipLib/ I will add the reference in my code above.

avatar image mgrenier · Jun 26, 2011 at 10:42 PM 0
Share

I built quick-n-dirty proxy classes and it seems to work!

http://codepaste.net/h1i7ne

I don't know if it's a good practice to include a binder that "does no validation" (see DeserializationBinder line 70) but it allow the saved bytes to load even tho it is a "different assembly".

avatar image
1

Answer by LucasMeijer · Jun 26, 2011 at 08:24 PM

It indeed looks like the .net deserializer only deserializes the vec3 after your GetObjectData() finishes. One workaround you could do is have your Vector3 nested in your Vec3, and handle the creation of it in the vec3 deserialize method.

The (to me) more interesting question here is: why do you need to do this?

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 mgrenier · Jun 26, 2011 at 08:51 PM 0
Share

As you can read in my post on the forum, I built a navigation mesh. I'm trying to save some "precious" runtime initialization with serialization.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[c#] Add components from de-serialized instance 0 Answers

How to export Serialized Data with my Build 0 Answers

"Serialization depth limit exceeded" with non-serializable classes 1 Answer

" Binary Save" Stuff - Workaround for Vector3 Serialization 3 Answers

Coding help: How to use xml serialization 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