Serialization of Values and Dictionaries in Unity | Json.NET Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there Blaze here and in today's tutorial I'm gonna show you how to serialize dictionaries and other complex objects to Json format in unity then we're gonna save this data to file and also load and use it inside the project there are currently two main methods to do this in unity first is by using external library and second without using only what Unity provides for us I definitely recommend to do this using external library and this tutorial is about that but I know that sometimes you can't use external libraries so stay tuned and check my channel second tutorial will be coming soon one more thing before we start today's tutorial covers only standardization to file so we will not display dictionary in inspectors window if you are interested in that wait for this second tutorial okay let's begin a quick reminder that finishing projects from this tutorial is available on my Pioneer Coffee page so if you want to obtain it you can buy it here prices starts from one dollar of course I will show you the entire process of coding in this video so think about this product more like as a support for my work thanks and let's begin so as I said we will be using external Library so the first thing is to go to newtonsoft.com Json website and this is the library we will use json.net this is Free Library which is provided to us using meet license and that means that we can use this project inside both commercial and free projects okay times ago there was a project called Newton soft Json for Unity but currently when you go to the site to this rep on GitHub you will see that science late February 2022 Unity has published an updated version of their package and this package is called Community nugget mutants of Json we need to copy that go to Unity window package manager click this plus icon and add package by name we need to pass it here we can remove this version here click add and now we have Newton's of Json package installed in unity currently I'm using version 3 power in 2.1 which is from May 2023 and this is good version for Unity 2022 okay so now we have our package installed and we will create some scripts and I will show you how to serialize this using external Library let's create the script and let's call it serialized data and let's open it in the visual studio or another your favorite IDE we can remove the mono Behavior inheritance or usings and the content of the class data in this class will be serialized later let's create another script and let's call it realizator and also let's open it this will be money Behavior so we need to add this script to the object on the scene so let's create new object called serializator and let's add components to realizator save the scene okay switch back to the visual studio we can remove unused usings and the content of this class we will actually call serialization and the serialization in the start function here so we can add it and we need two other functions one for serialized data and one for the serialized data and this real nice okay so we have our serializator class so we start and serializing the serialized data let's go serialize data in the start function and then we will call the serialized data and this is all we need to do in start now we will do our serialization but we need to have something to serialize so let's create some fields and let's start with basic types go with private end number five private load decimal number and for example three point 14 let's also add string name John Smith let's add a list and let's set the list of integers for example integers they will representing I don't know what but this would be integers so let's create new list and add some objects 567 for example we can use short new and let's create a dictionary for example screen and string custom fields and we can initialize it in the serialized data Constructor custom fields at and let's add for example phone number and set to whatever it is and maybe one more field email and let's go with John smith.neve.com okay so we have our data and this data should be serialized so to serialize the data we need to use external libraries but I will start with implementing this using default a Unity tool set so I can show you the difference between unity serializer and Json serializer from Newton software so let's create VAR Json we will use Json utility to Json and we need to push the object new serialized data we just create a copy of serialized data with all these fields filled in this way and now we need to save this to the file for example write all text and do application data path slash save data.json this will create this file in the assets folder so we can easily find it but if you use it in your game you should use another location maybe you can use application.persistent that path okay and in this realize data we need to read from the file read alt text from the same path of course and now we have this Json we can do with with some manipulations but I will just display it in the debug log so we can check this from the console if everything is fine okay so let's switch to Unity and start this application and as you can see our return Json is empty and if we go to the finder or File Explorer on the windows we have the save data Json file here let's open it with Visual Studio code for example because Visual Studio code have good formatting for Json files yes we have empty Json and the reason of this is because in serialized data we have all these fields private to serialize this fields we need to make them public for example or serialize fields and we need to import Unity Android then let's play the game now we have a number here let's switch here number five and let's back to this as you can see we have only this number added and the reason is because this serialized field is in class that is not set as serial is able you need to set sorry residual here then and then when the class will be serializable and the field will be serialized filled when we click play we have both numbers added here on the console and when we switch here format this we have this both number so you can use serializable class with server disabled private fields or you can use public Fields this works the same I will go with public Fields now because I want to keep this tutorial simple public fields are not as good as private serialized Fields but in some simple classes which is used only for containing data and public fields are probably okay okay so I set all fields to the public and now I go to the unity and what will happen when I click play and let's open the Json file as you can see we have number decimal number name and integers but custom fields are missing this is because standard Unity Json tools and not able to serialize and the serialize in dictionaries so when we want to use Newton sub Json Library all we need to do is change this line Json to Json to Json convert from the Newton soft Json Library serialize object and we can push this and then when we will save this back to the unity and click play let's open this file now we also have custom fields in dictionary which is something like the sub object here and this sub object have phone number and email filled with this content okay so this is about serialization this Json file which is here can also be this realized so let's make the object from it serialized data and to this realize this we need to use Json convert that industrialize object and we need to specify type type is serialized data and we need to push the Json now the only way I can show you that this object is industrialized that is just displayed with with the debug function and now here you have this Json string and here you can see that we have serialized data and that means that this object was industrialized to this object we can for example display the name from it or any other field and when you click play you have John Smith in this place so you can see that name is working and let's check also custom Fields so you can see that the dictionary is working also and the name of one of the fields is email and when we run this we have this this realized and we have John Smith at smith.com so as you can see this is the easiest way to serialize and deserialize dictionaries this also works for some other fields you can use also views probably let's set you off int q and let's nq2 and then five when we now go with the serialization so we need to play the game and check the file you can see that Q also is serialized as the ri mutants of Json is smart enough to set it to the type it should be dictionary for example is serialized as sub-object and view is serialized as the array okay so this is probably all about serialization using mutants of Json Library the easiest way to sterilize dictionaries but if you really don't want to use this library or you can use this Library the next tutorial will come in the next week or two so stay tuned and check my channel hope it helps or if you have any questions write it in the comments and see you next time bye
Info
Channel: blazeDev
Views: 2,989
Rating: undefined out of 5
Keywords: Unity serialization, Json.NET, Unity data management, Unity saving data, Unity loading data, Unity dictionaries, Unity value serialization, Unity, game development, tutorial
Id: AxHFJWgu5FM
Channel Id: undefined
Length: 9min 36sec (576 seconds)
Published: Mon Aug 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.