• 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
Question by vexe · Apr 10, 2016 at 08:00 PM · androidplayerprefssave databinaryformattercorrupt

[Android] Save data loss with PlayerPrefs?

Greetings,

we use BinaryFormatter to serialize the player state into a string w$$anonymous$$ch we store in PlayerPrefs.

We occasionally get reports of players losing their data upon updating to a newer version of the game.

We're not sure exactly as to why t$$anonymous$$s happens, and who's the culprit be$$anonymous$$nd it. Is it BinaryFormatter, PlayerPrefs, or somet$$anonymous$$ng to do with the user device/manifest settings?

I don't t$$anonymous$$nk the data saved in PlayerPrefs gets erased on every update otherwise all players would have their data wiped and not just a few of them.

  • Where does PlayerPrefs store its data on the device?

  • Are there any settings/manifest stuff that should be turned on/off for storage to work properly?

  • We do 'add' fields to the class we serialize and from what we've tested, BinaryFormatter can handle 'new' fields gracefully, it only breaks when there's a field rename or change of data type (BinaryFormatter seems to add metadata about the field's name and type in the stream, I assume that's why it breaks if either of those change). Am I correct, or is there still a chance that the stream gets corrupt upon adding new fields?

  • What are the alternatives to PlayerPrefs? I read about Application.persistentDataPath but that just seems more risky to me due to the whole internal vs external storage. We could also store t$$anonymous$$ngs in a DB but that would require users to have a connection to play w$$anonymous$$ch we don't want.

It's really frustrating for players and us when we hear someone lost their data.

Any help, thoughts, ideas or suggestions are appreciated!

@Bunny83

Thanks.

Comment
Bunny83

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Apr 12, 2016 at 03:40 AM

I wouldn't recommend to use the BinaryFormatter as it's not save to change the structure of the class you serialize / deserialize. It's better to use somet$$anonymous$$ng that isn't bound too strictly to a certain class type. Most serializers will complain when there's missing data (adding fields) or missing fields (removing fields). The most reliable way is to manually match the serialized data with your fields and simply use XML or JSON as data format. You're still free to store that either with PlayerPrefs or simply writing a file.

The way how PlayerPrefs are stored depends on the platform. On Android PlayerPrefs are always stored internally inside the data folder of your app. T$$anonymous$$s path can't be viewed by the user (except when the device is "rooted"). Only your app can access t$$anonymous$$s path. Just see the docs:

On Android data is stored (persisted) on the device. The data is saved in ShaderPreferences. C#/JavaScript, Android Java and Native code can all access the PlayerPrefs data. The PlayerPrefs data is physically stored in

/data/data/pkg-name/shared_prefs/pkg-name.xml.

W$$anonymous$$ch path Application.persistentDataPath returns seems to depend on your write access settings and if you do a development build or not. See "colchambers" answer over here. I'm not sure if you can always manually write to /data/data/pkg-name/files/ anyways. You have to do some testing on that topic i guess ^^.

ps:
My SimpleJSON "framework" also has the ability to save the JSON structure in a compact binary format. SaveToStream, SaveToCompressedStream, SaveToCompressedFile and SaveToCompressedBase64 all use that custom binary format. I haven't implemented any kind of object serialization so you would have to read / write your data "manually". I usually use an interface that has a JSONNode Serialize() and a void Deserialize(JSONNode aNode) method. That way each class can implement it's own serialization / deserialization.

pps:
The version on the wiki has been modified and improved by others and i don't really have an eye on those changes.

Comment
vexe

People who like this

1 Show 0 · 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

Answer by KaushikRahul · Apr 11, 2016 at 02:38 PM

Hi,

There only ways it is possible are:

  1. If for some reason, by accident you are setting the playerprefs or updating whenever the new application launches and its only happening a few time rather then happening everytime.(For some reason :P :P)

  2. Users wipe their cache/data before installing or after installing for some reason.

  3. And last but not the least, i only can t$$anonymous$$nk of write permissions in your players' phones.

Comment

People who like this

0 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 vexe · Apr 11, 2016 at 02:41 PM 0
Share

Thanks for the reply. How do I make sure there's write permissions for my game? Is it one of the settings in the manifest file?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

62 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

Related Questions

PlayerPrefs file wrong stored location 0 Answers

Saving data 1 Answer

High Score with player prefs confusion (JAVA) 0 Answers

What will happen with PlayerPrefs data after updating the app? 1 Answer

Serialization not working on Android 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