Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Saving application data?

Discussion in 'PSM' started by GMM, Apr 8, 2014.

  1. GMM

    GMM

    Joined:
    Sep 24, 2012
    Posts:
    301
    Hello guys.

    I am currently messing around with the PSM Preview release and i am wondering how saving data is handled. I currently have written a small system that creates a JSON string of data from some predefined arrays, that is then saved locally using the path returned from Application.persistentDataPath.

    My publishing license is currently pending approval at Sony, so i cannot currently test if this works properly on the actual device. So if anyone has some experience on how data saving is handled, i would love to hear your experiences.
     
    Last edited: Apr 8, 2014
  2. DFP

    DFP

    Joined:
    Nov 22, 2012
    Posts:
    12
    In Unity, I've usually used PlayerPrefs for singular variables (strings/ints/floats) since it's like 1-3 lines of code at most.

    ArrayPrefs for bigger amounts.

    That seems to work pretty well. PlayerPrefs confirmed working on my Vita.

    (If it was easier, I would probably just save files like you.)
     
    Last edited: Apr 8, 2014
    Raimis likes this.
  3. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    Both PlayerPrefs and Application.persistentDataPath are supported.

    Application.persistentDataPath maps to /Documents - this folder is persistent between runs
    Application.temporaryCachePath maps to /Temp - this folder may or may not be cleared between runs
    PlayerPrefs is in fact serialized to file under Application.persistentDataPath.

    There is a limitation on depth of the directory structure - IIRC it has a maximum depth of 6 (ie. /Documents/1/2/3/4/5/6.txt)
     
  4. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,404
    Is system.io and system.xml working on Unity PSM? I know it doesn't work on native SDK but haven't tried out yet on Unity PSM.
    Thanks,
     
  5. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    I haven't tried System.XML specifically, but I expect it to work. System.IO definitely works (it's limited to the read/write to /Documents and /Temp, and read-only from /Application).
     
  6. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,404
    If System.IO works I guess system.XML should too, going to check it out soon.
    [EDIT] Both works fine. Already deploying our game to the Vita! :D
     
    Last edited: Apr 10, 2014
  7. Nekotika

    Nekotika

    Joined:
    Jan 31, 2014
    Posts:
    17
    I can also confirm that PlayerPrefs and Application.persistentDataPath are supported. My game currently uses both and it works :)
     
  8. GunCat

    GunCat

    Joined:
    Nov 27, 2012
    Posts:
    2
    My game uses a large quantity of files loaded dynamically from disk, so I tried to bundle them by opening the .psdp packages with 7zip and pasting my file hierarchy in there like I do on PC builds, but the Publishing Utility for Unity then started failing to deploy the package back to the PSVita.

    After a bit of digging around it looks like I hit the 6 levels of depth limitation that was mentioned here, but the message displayed is pretty obscure, so I'm sharing it in case someone else comes across it :

     
  9. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,404
    Btw, you don't have to dig through the .psdp file, if you simply Build (instead of Build Run) Unity builds everything into folders/sub-folders where you can add/arrange new folders/files and then create a .psdp file out of that using the Publishing Utility.
     
  10. GunCat

    GunCat

    Joined:
    Nov 27, 2012
    Posts:
    2
    Oh, thanks for the tip, I'll try that !

    I thought you were supposed to "extract pdsp package" from the publishing utility, then sort of rebuild it through the "create pdsp" option, but I couldn't get it to work that way.
     
  11. GMM

    GMM

    Joined:
    Sep 24, 2012
    Posts:
    301
    Appreciate the confirmation eriQue! Now i just need my license to get approved so i can test the system :)
     
  12. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,404
    No you don't have to. We for instance have a database distributed around sub-folders that we install at build time. :)
     
  13. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    378
    Player prefs does not work on vita for me, they are finished games that works fine on android, iOS, mac and pc. (been released for over a year)
    Its just the usual player prefs name or rank saving string or int etc, am i missing something?
    Can anyone shed some light on this for me, heres a snippet as i know someone will ask.

    Edit: now when i start the game my ranks at 4, but nothing else seems to save, and rank a new rank won't save, not sure whats going on haha
     
    Last edited: Apr 15, 2014
  14. spacefrog

    spacefrog

    Joined:
    Jun 14, 2009
    Posts:
    739
    You might have to explicitley call PlayerPrefs.Save() after each change on the playerprefs
     
  15. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    378
    Thanks will give that a try, gave it a quick read and it makes sense, says set ints and strings may not be saved correctly if the app is force closed or crashes (swiping the vita app to close may be the problem) will post whether it works or not so you guys as well as myself are more wiser about unity-vita.
     
  16. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    This is just a guess, but - could it be that you are storing some data, and then re-deploying the game from the editor - and then your data is not there?
    Then this is expected (* see below) since the editor will uninstall the application, thus removing your PlayerPrefs, before installing the new version of the application.

    (*) This is only the case with the Public Preview version of Unity-for-PSM. The next version will have this fixed, and subsequent "Build&Run"'s from the editor will preserve the PlayerPrefs contents.
     
  17. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    378

    I have not been rebuilding like you suggested, but i have not yet tried the PlayerPrefs.Save() to see if that fixes my problem, if it persists i'l test a blank new project and submit a report on it if it does.


    Edit: Adding "PlayerPrefs.Save();" after my set prefs now saves the data fine :)
     
    Last edited: Apr 22, 2014
  18. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    Oh, ok - I'll make a note about that - thanks.
     
  19. PeterD

    PeterD

    Joined:
    Feb 6, 2013
    Posts:
    120
    Er what?

    Do you mean that the Build and Run process will simply update the existing version of the app on the device as opposed to uninstalling it and then dropping a fresh build. I ask because it's expected behaviour on every mobile/desktop device that an uninstall removes transient data. Also, if this is the case, then I hope that there is a toggle to turn on clean installs again.
     
  20. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    378

    Android and iOS, build and run updates the app (keeps saved data) it does not uninstall and remove save data, then put a fresh copy on >_<
     
  21. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    Uninstall yes, but that's not what we're talking about here. Build&Run should preserve PlayerPrefs and similar data. This is what happens with desktop (saved to the registry on Windows) as well as mobiles (stored using SharedPreferences object on Android for example).

    Build&Run preserving data is needed to 'guarantee' you can update your title in the wild without losing user progress or similar.
     
  22. Erikir

    Erikir

    Joined:
    Jul 23, 2013
    Posts:
    7
    Sadly for me even using de PlayerPrefs.Save() wont work, the PC build works fine but not on PSM.
     
  23. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    Any chance you could provide a small repro project, code and/or steps to reproduce the symptoms you're seeing?
    If so, please file a bug through the bug reporter (Help -> Report a bug, in the editor).

    Thanks.
     
  24. Hanov

    Hanov

    Joined:
    May 17, 2014
    Posts:
    19
  25. PeterD

    PeterD

    Joined:
    Feb 6, 2013
    Posts:
    120
    Yes installing an APK or an apple app over an existing install will keep the data. However, given that build and run is for testing purposes only. I would have expected it to uninstall the old version first as not doing so can lead to corrupted data and an app failing to run correctly on the device. Personally I never actually use build and run except with PSM simply because I've had way too many issues with apps seemingly being broken after a build and run but, working just fine after a fresh install.
     
  26. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    If installing "on top" of a previous installation (effectively updating) could lead to corrupted data, then that would be extremely critical since it could also affect the update mechanism of a released title.
    That said - the mechanism in the Public Preview is in fact to uninstall-before-install - so there is virtually no difference between "doing a fresh install" and using "Build&Run". The reason for this was precisely because of an issue with the install command in the PSM Tool Set for Unity version for the Public Preview. This problem as since been fixed.
    The Official Release will however use the expected behavior from other platforms, and install "on top" of the previous installation (keeping any files written to /Documents (such as PlayerPrefs) etc.)
     
  27. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    Yes - PlayerPrefs is supported. One thing to note is that PlayerPrefs.Save() must be called manually to serialize the changes to persistent storage.

    Application.persistentDataPath is also supported with PSM. So is Application.temporaryCachePath.

    With PSM, PlayerPrefs is in fact storing its contents using Application.persistentDataPath (which equals /Documents)
     
  28. Hanov

    Hanov

    Joined:
    May 17, 2014
    Posts:
    19
    Thank you, but the method used in the video don't use playerprefs, it use binary formatter and the lenguage serialization (not unity serialization) im sure my really bad english is confusing you lol sorry about that its my 3rd lenguage and im not so good at it. Any way my question is if the serialization used in the video and binary formaters can be used to make a save system for PSM. Not using playerprefs.

    Thanks again.
     
  29. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    The video mentions storing data using PlayerPrefs and manual serialization to Application.persistentDataPath (and the benefit of using the latter).

    Both of which I already mentioned in the previous post are supported with PSM.
     
  30. Hanov

    Hanov

    Joined:
    May 17, 2014
    Posts:
    19
    Thank you, well as im not that good speaking english i need to read answers for dummies just like that =) sorry to be a bother to you. And thanks again.