How to Use UnityWebRequest - Replacement for WWW Class - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on everybody and welcome to this tutorial video warmly showing you how to use the new unity web request class this is kind of replacing the old unity wwk s when I went to use the www class I saw that it had been depreciated by unity and in favor of this new class called unity web request and this just kind of has a lot more functionality and it's just overall better to use so with the new unity web request class there's a high level API and a low level API the low level API is a lot more configurable but it's a lot more complicated and it's kind of geared more towards advanced users that's not what we're gonna be going into into a today's tutorial video we're gonna be going over the high level API which just kind of provides a nice clean wrapper for all those lower level functions and so it's really easy to use so if you've never done any web calls with unity this is a fantastic place to start however if you are familiar with the WW class this is also a great video for you to watch because I'm gonna be kind of showing you what some of the main differences are between the old wwk s and the new unity web request class so in this tutorial I'm gonna be showing you how we can do get and post calls with the unity web request class so with our get request we're just going to be calling a PHP script that looks at just a basic little text file to grab the highest score from it and it will return it to our game and then with the post request we're going to be calling another PHP script setting up on our web server and that will actually kind of do a little comparison to see if it is greater than the current high score if it is it will replace the contents of that text file with the new high score and then we'll return the result back to the user so in this tutorial video I'm primarily going to be focusing on the unity and c-sharp side of things but I'm just gonna kind of show you what my PHP scripts do real quick in this tutorial video I'm not gonna be showing you how to set up a web server or put files on to it or anything like that but I will you leave you some resources down in the description below if you still need help on doing that and I also have a link in the description below which has all the resources from this project so you can kind of download my sample project and mess around with it yourself and then lastly I just like to say if you found this video helpful and you enjoyed it make sure you leave this video like also feel free to subscribe to the channel for lots more unity tutorials like this one and if you have any questions or comments or suggestions for future tutorial type X feel free to drop those down in the comment section below and without further ado let's jump on into the tutorial ok so here we are in unity I'm just gonna give you a quick overview of my project and then kind of show you what we're trying to accomplish with this tutorial video then I'm going to show you how everything works so basically we just kind of a bunch of text objects here first we have this text where the server messages will actually be displayed and we have a button for getting the highest score and we have a button for sending the score and whatever we have in this input field here is actually going to be the score that we're sending up to our server and so the only other really custom little thing I have is this web controller game object which the only thing on that is this basic web call script that I have and we have some public fields for the message text which is going to be just this text here that we're going to be displaying messages back to our user with and then we have this score to send input field and that's just a reference to this input field right here so we know what score to send up to our server okay so if we go over in a play mode here we can see we're actually trying to accomplish here so you'll see that this message at the top just changes to kind of prompt the user to interact with our game so we can get the current high score just by clicking on this button you see it downloads the data and says current high score is set to 18 now if we want to enter a new score that is higher than the current score let's say enter 25 we can send that score you see it sends up to the server it says new high score updated successfully high score is 25 and we can just validate that by clicking on get high score you'll see it downloads the data and the current high score is set to 25 now if we try to do something lower than the current high score like 7 we can send that up it says 7 is not a new high score high score is 25 so again it doesn't change anything if we do a get high score again it's set to 25 but again if we do something bigger like 72 and send that data up to the server the new high score is updated to 72 and so we're doing all that just with one simple c-sharp script which I'll be going into in a minute but real quickly I just want to show you my two PHP files that I have up on the server so first is the get one all this does is it uses this file get contents method and it just gets everything that's sitting in this high score txt file which is also sitting up on our web server so you'll see that the only thing in this high score dot txt file is just this representation of the high score which we actually set in unity over here so again it's just a pretty simple file and then we just use this echo command and this just kind of returns this string back to c-sharp so also show you how we can actually take this string here and print this out in unity so when we go over to say get high score this current high score is 72 you'll see that that's coming from this PHP script right here now the other one is the post one and this is a little bit more complicated but don't get too scared it's just kind of doing a couple more things here so first we're just gonna have a variable reference to the high score dot txt this is just keeping a name of the text file that we're using because we're gonna be using it a couple times so the first thing that we want is we're going to grab the current highest score again we're going to be using the file get contents passing in the name of the highest score dot txt file so the same thing that we're doing in the get PHP script but the only difference is we're casting this to an integer because we want to do one quick little integer operation on it now here's kind of the important part for using c-sharp and unity and everything like that so we're gonna get this current score variable and of course we're gonna cast this to an integer again and we're going to use this dollar sign underscore post and this POS T all caps and then we're gonna have the open and close square brackets here and inside it we're going to be looking for the current score key so I called it curse score key you can have this any thing you want but you'll see that we're actually setting this in our c-sharp script I'll get to this in a minute but basically we kind of have like a key value pair and this post method basically just looks for this curse Corki and then basically this post kind of key word here what this does is it returns the value that is associated with the curse Corki so the curse score is set to whatever we're sending from unity so hope that makes sense but again ask me below if you need me to clarify at all so then we're just gonna do a quick little comparison to see if the current score is greater than the current high score if it is then we'll do the file put contents method and so this is so much of the get contents method but it replaces everything within our high score file so we'll just pass in the name of our high score file and then we'll pass in what we want to replace it with and we just want to put in the current score because that'll be the new high score and then again we use this echo command to send a message back to the player saying that the new high score updated successfully and then print out the value of the high score and we have this else here so this is if the current score is not greater than the current high score so it just says it prints out the actual number of the current score it says is not a new high score high score is and then it prints out the current high score so again these are just some extremely simple PHP scripts to show you how to use the unity web request feature so now we'll actually go over into c-sharp and I can kind of show you how things work over on the unity side so of course these top three these are included by default when you make a new script but we also need to include unity engine networking to use kind of some of the web stuff and then unity engine UI so we can kind of do some stuff with the text and input field stuff just kind of on the screen right here it's like I mentioned earlier we're going to have a public text field for a message text and this again is just the message that is being printed out to the screen here we have the input field for score to send which is just this little input field right above the send score button now here we're gonna declare to read-only strings for the get URL and the post URL so the get URL is the PHP script that we're calling when we wouldn't do the get function again this is just this simple little script right here which returns the current high score and this of course is the PostScript which attempts to upload a new high score to the server so just go ahead type in the URLs here and we have these read-only so that we can guarantee that these don't change at any point in our code for whatever reason like that and I should just mention that make sure that you're using your own PHP scripts for here because by the time this tutorial posts I won't have these files up on my web server anymore so you won't be able to use them alright so kind of going through some of the order of operations here first thing that we're gonna do is just change the message text to press buttons to interact with the web server again just to prompt our user to interact with our application so next we have a public void function for on button get score which is the function that's called when we click get high score and so all this does is it changes the message text to downloading data and you'll see that when I click get high score it just kind of pops up really briefly and the reason we do that is because web web requests can kind of take a little bit of time sometimes they can take up to a couple of seconds but basically the reason we do this is because we want the player or our end user to just understand that the application is doing something in the background and it's not just like frozen and then we'll call a start co-routine for simple get request now for a unity web request these all have to be inside co-routines which we declare with this I enumerator keyword here if you don't know what a KO routine is that's okay basically it's just kind of a way to kick off some action and when that action completes then we'll kind of return to our code and continue executing again this is because it can take you know a couple seconds for our web request to actually take place and so we don't want like our whole entire game to freeze just because we're looking up a high score which could take a couple seconds so to actually make the unity web request will declare a unity web request and we can call us whatever we want I just call it W W because that's kind of what I'm used to with the w w class and all we need to do here is do unityweb requests get and we just pass in the get URL and so this kind of sets up our unity web request and then we actually send the web request with this line here or we say yield return ww send web request so this yield return this is kind of the KO routine part of it where it actually send the web request off and then once the web request is returned we get a response from our server then it will continue executing through the the rest of our code routine here so once we've returned first we just want to check to see if there's any errors if there are any errors it will just print it to the debug console so if you're having any problems with this make sure you just check the debug console because it usually has pretty descriptive mess messages of what's going on here otherwise if there's no error we're just gonna set our message text 2ww download handler text and so this is basically just the reply that we got from our echo command here so this echo here it sends it back to our code here but with this WWll handler text and then we can set that to our message text so this is actually where it would display here now real quickly I'm going to show you how to send a score with the post command very similar to the get command but there are a couple small differences that I'll go over here so the first thing we just want to check to see if the score to send input field is empty if it is empty we'll just kind of give a little error to our player in that message text otherwise we'll change the message text to sending data and again this is just to kind of let the end-user know that something is happening in the background and that the you know application isn't frozen or something like that and to just wait a second for the operation to complete now once we do that we'll just do a start co-routine of the simple post request and then we're actually going to be passing in just the raw text from our input field here so this is the score to send text now here is the actual co-routine where we're going to be doing our simple post request so this takes in a string for the current score and the reason that we have this as a string for the current score set of an integer is because when we get down to the key value pair both parameters are string value types now if you are familiar with the old unity WW stuff when we're doing post requests you may remember that we kind of had this thing called a WW form and we'd attach that to kind of our w w class and then send that up to the server now it's slightly different we actually use a list of type I multi-part form section and I called this www form because that's kind of what I'm used to with the www class stuff but of course you can name this whatever you'd like and so we just set this to a new list of I'm on type or form selection and so in this case we only need to add one thing to this list so we just do WWE addy and here we're going to do a new multi-part form data section and then we'll pass in our key value pair so our key is gonna be curse score key and the value is curse score so again you'll remember over on our PHP script here with this post command we're looking for this curse core key and it will return the curse score value to this variable here so again it's just a simple key value pair lookup now here's where we actually create the unityweb request so we'll do unityweb request we can call us whatever we want and then we'll do unityweb request dot post we'll pass in the post URL so it calls the post PHP script and then we'll also pass in this WW form which is this kind of list that we created here and then same exact thing as before we just called you overturn WWN web request if there's an error will print the error to the debug console if there's not an error will print the results just right to the screen and so that's pretty much it real quickly I'm just gonna kind of show you the operation one final time here so when we start it of course we get prompted to interact with it here we can get the current high score high score is currently set to 72 if we did something a lot less like five we could say send score it says five is not a new high score high score is 72 but if we wanted to do a higher score or like say 80 we could send the score it says new high score successfully updated high score is 80 and of course we can validate that last time by saying get high score here it says current high score is 80 so in ways that you're gonna wrap up this tutorial video here on how to use the new unity web request class of course if you have any questions for me feel free to drop them down in the comment section below but if you did find this video helpful and you enjoyed it make sure you hit that like button also feel free to subscribe to the channel for lots more cool unity tutorials like this one once again if you have any suggestions for future topics that I could cover feel free to drop those down in the comments section below as well anyways I hope y'all a fantastic rest of your day and I'll see you in the next one [Music]
Info
Channel: Turbo Makes Games
Views: 36,011
Rating: undefined out of 5
Keywords: independent, video, game, development, video games, video game, indie, dev, games, gameplay, design, gaming, indie dev, solo dev, programming, programmer, unity, unity3d, “indie, games”, game dev life, game dev lifestyle, 1GAM, 1 game a month, devlog, process of making a video game, how to make a video game in one month, making games quickly
Id: nVz3GBw1kDg
Channel Id: undefined
Length: 16min 44sec (1004 seconds)
Published: Tue Jul 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.