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
1
Question by fraanns · Feb 07, 2015 at 06:18 PM · c#iosiphonestringbytearray

Serialize and deserialize class to Byte[] on IOS

How can i serialize and deserialize a class to a Byte Array on IOS.

I mean that I write something like

 byte[] SerializeBytes = SerializeObject<ClassToSerialiteIn> (InstanceOfTheClassToSerialize);

and

 ClassToSerialiteIn someClass = DeserializeObject<ClassToSerialiteIn> (SerializedBytes);

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

Answer by Alanisaac · Feb 07, 2015 at 07:41 PM

Not sure from your question if you actually want a byte array or a string, but here is a solution for a byte array. The answer should be applicable to Mono as well:

 using System.IO;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;

 // ...

 object obj = new object ();
 byte[] bytes;
 IFormatter formatter = new BinaryFormatter();
 using (MemoryStream stream = new MemoryStream())
 {
     formatter.Serialize(stream, obj);
     bytes = stream.ToArray();
 }

Note that you probably shouldn't be using byte arrays for persistence of classes, since it won't be compatible with any changes to the class or to the Mono .NET framework, and won't be portable to any other platforms. I'd only use it if you're transporting data between the exact same version of the same application. Otherwise you might want to look into JSON, XML, or even a serverless database.

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 fraanns · Feb 07, 2015 at 08:59 PM 1
Share

In the way i wrote it (see Code below), it works in the Editor but not on IOS. I think stuff like IFormatter just does not work on IOS.

 public static byte[] SerializeObject <T>(T serializableObject)
     {
         T obj = serializableObject;
 
         IFormatter formatter = new BinaryFormatter();
         using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream())
         {
             formatter.Serialize(stream, obj);
             return stream.ToArray();
         }
     }
     
     public static T DeserializeObject <T>(byte[] serilizedBytes)
     {
         IFormatter formatter = new BinaryFormatter ();
         using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream(serilizedBytes)) 
         {
             return (T) formatter.Deserialize (stream);
         }
     }

But with the power of X$$anonymous$$L I have managed to write this version, which works on IOS.

 public static byte[] SerializeObject <T>(T serializableObject)
     {
         T obj = serializableObject;
 
         using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream())
         {
             System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
 
             x.Serialize (stream,obj);
 
             return stream.ToArray ();
         }
     }
     
     public static T DeserializeObject <T>(byte[] serilizedBytes)
     {
         System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer (typeof(T));
 
         using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream(serilizedBytes)) 
         {
             return (T) x.Deserialize (stream);
         } 
     }

avatar image Alanisaac · Feb 07, 2015 at 09:21 PM 0
Share

Nice! Glad you got it working.

avatar image
1

Answer by BytesCrafter · Feb 22, 2017 at 05:09 AM

I tested the script of @alfalfasprout. Yeah! its working. Now what is the sample used of this? First, for example: you want to send an entire class with variables for sync player prefab on the current device to other device which have the clone of the said playerPrefab in Multiplayer Implementation. Just sharing hopefully this help.

Comment
Add comment · 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

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

Fastest C# Byte[] to String conversion? 1 Answer

Multiple Cars not working 1 Answer

Using System.Reflection with iOS 2 Answers

Texture2D.ReadPixels not working on iOS (GL error 0x0502) 1 Answer

Distribute terrain in zones 3 Answers


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