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 Wasplay · Sep 25, 2018 at 06:06 PM · serializationsave data

Version Tolerant Serialization with Unity ?

Hi,
I'd like to use this on Unity but I don't know how to
I'm currently using

 BinaryFormatter bf = new BinaryFormatter();
 FileStream file = File.Create(Application.persistentDataPath + "/file");
 bf.Serialize(file, SaveVariables.current);

to save or

 FileStream file = File.Open(Application.persistentDataPath + "/file", FileMode.Open);

to load my data, using this class :

 [System.Serializable]
 public class SaveVariables
 {
     public static SaveVariables current;
 
     public int c = 500;
 
     public bool[] m = new bool[4];
     public bool[] md = new bool[4];
 
     public bool[] p = new bool[5];
     public bool[] pd = new bool[5];
 
     public int roundsPlayed = 0;
     public int gamesPlayed = 0;
     public int bulletsShot = 0;
     public int grenadesShot = 0;
     public int lazersShot = 0;
     public int bouncyShot = 0;
     public int playersDead = 0;
     public int powerupsDestroyed = 0;
 
     public bool sound = false;
     public int ad = 0;
     public string adTime = "";
     public bool rated = false;
 }

The problem is that I want to add new variables to the SaveVariables serialized class, but I can't I can't use PlayerPrefs because it's way too much unsecured, is there any other secured way that supports new variables ? Or a way to add new variables with the system I'm currently using
Thanks

Comment
Add comment · Show 4
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 Wasplay · Sep 26, 2018 at 06:33 PM 0
Share

bump, I really need that ! :(

avatar image Wasplay · Sep 28, 2018 at 04:55 PM 0
Share

this is really annoying

avatar image Bunny83 · Sep 28, 2018 at 05:11 PM 0
Share

Your question is way too unspecific. Ins$$anonymous$$d of bumping it all the time how about adding more details. What have you tried so far? For what exactly do you need it? The serialization system of Unity itself is not .NET serialization. So asset serialization is done with a propritary serialization system. However you can use .NET serialization for serializing objects into your own files at runtime.


So please edit your question and be more specific and detailed about your problem.

avatar image Wasplay Bunny83 · Sep 28, 2018 at 05:23 PM 0
Share

$$anonymous$$y bad, I edited the post

1 Reply

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

Answer by Bunny83 · Sep 28, 2018 at 06:08 PM

You still haven't said what didn't work. You linked the Version tolerant documentation page of microsoft, but did you actually read it? Have you looked at the example project? All you have to do is add the "OptionalField" attribute to all new variables. I just tried it and it works out of the box. I copied your class, saved a version to a file, added a new variable and mark it with OptionalField. like this:

 [OptionalField(VersionAdded = 2)]
 public string test;

I can load the previously saved class without any issues. If course the "test" variable doesn't have any value since that field doesn't exist in the save file yet. Just like shown in the example project you can add a "OnDeserialized" callback to check and post process any variables which might not have been initialized. For example like this:

 [OnDeserialized]
 void OnDeserialized(StreamingContext context)
 {
     if (string.IsNullOrEmpty(test))
         test = "Hello !!!";
 }

Now when i load the old save "test" will be initialized with "Hello !!!" after deserialization. If you change the new field now manually and serialize the new class again the test field will get serialized and deserialized automatically.


Keep in mind that version tolerant serialization is not backwards compatible. You can not remove any field just like explained in the docs you've linked. So if you store an object from a newer version of your application that has additional optional fields, those can not be opened by your old application.


Finally I'd like to mention that the BinaryFormatter is as insecure as PlayerPrefs are. What makes you thing that the BinaryFormatter is more secure? If you mean that it's not that easy to read or change the values, this is just security through obscurity and does not provide any security at all. It may scare off some pure noobs, but those are also incapable to figure out where the PlayerPrefs are stored.


The BinaryFormatter stores the values unencrypted as plain values, just in binary. You can easily edit those with any hex editor. I actually have one in my context menu and can open any file directly with a hex editor.


If you want to make it a bit more secure (note that 100% security is absolutely impossible when you have the code to execute on the client and the data stored on the client) you should add some sanity checks and checksums that you store along with your actual data. If something got changed you can detect that. This is still not secure but the cheater has to figure out how you calculate the checksum in order to successfully manipulate any value.


I also recommend a "late fail" method if you detect changed values. So just load the values and let the game run for a few minutes and then just close the program and delete the save that was changed. Don't tell the user you caught him cheating. A several minute delay has the advantage that it makes it tedious to test if his manipulations are successful or not. In addition you could sabotage the game slightly when it has the state "cheated". This is basically cheater trolling, the only form of trolling that i would consider valid.

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 Wasplay · Sep 29, 2018 at 11:07 AM 0
Share

Thanks, that's exactly what I was asking for !
Sorry if my post was badly written
BinaryFormatter is a lot more secured than the PlayerPrefs because you can't modify it like a simple .txt, most of mobile players don't even know what a hex editor is
Also, I already added a method to detect if a value is changed, don't worry ^^ (but of course there will always be a way to counter it)

(EDIT : "Assets/Scripts/Save.cs(29,6): error CS0246: The type or namespace name `OptionalField' could not be found. Are you missing an assembly reference?".. what do I forget ?)

avatar image Bunny83 Wasplay · Sep 30, 2018 at 06:54 PM 0
Share

You forgot to check the documentation. If you use Visual Studio it can figure out the required namespace for your by clicking on the light blub that appears when you hover over the attribute. Though when checking the documentation which is directly linked from the page you have linked in your question. The OptionalField attribute is located in the "System.Runtime.Serialization" namespace.


And no, it's not "a lot more secure". It's just a bit more "obscure". Depending on the target platform, accessing playerprefs from outisde your application could be as tricky as changing a value inside a binary formatted file.

avatar image Wasplay Bunny83 · Sep 30, 2018 at 07:03 PM 0
Share

Thank you so much, sorry for being so dumb !
I use Visual Studio and it didn't figured out the required namespace, weird :/

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

94 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 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

How do i change my save "Total Coin" everytime i get more coins. 0 Answers

Add an array to a class that has been Serialized with BinaryFormatter 1 Answer

Error when loading saved date : SerializationException: End of Stream encountered before parsing was completed. 2 Answers

Can't use .Add-Function for List 1 Answer

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


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