Search Unity

Lost Data After Update: How Does Unity Choose a Save Location?

Discussion in 'Android' started by kromenak, Feb 17, 2012.

  1. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    236
    Hey guys,

    On our current game, we have been getting some emails saying that save data is being erased when they enter or leave the app after our latest update. This clearly doesn't happen on every phone, since we haven't been able to reproduce it on our testing devices.

    We fixed a bug in our game before release where we had added the WRITE_EXTERNAL_STORAGE permission to our game, and suddenly player progress files were being saved to the SD card instead of internal storage, which made it appear that you had lost your data after an update.

    We are using Application.persistentDataPath for save data; how is this value determined on Android? It appears to point to different locations depending on whether the game has the WRITE_EXTERNAL_STORAGE permission, or maybe based on whether the game is installed to the SD card or not? Is there any built-in way in Unity to get a path that always points to internal storage?

    Thanks, any clarity that can help us figure out this data loss issue would be greatly appreciated.
     
  2. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    236
    As a follow up, I feel that changing the persistentDataPath variable based on the WRITE_EXTERNAL_STORAGE permission ought to be considered a bug. It is clearly not pointing to a persistentDataPath if it changes depending on the permissions! Additionally, it is probably not the intention of developers storing game data to make that data available to anyone who mounts their SD card. And finally, we've found that there is a bug in Android 2.2 where data saved to external storage will be wiped when the app is updated - probably not desired behavior, and a lot of people still use Android 2.2.

    So ultimately, a variable that is supposed to always point to the same location suddenly pointing to a different location for an undocumented reason, along with all the problems that occur when you put save game data on external storage, I'd say this is undesirable behavior.
     
  3. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Well, technically persistentDataPath is pointing to a path for persistent data ;)

    persistentDataPath mean, that the the path is for saving persistent data. Not a persistent path for saving data (then the variable would be called persistentPathData). Got it?

    Correct: persistentDataPath
    Wrong: persistentDataPath

    As for the rest: Well, yea. That may be undesireable. Best to contact unity support at support@unity3d.com or use the in Editor report tool. Help > Report a Bug
     
  4. mattbenic

    mattbenic

    Joined:
    Nov 24, 2010
    Posts:
    22
    @kromenak Did you ever get a solution to this? I'm seeing pretty much the same thing, I set "Write Access" in Android Player Settings to "Internal Only" yet Application.persistentDataPath is returning a path on the SD card. The odd thing is this happens on our game (which is pretty large) but not in a simple test case I put together set up the same way. The biggest issue with this for us right now is that it's a pain to clear savegames for testing.
     
  5. colchambers

    colchambers

    Joined:
    Jun 11, 2012
    Posts:
    5
    if you have development build checked you get the sdcard even on internal write access. That's my experience anyway.
     
  6. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,156
    Has anybody an answer to this?

    After updating my app the data that was downloaded to Application.persistentDataPath gets deleted and lost ....
     
  7. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    236
    We did ultimately solve our problem by writing a simple Android plugin that wraps the Android SDK functions getFilesDir() and getExternalFilesDir(). We could then directly call a Unity function like "DataUtil.GetInternalStoragePath()" to specify explicitly that we wanted an internal storage location. We got around the problem by avoiding the ambiguity of working with Application.persistentDataPath.

    I wrote a blog post about it, might be helpful for you: http://supersegfault.com/unity-on-android-save-data-pitfall/
     
    kreso likes this.
  8. kolloldas

    kolloldas

    Joined:
    Jul 2, 2015
    Posts:
    13
    Thanks for pointing this out kromenak. I faced a similar problem when I integrated Charboost. They for some unknown reason need the WRITE_EXTERNAL_STORAGE permission.
     
  9. Caffeinebomb

    Caffeinebomb

    Joined:
    Aug 22, 2013
    Posts:
    6
    Thanks for pointing this out and blogging about it kromenak! Pointed us in exactly the right direction to fix a similar issue and hopefully soothe our very angry customers.
     
  10. kreso

    kreso

    Joined:
    Sep 7, 2013
    Posts:
    97
    Been getting angry email from customers and couldn't figure it out, until now.
    Thank you sir.
     
  11. Nodrap

    Nodrap

    Joined:
    Nov 4, 2011
    Posts:
    46
    OMG. This is disgusting. I just hit the same problem. I added in Everyplay to my app and now the save data is lost. It it truly terrible that Unity have not fixed this. It is for PERSISTENT data, if it doesn't stay persistent then it doesn't do what it should. All they need to do is scan the local area and always use it if data has been stored there. It must be a set once and never change. How hard is that? This IS a bug. I haven't got time or the skills to create a plugin. I'm gonna have to just piss off my current userbase!
     
  12. prins150

    prins150

    Joined:
    Apr 22, 2016
    Posts:
    6
    using UnityEngine 5.3.4f1
    I had the permission in android manifest to write external,but that didn't work.
    The persistent datapath seemed to change between Android/data/Persistentpath and Data/data/Persistentpath.
    Anyway,this fixed the problem for me :
    changed "Player Settings" > "Other Settings" > "Write access" to External(SDCard).
    After that, persistent datapath has been always the same(Internal storage(Android/data/persistentpath)) and haven't had any more issues with save files being lost.
     
  13. kreso

    kreso

    Joined:
    Sep 7, 2013
    Posts:
    97
    Hey Prins, I would recommend reading the article from kromenak (in this thread). It goes into detail explaining why it's not smart using external storage & persistent path.
    I experienced angry customers first hand, and this article helped me a lot. I would advise you to read it.
     
    IgorAherne and prins150 like this.
  14. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    339
  15. kreso

    kreso

    Joined:
    Sep 7, 2013
    Posts:
    97