Reading a JSON file - an easy how to with Unity3d

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this tutorial you're going to find out how to read a json file and put it into variables in unity to use within your game this is really useful because if you had a for example a set of different characters in your game and you wanted them all to have different statistics you could store them all in a json file and then read them in and that way it makes it really easy for editing and testing to tweak different things like the amount of health of a character without having to work out where it is within your project so let's get going the first thing that we're going to need is our json file i'm going to put this in the resources folder i want it just to be a text file so i'm going to show just an explorer i'm going to create a new text file and i'm going to call it json we'll edit that in visual studio so we'll just leave that there for now i also want to add a script [Music] and let's just call it json reader so let's open both of these in visual studio now before we write our script let's just make a json file now these are really format dependent so you need to make sure your formatting right they're also case sensitive so you start with the curly brackets you then put what is going to be the name of the class that you're going to read it into then you use the square brackets and in between the square brackets is where your data goes so far i'm gonna have three bits of data i'm gonna have my name and that's gonna be a string it's important to note that json is only uses the double quotations not the single quotations auth is going to be an integer so i don't need the quotation marks around the 100 and you don't need a comma after the last entry now i'm just going to duplicate this three times so that we've got more data you also only need the comma if you've got another entry coming so we'll delete that comma and that's your completed json file let's save that and let's go and write the reader now the first thing we'll need is simply a reference to that json file because it's inside our project we'll just choose text assets [Music] next up we're going to have to create a class that has the name the health and the manner in it we're also going to want to use system serializable and that will make it so that the json file reader can actually see all of the different fields [Music] but this is okay if you've only got one person but we add three so we want to make another class which turns this into an array [Music] and you can see here we have an array of these so now in this array we can store each of our different entries in the draft from the json file finally we just need to make an instance of this class that we can use to read it into [Music] it's important to note that this player here is the player at the top of the json file and it's case sensitive so you need to make sure they're the same if they're not the same you won't get any errors but it won't read in the data so it's a little bit of a you know catch so if you're not getting your data being read in just make sure this is the same and make sure all these names are the same now we're just going to read this file in the start function and we're going to use the json utility to do this and we use from json we send it the name of the class [Music] and it's asking for a string and that string is our text asset and remember to get the string from a text asset you have to use dot text accidentally deleted the system.serializable here so let's add that in and now let's save this and go give it a test in unity i'm going to create an empty object it'll drag my script over onto it i'll drag our text asset into the appropriate place and now you can see you've got your list of players you've got no elements at the moment let's hit play you can see the three elements have come in and you can now assign these however suits your game if you're looking to do more complicated things with json there's a newton asset that's inside the store and it's only adds 300 kilobytes to your program and it is more robust than the default one but if you're just trying to do something simple the default one is fine they use the same name space but if you want to upgrade to it it's actually really easy to do that i hope you enjoyed this tutorial and if you found it useful i really appreciate if you subscribe like or comment really helps me know if people are liking the videos and i should make more hope you enjoy your game development [Music] you
Info
Channel: Destined To Learn
Views: 68,043
Rating: undefined out of 5
Keywords:
Id: XbdnG__wzZ8
Channel Id: undefined
Length: 6min 26sec (386 seconds)
Published: Tue Nov 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.