Realtime Database in UNITY - FIREBASE TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back to Coco 3D in this video we will implement the Firebase realtime database so let's get [Music] started now in our game we use player prefs to store users data for example their name total coins current level or maybe the high score but there is a downside of using player pref which is the data that we store using the player PRS can easily be erased by simply going into the app setting and clearing the data this will remove all the player prev data that means all the user progress will be erased now we definitely don't want this to happen so that's where the Firebase realtime database comes in by using Firebase realtime database we can store all the users data into the Firebase server and we can fet the data whenever needed now we have to first use the authentication in order to use the Firebase realtime database because by using authentication we will get a unique user ID for all the players and by using that user ID Firebase can differentiate that this data belongs belongs to this player and not the other one now for the authentication you can watch my previous video the link is in the description and now let's open the Firebase dashboard and create our first database now I have already created the Firebase dashboard in the first video of this playlist so if you want to see the setup you can check out that video the link to that video is in the description but for now click on build real time database now click on create database choose your preferred location I will choose the Asia now click on next now leave this option as it is in the lock mode and click enable so here we have our first database which is empty right now so now first click on rules and make the read and write to true then publish this changes and we can dismiss this now let's open unity and start the implement mation so in here I have a very simple scene with main camera and directional light so the first step is to import the Firebase realtime database package so this is the package that we want to import Firebase database if you want to download these packages I will leave a link in the description but for now let's simply import it and click on import make sure that you have entered the key Store password as sometime it autor resolves the dependencies okay so now let's let create a game object that will handle all the database work okay so let's wait for this to finish okay so now let's create a script and name it as data saver now let's attach this script and let's start some coding so first let's add some using statements using system and using Firebase do database let's remove this start and update and now let's create a class that will have all the variables that we want to store inside the realtime database the name of the class would be data to save and let's create some variables inside it first would be for the username so public string username public int total coins public int current level and public int high score and we can add more variables if needed but for now this will be enough now let's create an object for this class and a string for user ID and let's also create an object for database reference DB ref now inside the awake let's take its reference like this now other than that we need only two functions first will be public void save data function to save the data into the Firebase server and second public void load data function to load that data back into the game so first let's work on the save data function so here first we will convert the class into its equivalent Json format so let's write string Json equals to Json utility. 2 Json and pass in the object now we have to save this data into the server and for that we have to write db. child users. child user ID do Setra Json value async and pass in the the Json data and that's it by using these two lines we will save all the data that is described inside our class into the Firebase database so now let's first test this thing now in the inspector let's fill in some dummy data for the user ID I will write the user 001 then for the name let's enter tester 01 total coins 98 current level let's enter 21 and for the high score let's enter something like 40,000 and now let's create two buttons to call the save and load function and now let's play the game and check that it is working or not so now we got an error and it is saying that the database URL is missing so let me check my Json file and I think we have to redownload our file from the Firebase server so let's quickly do that and yeah Firebase URL is here in this new new file so let's replace it with the old one so let's delete these files and let's import the new one okay now let's play one more time and let's check that it is working or not so this time it didn't give us any error so let's press save data and I think that the data is now saved in the Firebase server so let's open the Firebase dashboard and let's check our data now in the dashboard let's open realtime database and inside the users we CAU our data so here we have our all values that we just sent from the unity so yeah saving the data is working fine and now let's try to load this data back into the game so now let me remove all these values as now we have to load all these data from the server now let's open script and inside the load data function we have to create an i numerator because loading the data from the server can take some time so that's why we have to create an i numerator so let me quickly do that I numerator load data enum and let's simply call it from the load data function now inside the sign numerator we have to write where server data equals to db. child users. child user id. get value async and now we have to wait for this request to finish so we have to write yield return new wait until predicate and we have to wait until the server data dot is completed now after these two lines we can print that the process is now completed but we have to still check that we find some data or not so for that we have to write data snapshot snapshot equals to server data. result and then we have to convert it into a Json so write string Json data equals to snapshot. getet Raw Json values now we got everything inside this string Json data and we just have to check that this string is empty or not so if this Json data is not equal to null we can that the server data is found or else we can print that no data found it can happen when you try to find a user whose data has not yet saved and now inside the server data found we can simply fill all the Json data inside our class by simply writing DTS equals to Json utility do fromjson then pass in the class name and then inside the bracket pass in the Json data and now let's check that this this code works or not so let's save the script and as all the fields right now is empty so let's play the game and let's press load data and as you can see we got all the data that we saved inside our server so our load data function is working fine and now let's try to change the user ID of the player from user 001 to user 002 and let's play the game one more time and let's let try to load the data and as you can see no data found which is correct because we do not have any user in our server whose ID is user 002 and with that our saving and loading data is working perfectly and you can add more data to your class according to your needs but for now that's it for this video the Firebase realtime database is working fine we save some data and then we load some data back into the game now I will see you in the next video with some other Firebase topics but till then do like the video and subscribe to Coco 3D [Music] bye
Info
Channel: Coco 3D
Views: 7,109
Rating: undefined out of 5
Keywords: Firebase, unity, game development, Realtime Database, Save data in server
Id: hAa5exkTsKI
Channel Id: undefined
Length: 9min 13sec (553 seconds)
Published: Thu Dec 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.