How To Make A Multiplayer Game In Unity 2021.1 - Lobby Password

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

i will be following forever now

👍︎︎ 2 👤︎︎ u/AFugginHedgehog 📅︎︎ Mar 31 2021 đź—«︎ replies
Captions
okay so hello welcome back to another unity multiplayer tutorial today we'll be using unity's new official multiplayer solution to create a password protected room so your host can set a password and the clients that join have to enter the correct password to be able to connect to the server hope you're looking forward to it so let's get started as always if you want access to the project files there'll be a link down below to our github repository you can go ahead download that and have a look through the project for yourself to save us some time i've already set some stuff up in the scene and that means we can focus more on the programming in this tutorial so what we have here is we have the network manager which the only thing i've changed on here is i've added the player prefab and if we click on that the player prefab let's go into the scene view is simply just a character i grabbed off miximo and it has an animator on it it's not too important for the tutorial it's just there so that when we hit play we'll be able to have an idle animation for the player like so and the important part about the player prefab is that it has the network object component and this is how mlapi identifies an object over the network and this is used for objects that we need to sync and track from one client to another across the server so let's get rid of the player from the scene and just remember on the network manager drag that into network prefabs and set that as the default player prefab and then if i put this back in 2d mode click on this background over here we have this background on a canvas and this is just to hold the ui where we enter our password and we have these two buttons for hosting and starting as a client and then one other thing is a disabled button down here which i've zoomed in far too much there is just a leave button so we can stop hosting or just leave the server as a client and on the root of this is just an empty game object with a script called the password network manager and don't worry it's basically empty right now i just have these free methods that the buttons are hooked up to and then these three fields that we reference the password input field so we can read the password the password entry ui so when we enter the server we can turn this off and then the leave button so we can turn this on when we enter the lobby and with these buttons host client and leave you want to make sure these are hooked up to the buttons so host is hooked up to host client and leave so let's turn off leave because that's meant to be off by default and zoom back out so what we'll be using to implement the password protected lobby is something in mlapi called connection approval and there is a part for it on the docks and i'll be linking this down below just keep in mind that on the docks there isn't really a full example there's part of one here and a little bit more here but you don't really get a full thing for this and maybe in the future there'll be a more fleshed out example of this but that's what i'm doing today i'm creating a tutorial here that you can follow through from the start to the finish to have a password protected lobby so now we're ready to start making it let's head back into unity and just make sure on the network manager we enable connection approval and with this off which is by default when someone tries to connect they'll just join and your server will allow it but by enabling this you can write your own logic to say there's too many players on the server don't let them connect or their passwords wrong don't let them connect or whatever else you can really make this logic be whatever you want but in our example we're just going to do password protection and with this turned on now let's head into our script so let's start off in the host method this is called when we hit the host button in the ui and we want to of course start hosting and the way you do this is networkmanager.singleton.start host but if we just do that and then for example in the client let's say we just did network manager dot singleton dot start client this would work the same way as the hello world example does where you just start and you let someone connect but we want to add our own logic here for letting people connect so on the line before we host we can say here network manager dot singleton dot connection approval callback and this allows us to subscribe our own method with our own custom logic that determines whether someone should be approved to connect or denied and let's call it the approval check and let's use visual studio code to do this for us so we'll hit control period generate the method and i'll move it to the bottom and this is the method where we write our custom logic now let's rename the arguments because they're just currently arg one two and three we'll call the byte array connection data so this will have our password in or whatever you decide to send the ulong is this client's id so we'll call it client id and the connection approved delegate is a callback and inside this method we need to actually call this callback and pass in some data such as whether we should approve the player or the client uh where they should spawn which prefab they should spawn in what rotation they should spawn in with all that kind of stuff goes in here so what we should do in here before the callback is to determine is the password correct and currently the password is a byte array which is no good we need it to be a string so let's say here string password is equal to and we want to convert a by terrain to a string so we're going to use encoding dot ascii dot get string and by doing get string you can pass in a byte array so connection data and that will convert the byte array back into a string so now we know what password it is that they tried to send so now let's make a ball and this is whether we should approve the connection so we'll call it approve connection and this is equal to and then we need to say here does the password equal the expected password so does the password they're sending us match the password input field dot text so when i hit host whatever password i've got in my input field is the one that we're expecting so now we have this ball approved connection that's true or false based on whether the password is correct or incorrect and now we can actually call the callback and to know what to pass in here if you mouse over this connection approved delegate you can see the first thing is a ball whether we should spawn in a player for them so this will be true the second is the player prefab hash now if you pass in null here it will just use the default player prefab that we set in the network manager so leave that null the next thing is the approved ball so we're going to pass in a proof connection then we've got the position and rotation so for us if we pass that in as null then it will just go for zero zero zero so for now we'll just go with null and what this will do for us now is just say when someone's connecting if they have the correct password spawn them in and now of course we need the client to be able to send the password so the way you do that is before you start client you want to set the password so network manager dot singleton dot network config dot connection data this is where you set that connection data and we want to set it equal to a byte array so we need to take the string that's in our input field and convert it to a byte array because you can't just send the strings over the network they need to be converted to bytes so encoding dot ascii just like earlier instead of get string it's get bytes and get bytes can take in a string so password input fields dot text and with just this we actually have it all working of course there's some more stuff we're going to do with leaving and the ui but it does work so if we head over to unity file build and run and then run it in the editor i'll open up my build over here and what we can do is we can set a password let's say abc and hit host the ui will stay up but we've started a server as a host and then if i go over here and say a b but don't put the c so i get the password wrong and hit client doesn't work doesn't connect but if i edit the password and say abc and hit client it now connects and you can see me uh join the server and there's two players now we're on top of each other and the ui is still up but it does work so back in the code we want to hook up to some events some logic where we can enable and disable ui when we connect to the server disconnect from the server and those various things so let's use the built-in private void start and private void on destroy and what we can do here is we can say we want to hook up to network manager dot singleton dot on server started plus equals and we'll make a method called handle server started and we want to do this for three different events so let's copy paste it three times we're doing on server started on client connected callback and on client disconnect callback and then handle client connected handle client disconnect now let's make sure to unsubscribe from all of those in the destroy method so we will change all the pluses to minuses down here and i'm going to also add an if check before this because when you stop playing in the editor and it tries to unsubscribe all these events but the game has stopped completely so the network manager's been destroyed you actually get some errors and whilst they don't actually cause any issues it's always nice to avoid those errors so let's say if network manager dot single turn is null then just return and then we need to actually now make these three methods now let's start with handle client connected so we'll go down here private void handle client connected takes in a you yulong client id now this is called on the server every time a client joins it's also called on the client side when they themselves join but not when other people join now because we're running this game as a client but we also have it so that we can host the game we want to make sure it works for both so we're going to say whenever a client connects if that client id is our id so network manager.singleton.localclientid if it is us then let's turn off the password entry ui by setting active false and turn on the leave button dot say active true so effectively saying here when we connect turn this on off now let's take this method copy paste it and just change it to be handle client disconnect and then inside the logic here we want to flip the false and true so when we disconnect we enter the password entry and we turn off the leave button because we've already left now there is an issue currently but it is getting sorted so by the time you watch this it may have already been fixed but right now on client connected doesn't get called for the host when it's themselves that's connecting so we need to make sure that we call that manually for now so what we can do here is we can make this handle server started method so let's do private void handle server started and all we want to say is whenever a server is started if it's running as a host which is a server and a client so network manager dot singleton that is host so for running as a host let's call manually handle client connected with our network manager.singleton.localclientid so we make sure that um mlapi calls this for everyone else and we're calling it manually for ourselves as a host this is just currently a workaround and the leave method's looking a bit empty so let's go up to the leave method and let's say when we hit leave if networkmanager.singleturn.host if we're the host then let's stop hosting networkmanager.single and when we stop hosting we want to unsubscribe from the approval check so doing the reverse let's paste that down here change the plus to a minus then we can say else if network manager dot single 10 dot is client then we can say network manager dot singleton dot stop client and in both these cases when we hit the leave button we also want to take this uh password entry true leave button false and call it here regardless because in both these cases we are still a client so we want to turn on the password entry and turn off the leave button when we leave and one other thing we can do down in the approval check is pass in here the position and rotation we'd like to spawn in with and in future tutorials i'll do a much better version of this for now i'll just do a bit of messy hard coding so we can make it look nicer in the game so what we'll do here is we'll say vector free spawn pos and quaternion spawn rot for rotation and we'll pass those in so spawn pause spawn rut and now we just need to set them so what we can say is when someone connects switch on the network manager.singleton.connectedclients dot count so this is how you find out how many people are currently connected and we say when we're connecting if there is currently one of a person then we want to spawn in what position and in my case i've set it up so that i want the spawn position to be 0 0 0 and i want the spawn rotation to be quaternion euler 0 180 degrees and 0 degrees so if there's already one person in the game i want these settings whereas let's copy paste this if there are two players in the game already so i'm player three then i want to spawn at an x of two and with a rotation of 225 and so this is happy we need to make sure we give it a default value so let's say vector3.0 and the default value of a quaternion is quaternion.identity and this accounts for clients joining but the host themselves which is a server and a client doesn't actually have any of this code called because it's not going to check their password if they're the ones setting it so we can scroll up and where we call start host you can actually pass these things in we can say new vector 3 minus 2 0 0 and then quaternion dot euler 0 135 degrees zero degrees so now when the host joins they'll spawn in at this position and this rotation because they're always the first player whenever other clients join they'll spawn in with these positions and rotations now this isn't foolproof uh if player 3 or sorry player 2 was to leave and rejoin they'd be in the position of player 3 because they'd now be the third player joining so that isn't taken into consideration here but it really doesn't matter because it's just for a visual indication of free players joining let's go back into unity we'll go file build and run and we'll see it all in action so we'll do this with the free builds here let's pick one of them to be the host let's say the one at the bottom and we'll set the password to be password we'll hit host and over here i'll try joining with the wrong password let's just type some random stuff in and hit client nothing works same over here doesn't work but if i say password and hit client it now joins and you see them both here it's spawned in the position and rotation for player two and for player three pass word hit clients and they join as player free and of course leaving works too so if i decide on player 2 which is up here to leave the middle person will now be gone so yeah that's it for this tutorial i hope you enjoyed and if you did please leave a like and subscribe share the video of anyone you think that would find it useful let me know down below what you want to see next thanks as always for watching and i'll see you in the next one goodbye but of course before i go i've got to thank my patrons a special thanks to mack francisco lira liz kimber bird or die benjamin hilda cat from garfield colter david mcdermott evan maxey jake nixon joris letter casey kattinkaman malvin mike troupe wreck san marcos wolfgrim andrew williams chris diplock fury and dario if anyone else is able to help support the channel monetarily link to patreon is down below if not there are links down below to other social media such as twitch twitter and discord you could help us out by following or checking any of those out that'd be greatly appreciated thanks again for watching and i'll see you in the next one goodbye
Info
Channel: Dapper Dino
Views: 38,191
Rating: undefined out of 5
Keywords: unity multiplayer, unity multiplayer tutorial, unity multiplayer 2020, unity multiplayer 2021, how to make a multiplayer game, unity multiplayer game tutorial, unity networking, multiplayer networking, unity multiplayer game development, how to make a multiplayer game in unity, new unity multiplayer, new unity multiplayer system, unity multiplayer password
Id: Pe2LVZGTK20
Channel Id: undefined
Length: 17min 5sec (1025 seconds)
Published: Tue Mar 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.