Unity3D API Request - Get & display data from the internet

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video we're going to learn how to create an api call within unity3d engine and the purpose of this is that we can hit this refresh button we'll make an api call and we'll be able to display some text here to replace hello world then every time we refresh we'll get different text so the api we're going to use for this is called uh cat fact and the link to this is down in the description below and if we do cat fact forward slash cat fact dot ninja four slash effect you get a random fact about cats so we'll be using this then to display within our project and then we're gonna use this uh json to c-sharp converter as well to turn our json into a c-sharp object so we can access the data more easily and manipulate it as we please within our application so let's just go ahead and get started with our uh script so i created a new script called api call and in here it just is a generic script but we're going to remove the update function and then we're just going to add some dependencies up here so we're going to do using newtonsoft.json and using tm pro and then lastly using unity engine dot networking and that's all we need so down here we're going to create our function that's going to create the it's going to do the api call so it's going to be an ienumerator and we're going to call it a get request and we'll pass in a string and i'm just going to call it uri then from here we're going to do using a unity web request web request equals unity web request dot get and pass in the uri so once we do that we're going to do a yield and return web request dot send request so this will wait for our web request to send and then after our web request is finished has responded we can do a switch on the web request dot result and then here we can do some error handling so we'll do case unity wave requests and then we'll do a data protocol or data processing here and we can just do depot.log air and say something went wrong let me just string that format something went wrong and then here we can do our web requests.air i don't know where that went and then from here we will just break so that was our error handling so basically what this switch statement says right now is if we have a connection error or data processing air we're going to basically log in air and it will show up in the console and but then lastly we'll just do a case for unity webrequest.result.success and then in here we're going to basically say what happens there we go so in here then we can actually handle what to do with the text or the response so if we head back over to our tab here that has the actual fact in the json structure if we copy this paste it into json to c sharp it will create us a c c-sharp object and this represents the json data that will get returned so we're going to add this into our project and just rename the class from root to fact just for easy to read and now when what we can do is we can basically turn our json into this object and then access the fact or the length um every time uh and you can use this for manipulating there you can use in the list and get loads of them or build up a list of objects and just an easy way of accessing so from here we'll do fact fact equals json convert dot deserialize object fact and then we will pass in our web request dot download handler.txt so with this json convert deserializing it this is how we transform what essentially would be this text and then we convert it into this object because it's mapped correctly and as you can see it has the field properly there's fact fact length length so it maps them correctly and then we can just access their properties by doing fact and factor factor linked and but we're going to use this to display on our actual screen so we need a public public text mesh pro for you ugly text and then down here we do text.text equals facts. and now every time we call this this will update our text in the canvas to display the result so let's give that a try we'll come back out here i'm going to put the script on our main camera so here's our script you can see we're looking for some text so i'm going to grab the text mesh pro from over here and we need to do one last thing in the script we actually need to start 14 get request and then we need to pass in the url so let's go back here uh cat factor ninja forward slash fact is what we put in here and now on startup we should change hello world to actual fact there we go so we try that again to get a different fact there we go and then our refresh button is not going to work just yet because we didn't set that up so let's get our refresh button to work we're going to do a public void on refresh and we can just call picture we just call start instead of duplicating code so once we do on refresh we call start does it start our quarantine and get us a new result so we need to go to our button uh add an unclick event and reference an object so we're going to drag our camera into here and now we can access our api call class and then our on refresh so now we click that button it should refresh the result that we see so here we have one result and now we are getting loads of different results from the api and that is how we can create api requests within unity and i hope this video helped and if you need anything at all just put a comment down below and i'll be sure to get back to you as soon as i can thank you very much don't forget to rate comment and subscribe take care
Info
Channel: Scott Durkin
Views: 18,100
Rating: undefined out of 5
Keywords: Unity, Unity5, Tutorial, UI, Video Game Development (Industry), Software Development (Industry), How-to (Website Category), How to, User Interface (Speech Topic), Software (Industry), Video, RequiredComponent, Unity (Software), Video Game Culture, api, api call, data, server, internet
Id: cU4UaqrPb84
Channel Id: undefined
Length: 9min 4sec (544 seconds)
Published: Thu Jul 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.