How to Use RESTful APIs In Unity [Unity Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys mike here from comf3 interactive welcome back to the channel today we're going to be taking a look at restful apis in unity we're going to take a look at how we do it why they're useful and a couple of examples of how you could actually use this in your own game now there's a few things to note if your game is going to be using an online api call then it's going to need an internet connection if it doesn't you can't get this data so just bear that in mind and the one that i've selected is going to be of absolutely no use to anybody but this is just for an example and we're going to be using chuck norris dot io and what this is going to do this is going to return a json format response from the api and it's just gonna have a chuck norris joke so like i said this is gonna have absolutely no use in your games whatsoever unless your game revolves around chuck norris jokes then go ahead have at it but this is just to show you very simply how you can go ahead and make these get calls from an api so there's a few things that this api actually tells us in its documentation so the get request needs to go to https api dot chuck nourished io jokes slash random so if we click that we should actually see the json that's being returned but that's all well and good we can read that but we want this in our game so let's get working on it but before we start i just want to thank gigatank3000 for sponsoring this video i've got his links down in the description below go and follow him on twitter check out his website he's got a new game in the works that i'm sure you're all going to enjoy so go and give him a follow do him a favor and i also just want to thank everybody supporting me over on patreon i love you guys okay so let's just take a look at what i've got here i've just got a standard button and a text mesh pro element the button is going to generate a new joke and the text inside the text mesh pro element is just going to display it really simple stuff and the script for that i have a method for a new joke and inside here we're going to set our job text to whatever we've got as our response and that button is linked up to our new job method now we're going to create a new c-sharp script and this is going to be our joke before we make any calls we need an object to actually pass our json response into so if we take a look at that example response we can see that we have different keys we have a string array called categories we have another string called created art we have icon url id updated at the actual url for the joke and then the value which is the joke itself so we need to create an object that this data can be passed into so inside a joke we don't want this to be a mono behavior we don't need any name spaces and we also don't need our start and update what we do need though is for this to be serializable we can add the system dot serializable attribute above our class and we're good to go so now all we need to do is create public variables that match the key names inside of the api response now save typing all of these out i've got them right there so as you can see we've got the string array for categories created art icon url id blah blah blah now value is going to be the only one that we're really interested in so we don't actually need those the call would still work because it can populate the value but it's always good practice just to have your complete api response mapped just in case you ever need one of these other fields so we can save that now that we've got the jolt class we can actually make an api call and get a joke so let's create one more c-sharp script and this one i'm just going to call api helper now this script is going to have absolutely no bearing on the unity engine whatsoever so we can make this static so we'll get rid of mono behavior get rid of the start and update and we'll make this a static class and we're only going to need one method in here and that's going to be public static and it's going to return a joke i'm going to call this get new job now inside of here we're not actually going to need our system name spaces but we are going to need to be using system dot net and we're also going to need to be using system dot io system.net because we're going to be dealing with http web responses so we're going to need that namespace in there and system.io because we need to instantiate a stream reader to actually read our response now this is actually going to be a relatively simple task because we're not actually going to be doing much with this response we're just going to be displaying the value out on screen and that's it so let's get to actually reading this so the first thing we're going to need is a http web request so that's our actual request that we send out to gather the information so call that request and we'll set that equal to webrequest.create and inside here is where we need to pass the url that's going to return this which is this so every api will have its actual target url that you need so we just copy that and we pop that inside as a string now as you can see we have this error because currently we're using web requests but we need this to be passed to a http web request so we can just go ahead and cast that like so so now that we've actually got our request we can interrogate that and find its response and the way that we do that we set up a http web response call that response and again we need to cast this we'll just do this straight away http web response and that's going to be our request dot get response simple as that now we need to read that response and the way we need to do that we need to pass it through a stream reader so this is where our system.io namespace comes into play so we can just create a new stream reader call that reader set it equal to a new stream reader what we want to pass in is our response dot get response stream so now we have all our data locally inside of our stream reader all we need to do is get the string json response from it and then pass that into our joke so that's two more lines of code so we need to create a string we'll call that json and we'll set that equal to our reader dot read to end so that's just gonna grab all the text value all the string from our reader and give it to us as an actual string and at this point json is going to look exactly like this this is going to be our data that's been returned so now the last thing we want to do is make that into a joke object and the way that we can do that is we can just return because remember we need to return a joke i'm going to use unity's json utility you can use any json utility library that you like this works perfectly fine for me and we'll call from json i'm going to want to pass in the type that we want to pass it to which is joke and then inside the parentheses we want to pass in our json string and that should be it so we're creating our request we're getting the response from that request we're getting the value stream into a reader from the response we're passing that reader into a string called json and then we're going to pass that json into a joke object so the last thing to do is actually call that method so inside of our new joke all i'm going to want to do is create a new job i'm calling it j set that equal to api helper dot get new joke and then we can set job text dot text equal to j dot value remember value is our actual joke in the object now if we pop back over to unity we can see that my button is hooked up to new job so if we run this and press new job we should see a new job pop-up perfect chuck norris can play the violin with a piano brilliant and every time we press start we're going to get a brand new chuck norris jog so i hope that clears up a few questions that people may have had i have been asked this question multiple times how do i make a api call from inside of unity and there you have it like i said at the start of the video just remember that this requires your game to have an internet connection and also depending on the api that you're using you may have some limitations with that so make sure that you find out all that information prior to committing to put this into your game so that's all i've got for you for this video i'll see you again soon thanks for watching guys don't forget to like comment and subscribe and follow us on facebook and instagram for more bites as unity hints and tips
Info
Channel: Comp-3 Interactive
Views: 8,150
Rating: undefined out of 5
Keywords: comp3, comp3interactive, unity, unity5, unity3d, unity2d, tutorial, game, development, how, to, dev, introduction, program, programming, code, coding, csharp, c sharp, c#, javascript, games, develop, 3d, 2d, artist, programmer, editor, extension, easy, beginner, advanced, professional, course, tutor, series, multiplayer, networking, uNet, design, android, mobile, tip, tips, tricks, rest, restful, api, api call, net, web, web request
Id: XIbZDz_p6vE
Channel Id: undefined
Length: 10min 23sec (623 seconds)
Published: Fri Dec 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.