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 DoctorWhy · Oct 08, 2012 at 04:40 PM · serializationfile size

Smaller serialization file size

I am serializing some data, which I already have complete. Using the BinaryFormatter to serialize, I get a rather large file size for saving very little. Around 11kb for about 4 objects name (string) and transform information (9 floats x 4). You would think that may be around 1152bytes for the floats, and around 960bytes for the 16bit string of 15 characters each. So you would expect maybe around 3k of data at max, but I am getting 11k. I know 11kb isn't huge, but it is still bigger than I expected. Is there a different serialization method that will work on all the platforms (Web, iOS, android) that will result in a smaller file size?

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

3 Replies

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

Answer by whydoidoit · Oct 08, 2012 at 06:30 PM

In Unity Serializer I use a modified version of SharpZip which is the fastest thing that I've been able to find. I also have a version of 7zip which is more compressed but very slow. BinaryFormatter is a bit bloated as it does store all of the type information mulitiple times.

Feel free just to grab the SharpZIP folder out of Unity Serializer.

Compression looks like this:

 var m = new MemoryStream();
 var br = new BinaryWriter(m);
                 var z = new DeflaterOutputStream(m);
                 br.Write(data.Length);
                 z.Write(data, 0, data.Length);
                 z.Flush();
                 z.Close();
         var data = m.GetBuffer();

Inflation looks like this:

     byte[] output = null;
             var m = new MemoryStream(data);
             var z = new InflaterInputStream(m);
             var br = new BinaryReader(m);
             var length = br.ReadInt32();
             output = new byte[length];
             z.Read(output, 0, length);
             z.Close();
             m.Close();
             
         
Comment
Add comment · Show 6 · 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 DoctorWhy · Oct 08, 2012 at 06:35 PM 0
Share

Does this work on all platforms?

avatar image whydoidoit · Oct 08, 2012 at 06:36 PM 0
Share

Yes it does.

avatar image whydoidoit · Oct 08, 2012 at 06:42 PM 0
Share

You could just use Unity Serializer actually - up to you.

It can serialize just about anything BinaryFormatter can:

    var data = UnitySerializer.Serialize(anyOldClassInstance);
    AnyOldClassInstance myCopy = UnitySerializer.Deserialize<AnyOldClassInstance>(data);
avatar image whydoidoit · Oct 08, 2012 at 06:43 PM 0
Share

Although it only uses the compression by default when it's saving whole scenes - you'd still have compress it yourself or call the helper function CompressionHelper.Compress on the results to get a compressed string out of it (suitable for sending over the wire to servers etc).

avatar image DoctorWhy · Oct 08, 2012 at 06:48 PM 0
Share

Would doing it that way serialize things based the Serializable and NonSerialized attributes?

Show more comments
avatar image
1

Answer by shaderop · Oct 08, 2012 at 06:23 PM

You could try piping your serialized data through a GZipStream. It is implemented in Mono, so it should work on all platforms supported by Unity.

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 DoctorWhy · Oct 08, 2012 at 06:30 PM 0
Share

This could work if all else fails, but if there is a better serialization method out there, I could avoid this extra step.

avatar image
0

Answer by k76 · Mar 03, 2021 at 05:40 AM

I recommend storing the large data portion in its own ScriptableObject and then use PreferBinarySerialization: https://docs.unity3d.com/ScriptReference/PreferBinarySerialization.html

Also, If you use a specific naming scheme for this type of ScriptableObject, you can track it with Git LFS, if you happen to use Git LFS.

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

10 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

Related Questions

Send model/GameObject from Server to Client 0 Answers

If i force close my game the changes i made to my scriptable object are not saving. Is there anyway to get around this? 0 Answers

List Type Mismatch 0 Answers

How do you serialize things on Windows Phone? 0 Answers

How to export Serialized Data with my Build 0 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