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 MD_Reptile · Apr 14, 2015 at 06:42 AM · saveloadfilebinaryio

from bool array to binary file?

I was trying to save these huge bool arrays (by huge I mean a little over 16.7 million values) into text files by using stringbuilder to append a string to either 0 for false or 1 for true while iterating through the array - to later load at runtime - and well a problem im seeing is this makes huge txt files, like 16mb if I save it all as one. I couldn't even open this txt up in notepad (or other programs) and decided to try breaking it up into 8 separate txt files. No joy, those 2mb files still are too much to open and view in notepad... bummer.

So I'm wondering if it's possible (for performance and filesize purposes) to instead save directly to a binary file (you know just the ones and zeros instead of a huge string of the chars 1 and 0) while I'm in the editor, then load those binary files at runtime.

I'm assuming that doing that would produce smaller files, and improve load time on device. Note that opening/editing in notepad (or anything) is not necessary, that's just how I realized those files are bloated.

Ideas? Anybody done this "save to binary file, then load at runtime" thing?

Here is how I was saving plain text files:

 StringBuilder sB = new StringBuilder;
 
 for(int i = 0; i < 16777216; i++)
 {
 if (hugeBoolArray [i] == false)
 sB.Append ("0");
 else
 sB.Append ("1");
 }
 
 File.WriteAllText (filePath + "_file.txt", sB.ToString ());


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

1 Reply

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

Answer by Baste · Apr 14, 2015 at 07:17 AM

You could use a BinaryWriter, which has a Write(bool) method. Check out the docs here.

Now, 16 million bools should be ~2mb (one bit for every bool). You won't get any lower than that as long as it's just arbitrary boolean values. You're getting 8 times that, which is probably because the strings are one byte (8 bits) long.

If Write(bool) still ends you with too much data, you'll want to convert 8 of the bools at a time to bytes, and use File.WriteAllBytes to write them. That should be guaranteed to work.

Comment
Add comment · Show 4 · 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 MD_Reptile · Apr 14, 2015 at 07:41 AM 0
Share

Awesome. Yeah I had figured raw chars must be using way too much storage.

While I've got you @Baste can you confirm my suspicions that File.ReadAllBytes would be used to read back those values at runtime? Can I do another array of bool that equals those values? Like someBigBoolArray [i] = thatFilesValues [i] or similar?

avatar image saschandroid · Apr 14, 2015 at 07:49 AM 0
Share

You could also use WWW.bytes to get all bytes from the file.

avatar image Baste · Apr 14, 2015 at 08:05 AM 1
Share

@$$anonymous$$D_Reptile, yes, you would read back that file using ReadAllBytes.

BUT, remember that each value in the byte-array you're getting back should represent 8 bool values, so you can't just do index-to-index translation - you'll have to do some maths.

avatar image MD_Reptile · Apr 14, 2015 at 04:39 PM 0
Share

Thanks for all the help!

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

19 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

Related Questions

Checkpoint autosave Method? 0 Answers

Sharing Violation On Path 1 Answer

SerializationException: Type UnityEngine.UI.Button is not marked as Serializable. Solution 1 Answer

Saving GameObject to file 2 Answers

How do I create/load/save Highscores? 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