How To Create A Database In Unity | Adding A Database In Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys fire here back at it again with the sexy glasses anyways a lot of you wanted to learn how to implement a database with unity so uh here it is let's check it out right away so this is firebase implemented in unity and if I go here quickly the first thing I'm going to show you is how can we authenticate with email and password so as you can see here we don't have any users if I go back now in unity and if I hit the play button we have email and password and I'm gonna put my email here which is of course my real email and here I'm gonna put the safest passer in the world that you're not gonna crack and I'm gonna click here register and this is going to register the user here in the console I'm also going to print out here you see registration complete and if I go back here in the chrome and I reset or reload the page you will see here we have a user in our database and if I go back let me turn it offers and clear the console and let me try to go here if I try to login so if I try to login you see here email and password and missing or actually we cannot login but if I type here for example my email that I just used your register which is my real email by the way and if I let's say type the incorrect password we saw the password is one two three four five six and pay attention here in the concept I click here login it's a wrong password it's missing you see and if I see here one two three four five and six and if I log in we will see that the user is logged in and we will see how we can detect this how we can do all of these cool things so on and so forth don't worry about that this is just a demonstration what we're gonna learn and I'm also gonna go here in scenes and I have a save and load scene which is a database if I go here this is my database we don't have anything in it and we're gonna see how we can store things in the database so let's go here in unity and if I hit the play button again I have a username and password but you will see in the tutorial I'm talking about you can use this to store your jambs play your score progress whatever and if I see here I don't know Kevin Smith for example and if I click here safe and if I go into database you see wallah he is saved you see we have users we have Kevin Smith and if we try to retrieve him if I click here on load we will see here in the console being printed the player is Kevin the players password is Smith let me clear this and if I try to add another one let's say I don't know Carl Z's ok like this and if I click here save if we go into database we see we have another user right away it's Optima it's updated automatically it's real time database so yeah we have them both let's try to retrieve them so load the data if I click here load you see now we have player is Kevin player is call Karl you see my point so yeah we're gonna cover how we can implement database in unity is gonna be really cool really fun a lot of useful things that I'm gonna show you that even in the documentation they don't have so if you want to learn how to implement all this stick with the video smash alike and let's start with the tutorial what's cracking people I mean a new project right here which I suggest that you download link will be in the description below because I have everything prepared I have two scenes the first one is about authentication the second one is about save and load so here is where we're gonna enter our email and password and register a login or logout or anonymous you get the point but the main thing here is that I have this set up because I'm not gonna show how to drag and drop all of these elements we already know that and the first thing in order to set this what we need to do is go here and you will need to download Android studio I mean you don't need Android studio what you need is Android SDK which is this one right here in preferences this one right here but the easiest way to download Android SDK is by downloading android studio so go type on google android or download android studio or whatever simply go on developer.android.com dot or slash studio so you can pause the video copy the link and paste it download the android studio click here download the android studio it's like downloading any other project or any other program that you install on your computer so when you have that done you're gonna click here and open Android studio and what that happens Android studio will run like this but if you don't have Android SDK it will prompt you instead of seeing this welcome right here and starting a new project and whatnot you will see a prompt for you to download Android SDK it's simple as clicking the next buttons you're simply gonna click Next yes install yes download select the location all of the good stuff so basically it's nothing you did not do a million times on your computer before so when you have that done you will need to know the path so when you when you're installing Android SDK make sure that you know that pad to that folder on your computer because you will need to go here in unity and I think this will work on Windows as well because on my Mac when I go here and click on unity in them preferences I believe for Windows its edit and then preferences so when you go there it will be here and generally you're gonna click on external tools and you're gonna go right here for Android SDK when you click on this browse you will see that the Android or unity if I remove this from here and I click browse you see Android SDK detected unity is pretty good at doing its job so it will detect Android SDK and then you're simply gonna say here do you want to use this SDK directory yes I want to use it and it will locate it for you but in case it does not do that you will need to know the location of the folder where you installed your Android SDK and this is how it goes this is how you do it so that is the first thing that you need to do to set this up the second thing to do is go here on firebase so type firebase that google.com so firebase Google comm and set up your account if you don't have it you're gonna register it like you register I don't know for Facebook for YouTube for Gmail for whatever you're gonna add your email and your password and whatnot all of the good stuff so yeah when that is done so when you log in you have your pass where you register your account you're gonna go here click to go council so go to council when you click on that or simply type consul that firebase that google.com and wala there you have it so the next thing to do now that we have everything in place and up to this point I hope that you follow me something is not clear asking the comment below or on the Facebook group so welcome to firebase we're gonna click here add project so when you click here this is gonna create a new project and this is the name of your project and I'm gonna call it firebase and unity tutorial or firebase and unity tutorial I cannot add here a slash I tried to do that so firebase in unity tutorial you're gonna click here is the default setting for Shane Google and it's very base you're gonna click on this I accept and the control terms of blah blah and then you're gonna click on create project this is going to set everything up that you need and it is going to create that project for you so let us just wait because well it takes a bit of time and while I your project is ready you're gonna click on continue and it is going to take you where your project is well uh this is what we need to do the first thing that we are gonna set up is authentication so that we can log in into our app or in our deem by providing email and password in order to do that you're gonna click here on the left side you see this list of things that you can have you see develop so you have authentication you're gonna click on that and it is going to bring you right here now here you see user step you're gonna click on the Sign In button so click here sign in method and here for the email password right here at the very end of it you have this pencil you're gonna click on that pencil and you're gonna click here enable okay so you're gonna click enable and allow users to signing using their email and password blah blah blah and you're gonna click save and this is enabled which means we can log in our attendee cage with email and password you can do that with phone Google Play games Facebook Twitter but we're not going to cover that we're covering email and password and the set up for all of these is basically the same you just changed a couple of things we're also going to go here for anonymous and go here on the pencil so click on it and we're gonna enable that as well and we're gonna click say which will allow users to log in anonymously in our app or in our game so make sure that those two things are done and when you do that we can go back here to project overview and you see this right here unity getting started you see add an app to get started we're gonna click here on unity and let me just go back again so that you see what we are doing you see here we have for iOS we have for Android I don't know what this is web app I don't know anyways we have here for unity again that is when you go into your project and project overview which is the first page of your projects you're gonna click here on unity and what you're gonna do you see registering app are you releasing your game for iOS and bubble by if you're releasing for iOS and Android you're gonna check both of these checkboxes but I'm only gonna do it for Android but basically everything we do for Android is the same for iOS so we're gonna check this a register as Android and pay attention here Android package name you see if I hover over this question mark it says you can find it under build settings Android player settings and other settings and bundle identifier basically we need to go back here in unity and I need to go back under file and then build settings you see here and then when I have this open right here I'm gonna click on player settings and here they are so I have player settings here and when you click on them pay attention in the inspector which is on my right side you're gonna scroll here and add other settings so you have here icon resolution splash image other settings and X are settings we're gonna click on other settings and we're gonna scroll down when we see this bundle identifier so just go here you see the bundle identifier which is highlight tool here it is bundle identifier voila now for the bundle identifier you see this comm company name product name you're going see here calm not your name and last name of your company name I'm gonna say here calm that awesome toots dot database unity okay so this needs to be out tending this needs to be unique you cannot have two apps right here on your firebase with the same credentials here so this needs to be different so here we have come awesome toots firebase actually I'm gonna stay here firebase unity toot so you're gonna type whatever come your name or your company name you get the point when you have that leave it like that so you're gonna leave it there and if I click beers play your settings it's still there you see then we're gonna come back here and we're gonna paste it so you see Android package name you're gonna pace exactly what you wrote there copy and paste it here and now we're gonna click on register app which is going to register it and now we're gonna have a download file which is right here see download the config file you're gonna click on and download Google services so we're gonna click here and it is going to download it and here it is in the download folder what we need to do is we need to drag and drop it here in our unity project so let's go back here and it gives you instructions right you see open the project window unity project then move your downloaded firebase config file into the asset it's really important into the assets so put it in the assets this is really important and you click on this blue button to download it so now that we have it I'm gonna go back here and I'm gonna drag and drop it right here in the assets and here it is so everything what we need is right here so the next thing to do is click on next and now we need to download the firebase SDK so click on this little button here it will start downloading the firebase SDK which we will need you see here unzip the downloaded SDKs somewhere convenient wherever you want then open unity project navigate to import package custom package and from the unzip SDK importer well it says here Analytics SDK but we don't want that we want authentication so far based auth and we want the firebase data bass nigel take a moment or two for this $2 I'm gonna stop the video here and continue when it's finished finally my internet connection had mercy for me and it downloaded this firebase zip folder so what we need to do is simply unzip it so click on it and it will unzip it let me just remove this from my browser so now that it is unzipped you have two folders you see here we have dotnet 3 and we have dotnet 4 now for our firebase authentication so firebase odd we are gonna import this one from 4 we will see in a moment so let me just go back here I'm gonna click on next and basically we're gonna click continue to the console so everything here is ready now waiting for data analysis blah blah blah anyways I'm gonna go here in our authentication and the next steps are to go at firebase to your project which is what you're gonna do now and this is getting started with firebase authentication now as I said if I go back here my Finder so we have a dotnet 3 and a.net 4 folder so for the alt this one right here we're gonna use one from the dotnet 4 so let's go back here in unity and this is the first thing that we're gonna do first you're gonna cover authentication then we are going to cover database and saving to the business and retrieving data from the database so right click here in the assets and go here under import package and custom package after that you're gonna go under dotnet 4 you see here you're gonna find the firebase SDK you're gonna go under dotnet 4 and you're gonna search for firebase auth unity package this is really important you cannot mix this package because we have one for the storage we have remote messaging blah blah blah blah blah blah you need this one firebase a uth that unity package this is the one that you need pause the video if you don't see clearly and you're gonna click on open and it is going to open all of this right here that you see you're gonna click import and wait for it to import people not take that long because these are only scripts nothing complicated in our project but apparently unity thinks differently you smash that like while you're waiting managed to subscribe and smash the notification button anyway let us wait for it to upload why does it take so long for some reason yeah but anyways it is finally finished yeah here it is and we have everything set up so I am in the authenticate that unity scene and here I have everything set up so we have here email we have password login these are buttons anonymous registered logout I can click on this odd controller it has a script you see here if I go it has a script I already prepared things so we have here text email input and password input and I have attached them so you see they're attached for the input that we want to get and basically that is the set up that's why I told you that you should download this project if you don't want it then you're gonna create this on your own so moving forward the first thing that we are gonna do here above is we're gonna type using firebase dot dot know for some reason it's not registering that I have it let me try to quit like this I'm gonna quit Visual Studio and then I'm gonna open it again hopefully it will now register I think I need to look things up how can I so how how can I reset it right here so that actually this works yeah I believe that somewhere around you built all rebuild all I believe anyways probably some somebody from some guy watching you see it is gonna correct me Oh technically you're gonna do this you're gonna click there in the project and then you're gonna do that I don't care I'm gonna look at that by myself okay so what we are gonna do well first of all we're gonna have a public void a log in button because this is for the login we are also going to have a public void the login anonymous so log-in underscore on Nemus like this and we're gonna have a public void log out simple as that so log in and log out and actually so we're gonna have a register so avoid the register user like this before we continue to program everything we're simply gonna go back here in unity and we're gonna select all of these buttons so select the login anonymous register and log out and here for the on click you are gonna click on this plus button and we've done a lot of these things before so you should know how to set up your button for clicks you should know how you can make your button execute code when it's clicked so select the login anonymous and register and click on the plus button here for on click and drag and drop the odd controller now for the login I'm gonna go here and for the function I'm going to select the log in function for the anonymous I'm gonna select the log in anonymous function for the register I'm gonna go here and I'm gonna select the register where it is where is it log in log out where is the register yeah and it needs to be public so public that is my mistake so my bat so here for the register you're gonna click on this and so click here and we're gonna go on there off controller and register user and finally for the logout I'm gonna go here and I'm going to select odd user and log out simple as that so we can go here and for the log in what we are gonna do well for the login we're gonna call firebase auth as you can see that default instance that sign in with email and password async and here we are gonna pass our email inputs of email input text comma here we're gonna say password input dot text and we're gonna see that continue with and like this here I'm gonna say task so actually it's like this and here I'm gonna say tasks open close curly bracket and we are good to go so what this do to me so it's actually why did I be here and I think I need to close it one more so what is another way oh yeah I also need to add like this so now we are good to go so firebase odd default instance sign in with email and password async email input text password input text dot continue with your gonna pace here or open close parenthesis task equals a greater than sign and open close curly bracket now this you can find right here getting started with firebase authentication in unity before you begin we need to you know registered into everything what we did here so this we already did it we registered everything and now you see sign gaffney users this is what you do create new user this is how to sign up or create a new user so email and password lasting email pass or continue with test blah blah blah signing is the existing users this is what we're doing so here are signing existing users now why am i saying a web page so here this task will tell us so here we can say if task that is canceled that means our task was cancelled now we also have is task and we have here is faulted now his fault egg will be called most of the time and here if or actually simply gonna paste it and he is completed now is faulted will tell us if we have any errors if we have any mistakes so log and if any problem that occurs or any problem that happen occurred so we will know that in task is faulted how can we know that well for that we are gonna create here below our log out so we're gonna say void get error message so that we know and output that error message and here for the parameter it's gonna be off error which I'm gonna call error code like and simply what I'm gonna do here is I'm gonna say string msg so string message by default is going to be equal to like this and simply here I'm gonna say msg is gonna be equal to u error code dot to string and below we're gonna say print to output the message so here I'm gonna say print and I'm simply gonna say message now of course here in between we're gonna have a switch in case statement now we will do this in a second so what we're gonna do here above inside of the log in so if the task is faulted and we're gonna do that in a second so we're going to see what is going on with that and how are we going to well tackle and so basically we're gonna say here firebase that firebase exception e is gonna be equal to task that exception dot flatten and here I'm gonna say that inner exceptions and the exception that's an element 0 I'm gonna do like this this year an exception that's an index you know as firebase not fire base exception voila so we're gonna get the exception and simply here we're gonna say get error message we're gonna output that error message and we're gonna see here out there and we're gonna pass here a that error code simple as that and below we're gonna say return and also here for that is cancel we're gonna say return now just to test it out we will come to things one by one but just to test it out do you see to tell you what is going on here so if I go here and hit the play button if I type something like this so let me just clear the console if I say something like this here let's say my name my name at calm and password is one two three four five six and if I click here log in you see user not top you see BAM and it is printed here user not found no maybe I should have done this with the register because we first of course need to register but anyways this is for the log in for our user now let me go here for that registering you we will see everything first of all let me just register the users that we can see that we can actually register the user and for that basically what we need to do is we need to say firebase auth dot default instance that create user with email and password and the email is going to be email input that text and I'm gonna see comment below it's gonna be password input dot txt and here I'm gonna say continue with and here we're gonna have a task like this and here close or color or semicolon excuse me so again here we're gonna test the same thing so we're gonna say if our task dot is faulted basically his castle will happen rarely but we can use it as well so here we can say if task is canceled again here you're gonna do the same thing so we're gonna exactly do all of this right here a fee of the task is canceled or if it's faulted and actually I also need to get the returned yeah I got it so task cancelled we're gonna paste these two so that we know the error message that happened is it faulted if something went wrong now is faulted as you saw in the login this is what I wanted to show you actually originally when I did it if you don't have a user in your database if the password is wrong if the email is not correct if the password is missing and blah blah blah you're gonna get all of that here in is faulted and here you're gonna have the same thing so what I'm gonna do here if task that is finished actually it's completed this is when we know that we actually successfully completed the task now here for is completed I'm something gonna say print and gonna say registration registration complete this is and it's print not pretty it's print this is for the registering the user now of course pay attention to one thing here I'm using email input dot X and password input dot txt what we can do here is we can test if our email input that compare or actually equals yeah and actually it's email text that if it's if it equals to an empty string and if the password input dot X dot equals an empty string so if these are true then we're simply gonna see here print please enter an email and password to so enter an email and password to register and here we're gonna say return now there are multiple things that can happen here basically what we're checking here is if we don't add anything inside of the input field so basically now if I go here and click enter or actually click the runge button if I click register you see please enter an email and password to register because we did not enter anything in the email in the password field we're checking down these this logic is up to you I'm not gonna do and create everything please don't ask me the video help me to implement this I have a game I'm showing you how to implement how you're gonna implement that's on your own and depends on the game that you that you're working on so don't expect me to create to code things for you so yeah here you're gonna test if email input dot x equals to an empty string and password input that x equals to an empty empty string now what can also happen is that you enter an email but you don't enter a password and it can also happen that if you enter if you enter a password you don't enter an email so basically we're going to check for those things now return will exit outside of the function I've used this multiple times in a void function when you use return it will simply exit out of the function which means it will not execute the code below it will exit completely outside of this register user function and it will ignore all the code below it will not execute it basically that is what we are doing so here let's try to register first a user and Gary's gonna see here let me go to my firebase you see if i refresh this we don't have anything you see we don't have anything here users not in the project we did not sign any one here so what I'm gonna do is I'm gonna go here and I'm gonna say my name my name calm and I'm gonna say faster one two three four five and six and let me clear the console I'm gonna click here register you see it should be completed she's registration complete which is this code right here this is what I was talking a moment ago if task is completed meaning we did not get any fault we did not get any candy the task was not canceled now fault means it can happen that your password this week and so on and so forth we will go now to dad so get error message that is all handled in the error message if somebody is wondering okay how can I tell my user if his password is not good and bla bla bla bla bla so now you saw that we have here registration complete I can turn this off and if I go here you will see now when i refresh this you will see that I have a user you see here it is voila I'm here and I have the user ID and bla bla bla and what not so voila Here I am so we see that this actually is working now let's try so let me go here in a sign of the log in let's try to log in and I'm gonna log in with the wrong password so I'm gonna go here I'm going to hit enter or actually why am i saying enter and here I'm gonna say my name com so let's say my password is one two in through my pass on your sides one two three four five and six and I'm gonna say seven here I'm gonna login you will see here wrong password you see wrong password it did not work and let me try to delete this let's say again one two three eight click here again wrong password and if I say one two three four five and six and if I log in we logged in but I did not print out anything so let me go here in the side of the log in so if task is completed I'm simply gonna say here print and I'm going to say here user user is logged in so that we know that we are logged in basically if an error happens here if is cancelled I'm also going to turn it also here what I'm gonna do is call it like this and not to returns only one so basically if the task gets canceled if something interrupts our tax problem trying to login we will do it we'll get an error here you as you can see we're getting it as a firebase exception and exceptions occur when something interrupts our game or actually interrupts these how to say these I forgot anyways you get the point so when something interrupts this task task thank God so firebase exception e we're gonna get it from the task and exception and flatten inner exceptions because inner exception is an array we're gonna get the first exception we're gonna get it as a firebase exception so that we can pass it in the error message like this so that here we can do something about it now what you can do is you can create a small panel where you're gonna tell your user because you will see wrong password and stuff like that if you want to do some specific things in regards to this and now we can I can show you that you can go here you can have a switch in case you can see here switch error code like this so switch error code and here I can have multiple cases so one of the keys can be out there that account exists with different credentials meaning we are typing an email with different credential credentials or maybe username and name stuff like that so here we can also see out there that missing password so missing password that can also happen break we can also hate say hey case out there I don't know wrong passwords and here I can say break we also have keys out there that invalid email and I can say here break if you want to see more you can go for all errors you simply type here for the firebase open image here you can see all their firebase and you can see out their documentation I think firebase there is it this let me just go here and see if this actually is that out there documentation and I'm teaching you to you also go and firebase out there requires where is it so let me just firebase odd error message this is the same thing I will search for the link I forgot where the link is just search for it basically out there and you will see all of the codes that you have or all of the possible things that can happen like missing password wrong pass or an invalid email and so on and so forth but basically using it like this you see print message and here of course you're gonna type your code so here you're gonna code and you get the point between the break and whatever you're gonna code your appropriate code then I'm gonna put this in a comment you're gonna put your appropriate to what you want to do but basically here if you see this error to string it will output output the string which we can say here actually we cannot say login because we're using this everywhere we're using it for the registering the user and so on and so forth so basically if I go back here this is my point so let me go in the console and clear and if I try to do this so let's say here my name password 1 2 3 4 5 log and you will see here it will say wrong password now you can display that to your user you can say log in err problem was wrong password and you can add things with switch in case you can say something like please enter the correct password to log into your app so something like that so voila this is what you can do with ok so this is how we register user so this is how we register a user like this and of course you're gonna handle your errors on your own depending on your game this is how we log in so this is how we signed in the user and again here you're gonna you are going to handle the errors on your own depending on your game now for logging anonymously if you want to allow your users to log in anonymously what you can do is simply here we're gonna call our firebase auth dot default instance dot sign in so default instance like it's signed in anonymously async like this dot and here we're gonna say continue with and again here we're gonna paste our tasks like this open close curly bracket and that same exact thing we are gonna do here so basically we can copy all of this if the task is canceled if the task is faulted or if the task is completed so depending on what is going on you are gonna do the appropriate thing and for logging out basically what we are gonna do here so for logging out we're simply gonna call here come on so for log out simply we're gonna call firebase auth dot default instance that sign out and simple as that now you can also test if we have a user so you can do with like this you can say firebase firebase ought default instance that current user if it's not equal to null meaning we have a user we have registered then you're gonna call the sign out simple as that now if I go here in unity and if I clear everything if I sign in anonymously if I click here sign in anonymous so I'm gonna click here we will see user logged in now let me see if that is actually displayed here I'm not sure so let me go here and test it out so just refresh it oh yeah you see anonymous so we allow people to log in anonymously in our app if you're click if you click log out it is gonna do it log out let me just see here if it actually removes him from here I'm not sure that no it does not but anyways when you click on it it will log him out so this is how you can register and how you can retrieve a user for your game so for example for what you can use this well if you have a game where you allow people to play online you can use it for this and make them register for the game get their emails and stuff for your marketing or for whatever purpose you want to do it so you can do it then in order to see how what you can do with all of this I'm going to leave all of these links and basically these links are available here but if I don't put them in the description of the video please remind me so that I will put them and yeah this is everything what you can do so see you next steps if you want to see how you can sign in with Google with Facebook or Twitter anonymous phone intent occation you can do that on your own so this is for authentication with firebase in unity let's move forward and let's go here in our scenes and we have now the save and load okay we have here save and load now one thing that I found for this is they're probably going to fix this in the future but we need to also import the package so we're gonna go here an import package I'm gonna go and custom package you see go in custom package but you're not gonna go in dotnet for we're not gonna do that we're gonna go here in our firebase Unity SDK we're gonna go under dotnet 3 and we're gonna search for firebase database so again it's dotnet 3 again this is a problem an issue I had when I tried to register or when I try to when I was building this project it was not working with dotnet for because what we need to do now you see you will see in a moment so go again firebase unity dotnet 3 and then go here firebase database we're gonna click on it and it's gonna import and while it is doing that you're gonna smash that like button and I'm gonna go here and simply click import and it will import everything so importing and blah blah blah smash the subscribe so we are waiting for this to you and I'm gonna test so you on my Facebook notification is ignore that hopefully is not somebody some you guys asking how to do this anyways let me just go here in project so build come on so build let's say build all let me see if this is going to allow me to go now in our data bridge right here and just a moment so I'm going to see using firebase this is working so let me just go here using firebase database dot database database no it's not I need to find how this works but let me just go here quit and now I'm gonna go here under data bridge and I will click it and open it again so now we are gonna go in our data bridge class which is in the save and load C which you saw me do so go here in scenes and go and save and load after you import the firebase database as we just did a moment ago so here let me try using firebase database now now it is working and while a lot finally this is now pretty cool now one other thing that we need to do before we can continue that's why I said we are using here dotnet 3 so dotnet 3 not dotnet 4 I'm sure they in the future they will fix this with dotnet 4 because we need to go here in file and build settings and player settings and I believe it was here yeah it's here so build settings and play your settings I believe you can go here edit and player settings where was it I forgot I forgot anyways you have this separately you don't have to go under file and build settings but you have it here anyways in the inspector when you go here and scroll on the configuration and let me just square the pencil you see here under configuration you will have this right here C script runtime version you have dot net for equivalent and you have got that 3.5 which is deprecated that is the reason why because this firebase database that we have just imported the unity package will not work will dotnet for we need to use that net 3.5 so I'm gonna click here and it's gonna change it I'm gonna restart so yeah do everything you just saw me do so restarted because it was not working at the time when I was recording this it was not working with dotnet 4 maybe they changed it but basically the code and the implementation are gonna show you it's gonna be the same but as I said it was not working with that net for for some reason I'm not sure why I'm not gonna find out apparently but yeah let us just wait for this to progress and blah blah blah and do it stuff so this is awkward smash that like button whether we come on unity I don't want to quit the video now that I have to edit all of this you can skip a little bit the video actually I'm gonna quit it No so what you see here the firebase database requires the null blah blah blah you're simply gonna click yes here and is gonna do that for you so now we are good to go so yeah we're gonna go here everything here is okay don't worry we're simply gonna clear the console and we are done so now we have our firebase database also pay attention here I have this player class which is really important if you want to save a class with like we are gonna do in your database you need to do the following your class needs to be serializable or serializable however this is pronounced so I have a public class play here and it's serializable we need to use type using system for that and it has a public string username and password we have an empty constructor and we have a constructor with arguments if you don't know what a constructor is then you need to go through my c-sharp tutorial series to find out so anyways this will help us create an object from this class which we are gonna use here in our data bridge too to actually save data now before we do that we need a private string and this is going to be our data URL which is going to be equal to the following now in order to check that we need to go here in our Google Chrome and we need to go in our database you see here firebase in unity and I'm gonna go here under database so this is how you set up the database of it will not work so when you go into database we need to create it first so click here under these tabs on the left side you have authentication you have database below it we just did authentication so go here for database again yeah I have issues talking so you're gonna click here create database and I'm gonna create it in the test mode because we're testing and I'm gonna click here enable and the wallah it is going to set up security rules and all of the good stuff nananananana and we are done now let me go here under cloud and real-time database you see here database you have this cloud file store you're gonna go under real-time database and before we do anything this is our database you see here for in unity tutorial under rules so click on this rules tab you will see this right here rules read and write both are set to false we need to both set set them both to true and this one also true and we're gonna publish this so follow these steps you're gonna go under rules in the real-time database rules and set both of these to true and then you're gonna click publish and now we're gonna go back to Dana and this is the link that we need to copy this is the link to our fight to our database so click on the link so click on it and copy it so click here I believe actually I'm working anyways right click and copy this is the link to your database so right click copy let's go back here and visual studio and paste it right here this is what we need to connect to the database or otherwise it is not going to work so it is not going to work and now we can start connecting to our database and we can start doing weird things so the first thing that I'm gonna do here on top I'm also going to type here using firebase that unity dot editor and inside of the start function so here start it's the built in what we're gonna do is we're gonna say firebase app dot default instance doc set editor database URL so editor database URL they're simply gonna face here our data URL simple as that we also need to create a database reference so that that's the reference to the database which is gonna be a private database reference you see and I'm going to call it database reference reference thank you and below this line of code here what I'm going to do is I'm going to CD to B's reference is going be equal to firebase database dot default instance that route reference which is going to reference the data base and let me see what we have here so let me just hover over non local man firebase interest cannot be used like a method that actually it's not a method excuse me so it's like this so database reference firebase database dot default instance that route reference Walla so we're gonna have a save and load Dana so here we're gonna say public void save data like this and we're also going to have a public void load data like this in our safe data what we're simply gonna do is we are going to test so if we don't have if user name input so if user name input actually let me go here with the exclamation mark so if user name him put dot text dot equals and we're gonna say here an empty string and if our password input dot X dot equals and again here we're gonna say an empty string so if both of them are an empty string then we are going to say return same as what we did with our of controllers so if the user in your game does not have any data to save now I'm using this as an example we're gonna put things here in the username and password field and we're gonna save them but you can use this to save your game data so for that you will not need to test this right here so below and you know what return does is it will exit out of the function but if we pass this so if this is not true and we pass it then we're gonna say our data which is our player as you can see here so we have the player the one that I just talked about a moment it goes so I have him here as a reference so we're gonna say the data is going to be equal to new player data and here I'm simply gonna say username dot text and here I'm gonna say password so password input dot text to create an instance from our player now we're gonna save this as Jason so what we're gonna do is we're gonna see here shrink JSON data is gonna be equal to JSON utility this is from Unity engine and we're gonna see thoughts to Jason so Jason you Timothy dots to Jason and we're gonna pass here data which is going to convert this class this data class to JSON data as a string which means we can now say database reference that child now this is the child of the database which is going to be our users in this case dot and here I'm gonna say actually users and actually yeah users and I'm gonna say here plus a random dot range between 0 and this huge number and I will see or I will tell you in a moment what that is and we're gonna say here dot actually we need to close here and we're gonna say dot and I'm gonna say set raw JSON value async and I'm simply going to pace here the JSON data simple as that and we can also here say something like actually no here we're gonna return and here we can say print no data so we can say no data now what is this right here see child users and I'm adding this random string this will create a unique ID for the child I've searched a little bit I I'm assured that you can do something like firebase let's say database default instance that actually know here but the firebase let me see here hot actually did not create so firebase that default instance no you don't have it let me just test it like this I I'm doing this on top of my head I'm not sure what I'm doing I'm trying to figure out if we can use firebase out here to create a unique ID so firebase at the default instance that current user that user ID then we can do it like this you can try that on your own you're gonna paste it right here now the reason for that is because we need to have unique IDs to store in our database that is the reason why we're doing it because if I go back if I do this you see all the users and let me let me first copy this and faced it here and I'm going to do it like this and I'm simply going to remove this here so we only now have users and we're gonna say set or raw JSON value and it's gonna set that data so let's see what happens I'm gonna go here and first of all we need to set on the save and load so go here save and register and it's actually a register not load load yeah load like this so the button name needs to be different save and load and go here and on the plus button click on it drag and drop data bridge for the save data we're simply gonna go here data bridge and save data for the load data we're gonna go here data bridge and load data so if I go now and hit the play button and let's say I add my username like this and I don't know let's say password is 1 2 3 4 and if I click Save we are going to see here see users password and we have you see it's working we're communicating with the database and we are storing our thing in the database but what happens if I do this what happens if I want to store another guy I don't know John with his password of let's say or actually I'm gonna say Smith here and if I go and click Save what's gonna happen then well it's gonna happen that we are gonna overwrite the previous one you see now we only have user's password Smith and John so we're gonna override the previous one and yeah you see what happens that's why we need to use these unique IDs so I'm gonna go here and do it like this because this random rage will Kuni create unique IDs because I'm using this pretty huge number what is the chance that the same number occurs for to users but again this is not the safe way to go I'm not telling you this is the best way it's just that I did not find how to create a unique ID with firebase which I'm pretty sure you can do it just type online and search for yourself if I find it we can cover that in the Facebook group but basically that's one line of code like we are doing here so now let me go first here and delete the database I'm gonna go here and it will delete all the users week we have so now we don't have anything if i refresh it we will see that we don't have anything in our database you see it's null it's empty so now that we did it like this you see I've saved it I can go back here in unity and I can go and see another user so I'm gonna hit the play button and let's go here for the users okay let's say now John and I can have here Smith and I'm gonna save it now of course actually I put I think I think I put a very large number here so maybe a couple of zeros so this is 1 million yeah 1 million so remove a couple of these zeros so yeah it's not working so let's go back and hit the play button again you will there is probably a way to create a unique ID for sure and let's go now and say John Smith and I'm gonna click Save and now it is working you see users and you have this we have not John Smith but let's go here again and let's now create I don't know let's see Kevin Kevin I don't know doh so Kevin Doe I'm gonna click Save and you see another user now we have Kevin Doe so you see now the previous one is not being overridden ok so the previous one has not been overridden ok let's now load our data so we're gonna go here and in the load data we're gonna retrieve everything what we saved now in order to do that we're simply gonna see here firebase database dot default instance that get a reference from URL and we're gonna face here our data URL which is going to get the database at this URL again this is the URL of our database which we got from right here this is our database URL to access this database and store our data in same way we're gonna retrieve it now so we're gonna say get reference from URL and this is the URL of our database and we are gonna see here dog get value async here I'm gonna say God continue with and it is going to continue with the task so here we're gonna say tasks like this and open closed curly brackets and we are done okay this is where our magic is going to happen of course we're gonna have here if so task that is faulted then something went wrong and why isn't this going here so leave it there and we're also going to have a task cancel so task he is cancelled and let's go here we're also going to have task is completed so you know what to do in if is faulted and is canceled you're gonna handle on air and you're gonna get it from the task now your assignment is to code these do what happens if you got some air you're gonna do that basically things are identical is here you're gonna research a little bit what errors you're gonna get so that is your assignment if you get any troubles then you're gonna going to output them here this is mainly to tell your users what is wrong and - well fix it so if task is completed meaning we got the data so what we are gonna do next well when we get the data we're gonna save it in something called a data snapshot so a data snapshot is an object that is going to store all the data and I'm going to call it well not data but I'm gonna call it snapshot so snapshot is gonna be equal to tasks that result because you see here when we connected database from this URL and we get the value that value is gonna be stored in the task if the task was canceled or faulted we have some issues than you're going to handle them there again that is your task but if it's completed then we've got the data and that data is stored as a result in the task okay so how can we get the data well here I'm simply gonna say string data so actually I'm gonna say P data for player data I'm gonna say player data is gonna be equal to you I'm gonna say snapshot dot get raw JSON so get get raw JSON value Y get Jason mother because here we saved it so here set a raw Jason value we save it as a raw Jason Valley so we're gonna get it as a raw Jason value and basically we can print it out so we can see here some like print and we can see data is like this and I can say that string you see and act not string but player data so player data and if I go back here in unity and let me clear the console hit the play button so when I hit the load button when the task is completed so click here the load you see we have users with passwords you see here but you see how they are stored they are stored in a weird way we need to extract them so we need to extract these users now if you have only one user you would do something like this you do player C we do player M for example he is equal to you and we can say JSON utility dot and we can say from jason like this and we can say player and we can pass here the player data and this will convert that data into a single player if you have a single player or in this case we're saving the player in your case you're probably saving your game data or whatever you're doing so yeah you are going to put that class and that class is going to be put right here you get the point but we cannot do it like this because we don't have one data we saved two players you see we have two players and what we need to do here is we need to say for each then we need to say for each bar child in snapshot dot children like this so for every child in the snapshot children because snapshot contains the data and the children are the data so what we're gonna see here we're gonna say string like this and now I'm going to say T for example is equal to child that get raw JSON value and now I can see something like print here and I can say data is like this play plus T so if I go back now in unity we will print them separately so I'm gonna hit enter or actually why am I saying entered this whole time clear clear the console click here load you see now we have data us password Smith and username John Doe but it's still not clear because we did not extract it correctly well for that what we can do is now this we can say player extracted data is equal to G's on utility dot from JSON passing here player and passing here T which is this string you see the child T and now we can see something like print the player is call on and I can see here + extract the data that username and I can see here below the players pass the players pass is extract the data password so now we are going to convert every single child into a player class in your case it can be your game data or whatever and then we can use it separately so if I go back now let me clear the console let me hit play button then I'm going to clear it here as well again click here load so you see player is John player is Smith or actually passwords players passes Smith player is Kevin players pass is Doe in our case this is first name and last name but you get the point this is how you get the data as you can see we have John Smith and we have Kevin though so we are storing the data what am i saving these pages so this is how we can get and retrieve data from the database and again this is your task to film finalized this right here and this is how we save the data you're not probably gonna use it to store username and passwords you're gonna do that with the auth controller that's why I did that as well but here you can see your game data things like how many jams your player has and so on and so forth stuff like that and basically this is the of our tutorial thank you for watching guys if you liked the video please smash the like button and subscribe and hit the notification bell again you can download this complete project as well it will be available from my website link will be in the description below if I forgot to put these links that we just used as a reference to implement everything and to create a fire base and blah blah blah remind me please so that I can put them there and yeah fire here from awesome to calm I will see you guys in another video
Info
Channel: Awesome Tuts
Views: 55,058
Rating: undefined out of 5
Keywords: unity database tutorial, unity database, firebase unity, firebase unity tutorial, unity android database tutorial, unity firebase tutorial, how to create a database in unity, simple database unity, simple database unity tutorial, connecting a database to unity, connecting a database unity tutorial, unity database connection tutorial, how to use firebase with unity, unity database how to, how to connect database in unity, how to add database in unity, adding a database in unity
Id: Vdgd9TLczY4
Channel Id: undefined
Length: 60min 46sec (3646 seconds)
Published: Fri Mar 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.