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 pikzilla · Oct 21, 2016 at 08:51 PM · androidandroid buildsave datapersistentdatapathio

Android: Trying to reset save data on device when pushing a new build!

Hi, all!

So I'm working on an Android project with some basic progression information being saved between game instances. To accomplish this, I'm reading and writing to a save file at Application.persistentDataPath, and serializing / deserializing a custom savedata class.

All the save/load functionality works fine on my test devices -- data persists as expected when I quit and reopen the app. The problem is, when I push new builds to a device that's already run the program before, the save data from that older build is not being reset, causing some big problems with my ability to test. Even though it's a fresh build, all my progression information from a previous build is surviving!

So my question is -- how can I reset the file saved at Application.persistentDataPath on an Android device whenever I push a new build? I'll say that in a perfect world, I'd like to generally be able to access the Application.persistentDataPath from the Unity editor (on Windows), so as to be able to generate and push "Dummy" saves -- but the base-level goal is just being able to reset the device's save data with each build.

I'm not sure if this is necessary for my question, but just in case, here's the save / load code I'm using:

     public static void Save(SAVEDATA_CLASS saveData)
     {
         if (saveData != null)
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + saveFileName, FileMode.OpenOrCreate);
 
             bf.Serialize(file, saveData);
             file.Close();
         }
     }
     public static SAVEDATA_CLASS Load()
     {
         SAVEDATA_CLASS output;
         if (File.Exists(Application.persistentDataPath + saveFileName)) {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + saveFileName, FileMode.Open);
             output = (SAVEDATA_CLASS)bf.Deserialize(file);
             file.Close();
         }
         else {
             output = new SAVEDATA_CLASS();
         }
         return output;
     }
Comment
Add comment · Show 1
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 hexagonius · Oct 21, 2016 at 09:00 PM 0
Share

I'm not an expert, but I think the savegame needs some kind of knowledge about the version it was saved by. The same goes for the version for the game. You could save a version tag to the savefile, and whenever you read it, compare it to the version of your game (build version maybe, or just a static variable in some code).
This way you could just delete it when the version differs and write a new file.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by elenzil · Oct 21, 2016 at 09:10 PM

try something like this.

 private const int version = "2";
 
 bool thisIsADifferentVersion() {
     int previousVersion = PlayerPrefs.getInt("appVersion", version);
     return previousVersion != version;
 }
 
 void checkAndHandleNewVersion() {
     if (thisIsADifferentVersion()) {
         // reset stuff
         PlayerPrefs.setInt("appVersion", version);
     }
 }

.. then with each release, bump the value of "version".

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

internal persistentDataPath with WRITE_EXTERNAL_STORAGE permission 4 Answers

Reading files on Android, 1 Answer

Can other android apps delete save data of Application.persistentDataPath? 1 Answer

PersistentDataPath saved files are not writing into android device? 1 Answer

Using the Android default debug keystone from Windows 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