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
0
Question by GenericToast · Oct 15, 2017 at 06:37 AM · serializationbinaryformatterserializedobjectserializer

What's a fast serializer that works cross platform that can be used for saving/loading custom levels?

I'm looking for a serializer that works across multiple platforms including those that don't support JIT compilation(eg. IOS). I would also prefer if the serialized object is non-human readable(eg. no xml/json).

Binaryformatter is a good choice but relatively slow.

Protobuf is a lot faster but requires pre-compilation in order to work on platforms which don't support JIT compilation, which i can't do since it breaks some of my code.

Is there any other serializer that i can use which has a similar speed to protobuf? (I mainly care about the deserialization speed and not too much on the serialization speed)

Comment
Add comment · Show 6
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 Bunny83 · Oct 16, 2017 at 04:45 PM 0
Share

The question is a bit vague. What do you want to serialize? Arbitrary native object trees? What's the purpose of the serializer? Why does it have to be fast? Network transfer or something different? The BinaryFormatter is not only slow but also has a lot of overhead as it serializes the fully qualified class names as string. If you work in a controlled environment it's easier and smaller to create case specific serialization

avatar image GenericToast Bunny83 · Oct 17, 2017 at 03:02 AM 0
Share

Im creating an in game level editor. The level data itself is a class which i want to write to a file which can be stored.

avatar image Bunny83 GenericToast · Oct 17, 2017 at 10:49 AM 0
Share

But it's most likely not just a single class? Does it have Vector2 / 3 values in there? That's in general a problem for most serializers as Vector3 does not have the Serializable attribute. $$anonymous$$any internal types of Unity are not really prepared for general serialization.

As i said the easiest solution would be to just roll your own format. Just use a BinaryReader / BinaryWriter and write out your data the way you like.

I'm happy to provide some example code, but without any insight what your "data" looks like it would be a shot in the dark.

ps: If it's a level editor, why does the speed of the BinaryFormatter actually matter? Not that i would recommend using it, i'm just curious.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 16, 2017 at 05:14 PM

You may have a look at this post. Reflection doesn't require JIT support. Only dynamic code generation is not supported. I haven't used any Protobuf implementations yet, but i'm sure there are implementations which work on AOT platforms.

edit
Here's a quick example how you can manually serialize objects using the BinaryReader / Writer:

 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
 using UnityEngine;
 
 
 public class LevelObject
 {
     public string name;
     public Vector3 position;
     public Quaternion rotation;
     public void Serialize(BinaryWriter aWriter)
     {
         aWriter.Write(name);
         position.WriteTo(aWriter);
         rotation.WriteTo(aWriter);
     }
     public void Deserialize(BinaryReader aReader)
     {
         name = aReader.ReadString();
         position = aReader.ReadVector3();
         rotation = aReader.ReadQuaternion();
     }
 }
 
 public class Level
 {
     public string name;
     public List<LevelObject> objects = new List<LevelObject>();
     public void Serialize(BinaryWriter aWriter)
     {
         aWriter.Write(name);
         aWriter.Write(objects.Count);
         for (int i = 0; i < objects.Count; i++)
             objects[i].Serialize(aWriter);
     }
     public void Deserialize(BinaryReader aReader)
     {
         name = aReader.ReadString();
         int count = aReader.ReadInt32();
         objects.Clear();
         for(int i = 0; i < count; i++)
         {
             var obj = new LevelObject();
             obj.Deserialize(aReader);
             objects.Add(obj);
         }
     }
 }
 
 
 public static class Extensions
 {
     public static void WriteTo(this Vector3 aVec, BinaryWriter aWriter)
     {
         aWriter.Write(aVec.x);
         aWriter.Write(aVec.y);
         aWriter.Write(aVec.z);
     }
     public static Vector3 ReadVector3(this BinaryReader aReader)
     {
         return new Vector3(aReader.ReadSingle(), aReader.ReadSingle(), aReader.ReadSingle());
     }
     public static void WriteTo(this Quaternion aRot, BinaryWriter aWriter)
     {
         aWriter.Write(aRot.x);
         aWriter.Write(aRot.y);
         aWriter.Write(aRot.z);
         aWriter.Write(aRot.w);
     }
     public static Quaternion ReadQuaternion(this BinaryReader aReader)
     {
         return new Quaternion(aReader.ReadSingle(), aReader.ReadSingle(), aReader.ReadSingle(), aReader.ReadSingle());
     }
 }
 
 public class Loader : MonoBehaviour
 {
     public Level level;
     public void SaveToStream(FileStream aStream)
     {
         using (var writer = new BinaryWriter(aStream))
             level.Serialize(writer);
     }
     public void SaveToFile(string aFileName)
     {
         using (var fs = File.OpenWrite(aFileName))
             SaveToStream(fs);
     }
     public void LoadFromStream(FileStream aStream)
     {
         using (var reader = new BinaryReader(aStream))
         {
             level = new Level();
             level.Deserialize(reader);
         }   
     }
     public void LoadFromFile(string aFileName)
     {
         using (var fs = File.OpenRead(aFileName))
             LoadFromStream(fs);
     }
 }

Of course this is just an example. Of course if you use polymorphism in your object tree you have to handle that yourself. However most serialization solutions do not directly support polymorphism as the actual runtime type would need to be encoded in some way so the deserializer can recreate the correct type. The BinaryFormatter does this by serializing the full class name as string which makes the result quite large.

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

76 People are following this question.

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

Related Questions

Binary serialization layout on different platforms 0 Answers

Why does the prefabOverride not work on fields marked as SerializeField? 0 Answers

Which types are actually serializable? Is the documentation incorrect or am I? 3 Answers

Error when trying to Serialize a field that is in a class 0 Answers

What is the best/easiest way to save a game (or scene) at runtime? 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