JSONUtility - A Better Way to Save Persistent Data in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's see a proper way to save data with a Json utility class all right we found this back in the unity once more and in this tutorial we're going to be looking at the Json utility class and how you can save data to a Json file basically using that and for this we will once again use the card display over here that I've already prepared a link for this as a Unity package is linked in the description below both with all of the code that we're going to write in this tutorial and without the code so you can do either or let me just duplicate this over here because this is going to be 9 and instead of the playerprof chords this is going to be the Json utility cards there we go and let's go so in our save data over here instead of using the save data for the player prefs that we've used the last time we're gonna make a new class and that's going to be the Json utility let's just create that C sharp plus so we're just going to call this the save data Json let me just add the class here to the save data and then let's take a look at how we can create this so in this case we have this player data class over here that we basically wanted to save now the cool thing about the Jason utility class is that you can just pass in an object and as long as it has public Fields those fields are going to get populated and basically generated into a Json file really freaking useful what we can do here is we're going to make a private layer data so this is just going to be our player data right here this is going to be equal to playerdata.instance because that's just the way that I set it up basically so that we populate this with with some basic data over here and then we're going to need two methods the first one is a public void save data method which is of course going to save the data and then a public void loads data you can see it actually even suggests this to us absolutely awesome and let's start with the saving of the data so the idea is that we're going to get a string and that's going to be our Json string and funnily enough it actually suggests what we need to do exactly this we want to call the Json utility class for the two JS method you can see that if I hover over this it generates a Json representation of the public fields of an object and because our player data has all of the fields that we want to save public we can basically just pass it into this and there we go go what we can do here for the sake of argument is just output it before we write it into some data let's just do that and then the question is how do we write it to a particular file well we're going to do the following we're going to say using and inside of this using we're going to use a stream writer here in this case I'm going to call this the writer this is going to be equal to a new stream writer now this is not quite right because here we're just passing in the Json that's actually not quite right because what we want to do is we want to make sure that this is saved inside of the actual project path and that is going to be done with by using application.datapath and I'm going to do path dot alt directory separator character and then the name of this file we're just going to call the save data.json there we go and now inside of this using you can see it actually suggests this to us although what I want to do is I just wanted to use the right instead of the right line and then just writing in the Json string right here and that's going to do well exactly what we needed to do and namely make a Json file of this so we can actually take a look at this already let's switch back to Unity over here you want to make sure in the pan channel right here for both the save and the load button that we actually connect the save data over here with the save data save data method and then the load one with the load data method even though that doesn't do anything just yet that's the load data method just making sure that this is set up in the package with the code this is of course already done and now let's start the game and we can see is if we now save the data you can see we're saving this data and under the assets right here we even got the save data and you can see we saved the HP the MP the XP and the card index and the same thing of course works if I change a card over here and I'm going to say four five two one five two and like eight six something I'm Gonna Save this and then you can see it change it here you might get this warning right here this is because this has not reloaded so even if I click on this again you can see this is not done if I right click and refresh this you can see now it has been done sometimes it might not update inside of the project so do keep that in mind but in theory if you don't have to select it and you were to change something in theory if you then go onto it you don't get the error but you still have to refresh this for this to change range overloading it should actually work regardless of refreshing your project so let's take a look at that as well so for loading the data of course we once again have a string Json over here which is just going to be an empty string for the time being and then we're going to read this in so we're once again going to use the using over here but this time a stream reader in this case this is going to be the reader and this is equal to a new stream reader passing in application.datapath path dot alt directory separator plus save data dot Json making sure that whatever string we're passing into the stream reader is the the same string that we pass in right here otherwise of course it's not going to work and then Json is equal to the reader.reach to end this is exactly right and then here we make a new player data so there's going to be player data called Data and that's going to be equal to adjacentutility Dot from Json and you can see it suggest this to us of type player data passing in the Json and then here this is extremely important but we've created this right here we still have to then call playerdata.set player data the reason why I'm using this is because this actually prompts the UI to change as well if I were to just do layer data equals data then the UI wouldn't change so keep that in mind so we want to do set player data and then just basically doing all of them manually over here MP we then want data.xp and lastly data dot Port index basically setting up the player data here with the new information and now going back to Unity we can basically immediately just load the data so if we click low data you can see it loads the data absolutely no issues and once again we can change them out here so let's say 19 and then this is going to be 55 15 and maybe like 87 and we can save this again going out of the game going back into it loading it again there we go it all works now there might be still some drawbacks to the Json utility namely of course that well it's just a Json file so in theory you could go in there right so a malicious actor could just double click this open it in any kind of program and then say well actually I want the card in next to be five and I want XP to be you know a very very big number and HP to be like one save this and all of a sudden if I were to reload this right it changed over here loading bam all of a sudden the data has been sort of corrupted or has changed now what this could be considered an issue it depends on really what game you're going for right I mean any online game is going to have the data saved on the server anyway so it wouldn't be accessible by players and also there should probably be a little bit more of a sophisticated way of saving this rimworld for example saved their data with XML I don't know if this is still currently the case however that is how they did basically save everything like the entire file has just been saved with XML it is a similar format to Json a little bit more cluttered basically but the ideas Remains the Same and that's also totally fine in theory you could also create some sort of checksum with your data so maybe that it saves like the hash of the actual file right so the contents of the file are hashed and then that is saved in the file as well you could do something like that and then basically it checks against that and if someone has been altered then it basically says hey this is no longer a valid file you could do something like that as well to double check although it shouldn't make too much sense to worrying about that as of this moment but as always of course all of the assets are linked in the description below and if you've ever wondered how to do some a smooth camera follow in a 2d take a look at this video right here hope to see you there so yeah
Info
Channel: Game Dev By Kaupenjoe
Views: 2,101
Rating: undefined out of 5
Keywords: kaupenjoe, unity tutorial, unity guide, unity 2d, PlayerPrefs, PlayerPrefs Unity, Unity Saving Data, JSONUtility, Persistent Data Saving
Id: Wu4SGitck7M
Channel Id: undefined
Length: 7min 17sec (437 seconds)
Published: Thu Aug 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.