How to: Connect your Unity game to Database, MongoDB Atlas, leader board, multiplayer cross platform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome guys in this tutorial I will show you how to create a simple high score system through connecting your VR unity game or any unity game to MongoDB Atlas which is a cloud database it's easy to use and it's very versatile you can now create cross-platform games or multiplayer games you can do whatever you want with this database it's very versatile so if you get any issues there's a patreon link in the description to the full project and all the code you can also ask me if you got questions or issues and I will answer so let's start like and subscribe head over to MongoDB comm slash cloud and create a free account select a shared cluster that's free you can then easily upgrade if you like and it will obviously be faster and you will have more storage but if you're only gonna store some text half a gigabyte will last forever almost of course and here we go this is the cluster interface so click connect and here you can Adam IP to whitelist but we do not need to do that now just create a username and a password you need to remember these it's very least the password so oh not that one I'm just gonna do test password and create a MongoDB user great and then later we will do the IP thing not right now and we will also get the connection string for the application but first we need to deal with the IP so go to network access add an IP and just allow access for any IP because we want any users of our game to be able to access this database so go back to the cluster interface by clicking cluster and now you can click on collection add my own I'm calling this database high score DB and the collection high score collection just to make it very obvious what I'm doing here so let's create an empty game object and call it database that's my dog if you hear that sound now create a script I'm calling it database access and just open up the script and at the very top of this script we want a variable that's the client so I'm calling it client equals new client and this is inside here we want to add a connection string but we have an issue right now we are using unity normally we would just download a new get package to get this to work but we cannot do that unity it's a different story so we need to add the dll's to the unity project so create a folder call it database and inside that folder create another folder call it plugins make sure you spell this correctly it needs to be exactly like this and then inside this folder we add the dll's now I spent quite a few hours modifying these to work with unity so they are available on patreon.com you can download them there and now we can access the mangu client inside our script all selectmen client alt-enter and import using and you can type it in manually if you like as well then go back to the MongoDB website and find a connection string by pressing connect and then look at c-sharp net 2.5 or later and then copy this whole connection string we're gonna paste that in to the client right here inside a string not outside a string there we go and you need to change the password remember I chose test password and you cannot use my string I will remove this from my cluster after this video so you need to create your own string next you need to create the I database the variable for the database and the I collection which is a generic collection and we will use a be some document and I'm gonna call this one collection but you also have to select be some document hit alt enter and import the using there we go now that works so in the start function set the database to client don't get database and inside here the string for what you named your database and I don't remember what a name mind so I'm gonna have to look it up go to collections and to the left you see high score DB that's the database name and we gotta get the collection equals client though our database don't get collection and that's a besom document and then what you named your collection for me it's high score collection next we can try this out this should not work we should have a connection and be able to interact to wit our database straight from unity so I'm just gonna create a fake be some documents to add to the database I'm gonna call it let's say the user name of the player is username and the score is 100 now we do collection dot insert one and we want to insert the document so now if we hit play we should upload username and 100 to the collection so let's test it out and we do not seem to have any errors so we can go to the website refresh it and right there it works awesome so now we can remove the whole test statement we did it right there remove the update function as well we do not need that and create a public ice async void save score to database and async its async programming if you do not know what that is that means that we can upload and download from the database without stopping the game if you do not make it ace async a synchronous then the whole game will stop and freeze until we saved the data save the data to the database this way the game will continue and we can update just update the score in the UI whenever it's done hope that makes sense you can Google asynchronous programming if you want so I'm creating a newbie son document from the in parameters username and score that we can send to this method we need to save something and a weight keyword before collection dot insert one async make sure that it works synchronously next we also want to be able to get all the scores from the server so I'm calling this method get scores from database and I'm also gonna create an inline class to represent a high score at the server so this is just a simple class that will have two properties one is the name of the user and one is the score of the user there we go so first I create a variable that represents just everything inside the collection this statement will find any document in the collection we are targeting there are many different methods if you only want to get a specific document you can look it up in the MongoDB documentation and since it's asynchronous it returns a task and we need to await the task to get the actual score now we're gonna construct a list of high scores from these scores because that's only a JSON object so we will get a string from that so let's do scores awaited to list now we'll have a list of strings all the entries in the collection and normally we will use a nougat package and translate the JSON into the object but since this is unity and I don't it's beyond the scope scope of this tutorial how to get nougat packages it's a pain in ass like the DL else from earlier so instead I'm just gonna do some old-school string manipulation to get the data from the JSON string you need to import the task threading so in the deserialize method I will just extract the important information from the raw JSON and populate the properties in a new high score object which we will return from the deserialize method I'm gonna try to take this step by step so you can see what I do it's only simple normal string manipulation nothing fancy first of all we're gonna call the database and get all the data but we need to remove this later this is only for testing purposes and I'm gonna press f5 to go into debug mode and see how the string looks when we get it from your database now when we press play we're going to call the database and return all the data in the specific collection and this is what gets returned we get a list of strings that looks like this so I'm gonna save this right here so I can remember it's easier that I can see straight away what I'm trying to work with what string string are working with so first I'm gonna remove the whole first part I don't care about the ID really for this high score list I'm gonna use substring and I'm gonna go from the index I want a substring from the index of these two characters starting there and I want it to go I wanted to start four characters before this one two three four right at the start of the username and then just take the rest of the string so I will have user name till the end of the string and I will hit debug and play to show you and here we go and you see right there we have username and the rest of the string so now let's do the next step let's extract the username from the string without ID string so do substring again and I want a substring from the first position where is where the uses the username start until the position of the I don't know what these are called two dots above each other - - so this position - one - that will get us only the username no matter how long you same is that should be the username oops so let's test it out again and when you're working with with strings like this you really should test the whole time because it's easy to think you're getting it right but you're not and that's right now we have the username lastly we need to extract the score so I'm gonna do the same thing again I'm calling this one score substring and this time I want to go from these two dots whatever they are called and I want to end one character I think after the ending brackets so let's get the index of the ending bracket - the index of the two dots if you do not understand this one I'm just making sure that I only get this core and you can copy this straight up I realize this looks confusing and this is not really how you do it when you just you download a good JSON NuGet package and do it instead but this will work if you're gonna have very complex strings JSON objects I suggest you just get figure out how to use a plug-in for this a nougat package there's probably stuff made for unity I just this is quicker for me I don't want to have to deal with it so let's populate the high score with a username and with the score but we need to convert the score to an int so convert to int 32 there we go I'm converting the string score to an integer and returning the high score so we will loop through the list and deserialize every string and populate a list of high score objects you need to display them somewhere so create an empty game object and inside that game object create a text much bro and we're gonna call it something like high score outputs good enough and create a text mesh this will be where we display the high score I'm not gonna put too much time or any time into making this look nice it's not what this is about I'm gonna change the color so it's easier to see though so black now we want to add a script here as well so display high score create and add the script and double-click to open now inside here we first want a access to the data base access so in the start function set the database access variable to game object the find objects with object with tag not on s find object with tag database access and then get component and get database access out of that game object just like that I can't spell Oh made it worse I'm just gonna copy this goddammit oh not my day there we go sorry and now we need to apply the tag to the database game object as well and create a tag there we go created add it okay so far so good now we want to create another variable for it takes match Pro there is attached to this game object where we want to display the high score we need to import text much pro they're using and we want to get the text match Pro just like that now we have access to the text match Pro object so let's create a new method to display the high score and set the high score output text to the database yeah this needs to be a synchronous because we got a call to database get what did I call this now I have memory of a goldfish I swear to god oh my god I gotta go do that method there we go awesome this will return a task and we need to wait the task not like that though I await tasks and now we need to I can't call it the same thing as the class of course I'm gonna call this method something else so I'm calling it in text mesh now we cannot do it like this because it's not a string it's a list of high score objects so I'm gonna create the string by looping through all that score objects in the high score object list so I'll put plus equals and this is what each line will say in the output so first a username and then the score just like that and now we can just output the output so set the high score output text to the output we just created and now we need to call this function somewhere so I'm gonna try to call it right here and this should of course be high score output equals getcomponent not two dots it's my bad guys and this will we have to do this in a it means to wait because at compile time we need the database to connect before we call it so let's give it two seconds before we call the display now if we hit play this should actually work we should load the data from the database in about two seconds and there we go straight from the database to the unity UI how cool is that so let's add a few scores so I'm gonna go to the start function in the database access class and just hard code in a few more scores so we can see that it all works so now when I compile I will add these two scores and then two seconds later I will fetch them from the database and display them in unity again oh it works but we need to make sure each is on a new line so let's add a new line character want to construct the string right there backslash n like that okay now remove this or else we will just keep adding them every time we compile let's try this out awesome look at that we're getting there and if we update the cluster on the website we can see that we're actually saving and getting the information from the database so I created a save button as well it's just like all the other buttons there's no difference there's a text much pro attached to it that is called save and it has the key feedback script so in the key detection script let's add another if-else statement cause if we hit the Save button we want to save whatever is typed so far and there's no error checking here you might want to check that player text output is longer than 0 or whatever but I'm not doing this right now so I'm just gonna copy the database access that I did earlier so I don't have to type it again it will be the same yeah just like that paste it in not there in the start function and when the player hit save we call the save score database we inset insert whatever is typed and I will insert just a random score right now when you do this you of course want to add the score that the player has accumulated in the game but I do not have a score right now so I'm just gonna get a random number and call the method width so that this will be saved as the score okay let's test it out this is using the link cable so I'm kind of connected I am connected to the computer still but I'm in the hands headset I saved it and let's check out the website to see that we actually saved it and that works Sayed right there so let's do this invoke repeating will for as long as the scene is active or the script is active we will call this display high score every 10 seconds first of the two seconds and then every 10 seconds so now when we add a new high score we get it straight in the list straight from the database and just to prove to you guys that this works I built to my quest and this is not connected to computer anymore this is purely from the quest you have to wait a few seconds for the invoke repeating to call you can make that quicker if you like if you need to and just like that so now there's so much you can do with this you can do a cross-platform game like that squirrel game whatever it's called in the owner oculus quest so users can interact with their phones cause you can just call the same database and share the information you can do high score list like I do here and so much more it's up to you guys please if you have any issues you can just ask me and I will try to help out thank you for watching like and subscribe take it easy
Info
Channel: mrPCoding
Views: 15,429
Rating: undefined out of 5
Keywords: #vr, #vrdev, #madewithunity, #oculus quest, #oculus rift, #Oculus, #Htc vive, #tutorial
Id: v-3NNV93jDE
Channel Id: undefined
Length: 30min 16sec (1816 seconds)
Published: Mon Apr 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.