- Home /
the Json script elements are saved even I build a new apk ?
let me explain the question i had developed a game using unity 3D in which json is used as data base to store data . i had bought some items in editor and some items are unlocked . when I build an apk . the items which are unlocked still remains same on my phone .
[image on my editor]: /storage/temp/180717-screenshot-362.png
[image on my phone ]: /storage/temp/180718-screenshot-20210515-130836977-1.jpg
why it is happening like that . the save states are same on both images , please help
Answer by Bunny83 · May 15, 2021 at 09:17 AM
Well, where did you store your "json database"? It's just a file that is completely outside of Unity's control. If you edit the file in the editor, it will be shipped with your game in it's current shape and form. So if you directly modify the file, those changes will of course persist. Keep in mind that while in the editor you can directly change files in the assets folder. However that is not possible in a build game since the assets are compiled during build.
So it would be interesting to know how you intend to use your json database in the build? Where do you actually store the modified file on the device? For standalone builds one could use the streaming assets folder to store such files, however that's not possible for Android or WebGL as all assets that are shipped with your game are inside the APK file. There's no direct path to those assets. You usually use the persistentDataPath to store modified information on the target device. It's common to ship pre populated databases as either TextAsset or inside StreamingAssets and extract that file at the first launch into the persistentDataPath where you will be modifying the file. Note that this method has some drawbacks: when you update your game and ship it with a new / updated database (maybe you added new items?) they would not appear on the target device since once we created the file in the peristentDataPath, we would continue to use that one.
One solution is to seperate out the actual database data you specify for your game and the data that is changed on the user's device into seperate files. That way you can keep your database inside your project (load it from a TextAsset or from your server or whatever) so your database is always up-to-date. When the user buys something you just record this in a seperate file in the persistentDataPath where you just "link" to your actual database entries. We don't know how you organised your items in your database, but usually each item should have a unique id or name. So in the persistent datapath you would just need to store the bought-state for each item (plus whatever else information you need, like the date / time it was bought).
So when loading the database you would always load your internal database that is never changed on a user's device (only you could update it) and you would augment it with the data you stored in the json file on the user's device.