How to use Firebase Database in Unity (REST API)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and welcome back to Unicode ever today we're gonna learn how to make a database to store all of the information you want for your unity game and as a database we're gonna actually be using firebase database this is the one I used for the application and currently developing locked if you wanna check it out there's gonna be some videos in the description and yeah so let's get started so the reason I'm doing this video is because when I try to implement a database even if it's not that hard of a task I had a lot of obstacles and they actually took me a while to do because I did some very silly mistakes so with this video I wanted to show you the way step-by-step on how to make an actual database and how to link your unity application with your database post and get information from it so this is what we're gonna do today so since we're using firebase I'm just on the firebase website as you can see I just said I checked you know search the firebase on Google and it is where I end up whether I go to console and we're gonna actually create our project alright so once we are in the console we actually have to add a new project firebase does a bunch of stuff does authentication fire database even cloud functions today we are going to only be using firebase database though now you can give a name to your project in my case is gonna be YouTube project and this is gonna be the project ID you can actually edit it to whatever you want - so like YouTube project but in this case we're gonna just leave it be oh my gosh I messed it up and then you can do I accept and you can create the project now I've reached the limit for the project so because I'm crazy you can only have an 8 project in the free plan but right now I have a project that I actually don't use and I'm gonna be using that as a database but if you're new to the website you're gonna just be able to click on create a new project so once you've created your project you're gonna be here my project is called guess my phrase but don't worry about it it's just a test project that i created a while back so what you're gonna do is gonna go to database and as you can see it is gonna be our amazing database and we have to create it so let's click on create and now we have to choose if we have to start it in locked mode or test mode so I always suggest to start it in test mode so everybody is gonna be able to read and write to your database but naturally once you release your game to the public you always have to you know add some security so that nobody or at least not everyone can actually access your database for you know data security so we're starting test mode and then maybe when in another video I'm gonna show you how to implement an authentication system we're gonna switch the to lock mode but this is not the purpose of this video this video is all these videos only purpose is to actually create a database and you know actually do a get or a post request to do it so we're gonna set up test the security rules so everybody can read and write to your database so now that we are here we're actually gonna switch to real time database because if this is what we want and now we're gonna have this useful link right here so this is just a note I'm at the imposter editing because I noticed a problem with the database so I know we initialize the database in test mode but sometimes firebase is starborn and basically doesn't listen to us so make sure to go to rules and check if the rules are actually true the they both have to be true read read and write both have to be true if there's not you can just right through you be sure to spell it correctly and true and you publish remember that before you know publishing the game to the public you actually have to add some security to the database as I said but for now for testing these two values have to be true or we're not gonna be able to access the database so now we are completely done with setting up firebase what we need to do is actually open unity and unity and do everything there and actually build the code that is not actually gonna post to our database which is also pretty simple so now we're gonna actually create a new unity project if you already have a game naturally use that but for the sake of this example we're gonna create a new project is gonna be called YouTube project is it I don't really need it to be 3d and I'm a hit Judy so we're gonna do create a new project so since this video is not about creating a game but just storing stuff on a database we're actually gonna create a little simple game that is not actually a game if you you know wanna call it that basically every time you start the game you're gonna get a random score and then the user is gonna be able to write this name and with a pattern is going to be able to submit all of that information so it's score and it's named to firebase everywhere trying to make this work so first of all let's create a text this is where the player score is gonna be displayed so this is the text as you can see unity already created a canvas for us that's great then we're going to create an input field this is where the player is gonna be able to type out its name and then last but not least we're going to create a battle this is gonna be the submit button so now we have our three components that we're gonna need and we're gonna go to the canvas and change these settings to screen space camera and then select our camera and put it right here so now the canvas is at the same size and anchor it to the camera so we don't need to worry about all of that stuff so it's easier you know to keep track of it and also if we play the game it's gonna already be at the right position with the camera at any resolution as you can see well whether it's not gonna work at a decent resolution right now but that's not our concern our concern is actually making the database anyway we're gonna put these buttons right here we're gonna actually try to make it can appear a pretty i I don't know I'm not that good in deciding but here in text we're going to actually put score column and here that is gonna be our score and here in the input field we're gonna go in placeholder and say play your name or your name and then impact on we're gonna actually go to the text and type submit so these are gonna be our three components all right so what we're gonna do now is create a game object an empty game object so right here create empty so this game object is gonna have all of the scripts that make our game work so basically this I'm gonna just do one script because the game should be pretty simple I will call it a player no I mean play your scores I don't know because this is supposed to be just a you know school review you're not supposed to I really have the score and just submit your name but you know for the sake of the example we're just gonna make the score randomly generate so now we have our script put your scores and as you can see is right here so now we're gonna open it I'm gonna be using the JetBrains rider to open scripts you can use whatever you want from notepad plus plus to Visual Studio code you know I just like this one better alright so here we are on to our script so basically the first thing that we're gonna do is we're gonna add a reference to the text so basically this text right here so we can set the score from the code because as we said we have to generate it randomly so public text score text and sorry if you log so it's still compiling it so as you can see it's gonna see that we don't have the you know text library or whatever you're gonna just import it with alt enter oh this so basically it added this line of code right here nothing really important and did you score tanks so then we're gonna add two variables that are gonna basically be deplete your score and to play your name so we're gonna do public integer play your score and public string put your name so as we said the score is gonna be randomly generated so we're going to basically create an instance of not a random to create random scores it's gonna be private random equals new sorry random random equals new random to create a new instance of random and then here we can do that play your score equals a random duck next from zero to one another one so missing what teachers gonna do is gonna generate another random number from zero to one another one so here it was giving us an error because it was conflicting with another random object so you have to put the system dot random right here to make it work it just a conflicting issue so now our score is actually random dot next 0 to 101 so it's a number with in between 0 and 100 so we're gonna make it so our score text that is actually you know the text our stacks right here duck text it's gonna be equal to so it's gonna be evil to our score space and then we can add the score which is player score sweet this is working so far so now we have to add a function that is going to get executed once the player hits the button this is gonna be called public void on back to on submit we can call it so like this boom and right here is gonna basically tweak the input field text and it's gonna set the player name to the text that's right here so the text that the you know user actually typed so we have to create a reference to our input text so public input field and we can call it play your name I don't know who your text or name text it would be a better name and then we are right here and here we already have the play your name variable so basically were ever gonna do is we're gonna do name text dot text so we get T you could feel text and then we make sorry play your name equal to name text talk text on submit so this variable is gonna be executed by the button so we're going to see if this code actually so now we are right here and we're gonna wait until it compiles and we're gonna go to the button and right here where it says unclick list is empty they're gonna add an item we're gonna add this game object right here and now we still like him because he's compiling the script so so we're gonna give him some seconds all right he finished so as you can see we're gonna put the game object right here and here in function we're gonna go on to player scores and we're gonna find our on submit function so once this button gets hit this function is gonna get executed so we're the player name is gonna be actually play your name and the player score is gonna be this so these two variables are set and we are ready to go also meet is that I also submit the stuff from Tory our database so we can create a new function that is going to be called post to database and this is gonna do all of the dirty work to post the stuff on to the database and this function is gonna be called right here so far so good so I'll do we post and get data from the data well there are two ways to do it you can use the firebase SDK or you can use the REST API in this video we are going to be using the REST API because it just works for every single device well the firebase SDK works or at least is supported only for mobile you can manage to make it work for every single version like for every single device but you know the main support is only for mobile and Android so just to make our tutorial in our app universal we're gonna actually use the REST API we can actually delete this function because we're never gonna use it so how to use the REST API so to connect to the internet I actually found a really cool library that makes it do just that really simply it's called the rest client library so basically we're gonna download this one and we're basically at the asset store and we're gonna import it into our project surfing as you can see were right here and it's free so we can just import it and we can do a boom import and now the rest client package is into our project we can easily connect to another service to another website and do a post or a get request and it's done that's sweet so now we can go back here and now it's as simple as this we can just do rest client dot post because we're actually going to try to post something you know on to our website and here we have to put the you know website or the endpoint URL and we have it right here you're gonna have it right here so just copy this URL and then add the dock to JSON at the end now we still have to import this so let's go right here let's do alt enter to import it so as you can see it added this new line right here and now we have the link and then we have to prove actually the object we want to add to our database we want to post to our database so we actually don't have this one yet so basically what we have to do is create a class with all of the information about our user the only information that we need to post is the score of the user and the name of the user so let's do this right way to do that it's extremely simple we just have to create a new script and then I call it user yes we can call it that and as you can see now is gonna appear right here so basically this user is not gonna have a motor behavior and we have to put it onto the serializable tag so cellulite sample right here and this is great and now we can remove all of these functions freaking because we're not gonna need it everything we need right here it's a public string there is gonna be the user name and a public int that it's gonna be you guessed it the user score and now we need to actually you know set these two variables so in the constructor of the class is gonna be a public constructor in the constructor of the class we can actually do that the username is equal to play your scores dot username I think it was cool to play your name right here yeah I'm really bad at naming play your name don't play your name and the user score is gonna be player scores doctor player score and this is giving us an error so maybe we should make these two fields it's tacitly because we're not gonna use multiple orders at the same time so let's just make this topic for the time being trust you can see now this is gonna work so basically every time we initiated this user class we are gonna have an object that is gonna be the user and we're gonna have a new surname it is gonna be equal to this and sorry today's and then user score that is gonna be equal to this so now all we have to do is actually initiate our user so we can do user user equals new user and now you guessed it this user right here we can just post it into our database so user and this you see this is actually the code that makes everything work this is like it's not really long I know it's something really simple the actually database the actual database is just two lines now before actually testing and seeing if this works I have to give a little bit of a disclaimer I mean not a disclaimer our warning for anyone that is using a Linux operating system that is not Ubuntu because there is a bug and this bag actually got me stuck a long time that actually doesn't make web requests work on this operative system but it is a really easy fix to this body you basically just have to put a command in the terminal and I'm gonna show you right now so if you are on Linux and you're accounting this problem just go on terminal just open a terminal and run this command right here boom this one and this is gonna fix your problem what it does is gonna basically create a link to a certain folder so basically unity it's gonna find a web certificate or something because without it unity is not gonna be able to connect to the internet and you're not gonna be able to do that match so it's just this and then you have to put your password naturally so that's it and now let's go back into the video alright before Ronnie we actually have to go onto our game object and as you can see at the script right here we have to you know fields that we actually have to feel to feel yes so basically the score text this year and the name text study input field so let's do here let's take the input field and it's put it here oh my gosh I have a really bad aim today and the text yes this is it so we're gonna put it right here so now if you work we're gonna save our project and we're gonna click on play all right so we have a score of 41 I'm so excited this code is actually pretty good we're gonna say Nico and I gotta submit and let's see if he's gonna actually submit to here and yes it worked wait wait what what did you submit all right so it worked as you can see in our database now we have this folder with a really peculiar name and inside of it we have a user name liquor and user score 41 so this worked and we can actually modify the values from here and we can modify the name from here if I never knew it okay we're not to me okay we're not gonna you know change the scores because that would be bad now you may see me go what is this abomination of stuff so if you don't want this abomination of a string to appear randomly basically what you need to do instead of making a post request you can do a put request and it's basically not gonna make this thing appear so that it's only gonna be user name and score as you can see we're gonna test it right now I already know that it in I did some testing and we're gonna actually see Nikola Pro yes because a pro and now if we do submit and we go right here as you can see there is only gonna be username Nico the pro user score 79 as you can see this actually is deleted all of the other scores and we actually don't want that so perhaps we have to store each player's information you know in a different place because else is gonna just get replaced and replaced so what we need to do is actually here let's add a plus sign to plus size and here wait I have to actually escape from this is turning into a mess okay and here we can actually put yeah let's put the username why not so username where is it how did I call it it's right here put your name sorry player name so basically what this is gonna do is what I put it into you know we could say a sub folder called with the name of the play your name let's see if this actually works it should work so basically what it does instead of putting it you know right here it's gonna actually put it in a subfolder so let's try it out to make a code ourselves Nico and now when we submit and we go right here see that it's gonna be Nico and inside Nico that it's gonna be my name and my score so alright we reached to do a post request to our database well it's actually a pure output to request not a post request but you know it's the same concept now we want to actually retrieve something from our database so what we need to do we are gonna actually reuse this input and we're gonna add a new button yeah it's as simple as that let's actually add a new button and this pattern we're gonna do ctrl D to duplicate it and this pattern right here is gonna actually not be submitted but actually again score let's actually change this and I'm gonna say get score so basically what it is gonna do you're gonna input the name right here and then is gonna input the score of the the player right here if you found it if he doesn't find it then it's gonna just you know tell your arrow so we need a new function and it's gonna be called on get score I guess that's not really a good name but that's okay on that score and instead of cost to database we can do reach from database look at that so now let's actually create the function retreating from database we're gonna still do this okay so it's the exact same thing but instead of poo to me that I actually get some information so we don't need the taste anymore because we don't need to post anything so now he's gonna get some information from the put your name and the put your name is gonna be the one right here so the one written right here so this is good but we're gonna retrieve an object right now we're just retrieving a string or JSON we actually want to retrieve the user object so we're gonna retrieve a user like this now we actually want to retrieve this user so what we're gonna do we actually have to say give me back the user when this code actually gets executed and when I get the response so we're gonna do doctor then and then we're gonna do response and then we're gonna do this funny little arrow and then this so basically what this is gonna do is gonna say once you get the response give it to me and I can do whatever you want with it right here so this code that I put a type right here is that I only execute once we get the response back and that response is gonna be a user object and it's gonna be right here so basically what we're gonna do we're gonna just return the response so this is not gonna be employees function it's gonna be a user is gonna be returning a user and so this one right here where is it retrieved from database this will return a user so from here we can actually get the score I guess user score look at that so we can get the score and we can just make it so the name text yes not not to the attempts to the score text yes it's gonna be equal to days now this is giving us an error because the score is actually an integer well the score text the text is aa string but we could just do docked to string it and it's gonna fit to the fixed arrive at the error because they if the jar is going to be converted to a string so in theory just in theory this code should work and oh then it's an error right here in player scores where is the error I cannot find it I cannot see it so if it actually came to retrieve the information for some weird reason whether I just for now return no I don't know if this is gonna actually give it okay it's gonna let it pass don't do this in your code through some exception right here say to the user something weird is happening I cannot connect to the database don't just return the new because then the code is gonna go into an exception but because right now I'm you know we just want to see if this works I'm gonna put it - no just so this guy doesn't give me any problems but don't do this in your actual code because you can see is even giving me exceptions but right now we're gonna test it out so let's go into our database we have hello and Nico let's go - hello and let's click on get score and is he gonna retrieve my school I don't think he's retrieving my score oh yeah I'm damned I always forget I actually didn't link the button to the code so as you can see we have to go to the button right here and we have to actually make it so it doesn't do on submit but it actually goes to only get score so now we can see it again and play it and hopefully this is the right time hopefully hopefully this time actually it actually works so we're gonna go we're gonna see school alone hello and we're gonna get the score and so the reason why this school actually doesn't work is for a really simple reason as you can see to get from my computer to the database it actually takes a while it's just fractions of seconds but you know it's still time so basically what this function does it ignores this bit and just returns no so this thing is no the user score is no and it gives me an exception and that's unacceptable and we cannot have that happen we can actually work around this issue by making this avoid so not return anything and then making the response equal to our new sir so basically we can do a new user right here maybe we can just create a user right here right on top so right here and so this is gonna be our main user so the response sorry the user is gonna be equal to the response they're gonna get and only when this is set then there is gonna be a new function that is gonna actually update the score so we're gonna do a private void update score and as you can see right here so basically what this is gonna do is gonna do score text sorry play yeah players or our score text yes duck text equals the user that we set to the response remember the user dot the score so the user score so now not really to string because it's still an int and here this function is gonna get cold from here so after it's cool so now in theory this should work around the issue because the return the the function actually doesn't return anything and this function only gets executed once this one is done so basically it does the exact same thing but just in a different way so you know that we don't get into the issue and this function actually gets executed at the right time now I have no idea if this actually is gonna fix it or not and I guess he fixed it but we still have a little bit of a bug this is actually a silly one the school text it's not gonna be we don't have to set it to this but we actually have to set it to something else we have to set it to score plus this or you know it's just going to retrieve the score without anything else but other than that the application is done and everything works and now if we retrieve the score of Niko and we do get score okay if it always 35 now it's 65 oh I see what the issue is you actually put it down okay first of all we don't need this anymore than to string second of all this doesn't actually have to be player name but he actually has to be yes name text because he was taking the player name from the user but you know he actually has to take it from the input field and it is working for a second I see some some red right here that there is nothing else to happen so yeah now it should actually work yes I know I said it like two thousand times but you know that's how he goes with coding all right so this is the extreme the last test we are gonna do okay what name should we do nico underscore underscore okay with a score of 42 okay and we have it it's in our database nico with a score of 42 and then we are going to retrieve the score of nico and we're going to get the score and it's gonna be a score of 65 it works this is how to create a database to store your data onto a database within a get and a post request all of the code is gonna be linked in the description now naturally this URL is not going to work because I'm gonna make it private by the time of this video you know gets published so if you want to try to do something you know for yourself just just change this URL to the one that's right here in your project and that's it I hope this tutorial actually made you learn something and you know it was entertaining to watch I tried my best this is my first tutorial I ever done on the unity and you know if you wanna see more of this kind of content leave a like and leave a comment and tell me if you know you like this or any suggestions about this but anyways thank you ever so much for watching this video it was pretty exciting and amazing to make it and yeah I'm gonna see you guys next time see ya
Info
Channel: uNicoDev
Views: 77,956
Rating: undefined out of 5
Keywords: game, gamemaking, tutorial, how to, firebase, unity, unity3d, unity2d, database, firebase database, serialize, serialization, locked, locked game, sokoban, puzzle, coding, developers
Id: Fz0Sl4tW5O0
Channel Id: undefined
Length: 27min 36sec (1656 seconds)
Published: Mon Dec 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.