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 Noxury · Jan 19, 2018 at 07:06 PM · variablesave datavisiblebinaryformatternames

Variable names are still visible when saving with binaryFormatter

Hello,

I am saving data that can be loaded later on. This works very well, however when I open this file in a text editor I can still see the container class name and its variable names in plain text.

I wonder why the Unity Tutorial used BinaryFormatter in in terms of preventing data corruption, when there is half visible.

How can this be fully changed to crap looking characters?

 public static void Save<T>(string filePath, T data){
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(filePath);
     bf.Serialize(file, data);
     file.Close();
 }


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 Bunny83 · Jan 19, 2018 at 08:21 PM

Just don't use the BinaryFormatter. It actually has a lot of overhead which includes the exact class and field names. You can just write your desired values "manually" to a file by using the BinaryWriter and BinaryReader. The only important thing is that you have to read the exact same variables in the same order as you have written them.


This will only write the actual data, nothing more:

 public class YourData
 {
     public float health;
     public int coins;
     public bool someBool;
 }
 
 public void Save(string filePath, YourData data)
 {
     using (var stream = System.IO.File.OpenWrite(filePath))
     using (var writer = new System.IO.BinaryWriter(stream))
     {
         writer.Write(data.health);
         writer.Write(data.coins);
         writer.Write(data.someBool);

     }
 }
 
 public YourData Load(string filePath)
 {
     using (var stream = System.IO.File.OpenRead(filePath))
     using (var reader = new System.IO.BinaryReader(stream))
     {
         var data = new YourData();
         data.health = reader.ReadSingle();
         data.coins = reader.ReadInt32();
         data.someBool = reader.ReadBoolean();
         return data;
     }
 }

In this example the file will only contain 9 bytes. A single float value has 4 bytes. A single int value has 4 bytes. A single bool value has 1 byte. As i said you have to keep in mind that you have to make sure you read the exact same data that you write. Of course when you want to write dynamical data you have to make sure you can reconstruct the data. So when you want to store an int array you have to store the count first so that when you read it you know how many values you have to read.


Independent from the way you actually save your data you can of course "encrypt" your data if you just want to prevent the user from reading the data.

Comment
Add comment · Show 3 · 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 Noxury · Jan 19, 2018 at 11:51 PM 0
Share

Thanks, I will try this out. Does it work when using custom Serializalizable Unity BuiltIn variables ? And can this be turned to a static method that will then write all variables from the parameter T just like in my code?

avatar image Bunny83 Noxury · Jan 20, 2018 at 12:37 AM 0
Share

No, you simply have to store the data you need "manually". However you can create some helper methods for types like Vector3

 public static void WriteVector3(BinaryWriter aWriter, Vector3 aVector)
 {
     aWriter.Write(aVector.x);
     aWriter.Write(aVector.y);
     aWriter.Write(aVector.z);
 }
 public static Vector3 ReadVector3(BinaryReader aReader)
 {
     return new Vector3(aReader.ReadSingle(), aReader.ReadSingle(), aReader.ReadSingle());
 }

avatar image Noxury Bunny83 · Jan 20, 2018 at 01:25 AM 0
Share

I appreciate this so much. Avoiding double code is key for clean code. Thanks a ton.

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

78 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 avatar image avatar image

Related Questions

Save Game format that is change proof and secure 0 Answers

Save data not creating save file 1 Answer

My app generates a binary file but either fails to save data or load it || Unity for Android 1 Answer

Initialize List in User Data Class without deleting binary file 0 Answers

BinaryFormatter - saving and loading a list containing sprites. 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