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

Discussion About Json format and Player Prefs

Discussion in 'Scripting' started by energiva21, Apr 13, 2024.

  1. energiva21

    energiva21

    Joined:
    Mar 4, 2023
    Posts:
    68
    I'm making an Offline RPG game in Unity. I want to save some things in my game. I have heard of two recording systems for this, one is PlayerPrefs and the other is to save in json format. PlayerPrefs is simple and easy to hack, and it does not work on all devices in json format. I do not consider the json format because it cannot be played on every device, and since the game is offline, I do not see any harm in doing it with PlayerPrefs. What do you think, what would be better for me to do?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,421
    They are both equally easy to hack (as in: modify).

    Json provides more flexibility and it works on all devices. You just cannot write to every location on every device, and for WebGL specifically additional file I/O restrictions exist.

    Playerprefs don't allow you to save collection types, only int, float, bool and string. It's also increasingly cumbersome to use at scale because you need to have a unique key for every new item whereas json simply serializes from/to serializable C# classes directly.

    In both cases you need to consider what needs to happen when you make changes to the save format after release. Loading an older save file must continue to function after every update, therefore you also need to implement at least a rudimentary versioning and switching the loading process based on the version of the saved data, providing default values for newly added variables and ignoring values that have been removed, and also redirecting values to renamed variables.
     
    Ryiah likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,450
    Only one of these is an actual "system": PlayerPrefs. JSON is a file format. You can combine them having all of your data stored as JSON in a single PlayerPrefs entry but you shouldn't use PlayerPrefs for anything other than basic preferences (eg resolution, audio volume, input selections, etc) due to some platforms having very tight limits.

    On Windows PlayerPrefs is stored within the Windows Registry which is only meant to have small amounts of data stored within it. I don't know what the actual limits are these days but you can screw things up for a user if you try to use it excessively on that platform.

    On WebGL there is an enforced limit of 1MB.

    Since JSON is just a file format it can be used on every device. You just need a storage method that is compatible with that device. Some platforms have restrictions on what you're allowed to do on them (eg WebGL can't access the file system).

    Everything stored on a user's computer is trivial to hack. You don't even need to have the file format. You can just modify memory directly via tools like Cheat Engine. It's even doable on platforms like iOS which are supposed to be locked down.
     
    Last edited: Apr 13, 2024
    Bunny83 likes this.
  4. energiva21

    energiva21

    Joined:
    Mar 4, 2023
    Posts:
    68
    I understand Thank you for the valuable information you provided. When I combine the videos I watched online and what you said, I understand better what you mean.