How To Make A Game With Unity Multiplayer Netcode | Relay Service Setup

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody is it dumber and welcome back to my channel so in today's video i'm going to continue with netcode for game objects and specifically we're going to be looking at how to set up a relay server i'm also going to show you how to do what's called an allocation so that we can connect to the service and also how we can join a game after we get a joint code i'm going to be providing you with documentation and also code examples of how to accomplish all these tasks so let's jump into my computer and start working on it what's going on guys so what we're going to be doing today is adding functionality to the existing project that we worked on during the last four videos and that functionality is going to allow us to play multiplayer games and the games that we're building over the internet so we have a lot to cover so i want to make sure that you stick around so the only thing that i added on these before we we first started today is i added the join code here so if we go now to the ui you're going to see that there's going to be a joint code it's just you know a text box and then i just rearranged some of these buttons just to make them look better and also i added this debug area so if you look at the scenes now we have a basic multiplayer scene and that one is going to be for when we're using unit when we're using more of a lan you know multiplayer connectivity this one is going to be four times when we're going to be using the unity services which are going to be in this case with uh with the relay so we have a lot to do so i'm going to start with the fundamental thing which is to set up basically a project and a project which is going to be a unity project which is different to the project that you're seeing right now so if you go to build settings and then player settings you're going to see that i already have one project in here but i'm going to unlink it because i want to create a brand new one and this is going to be the first thing that you do when you want to set up unity services is basically to create an app that is going to be in the cloud and therefore we can use some of the unity services such as lobby such as relay and other things that unity provides so i'm going to select the organization in my case i have two so i'll just do dilmar and then it's going to tell you do you want to use an existing projector id and i could go in here and then basically select one if you had one already what i'm going to do instead is i'm going to i think i can just go back i'll just go back here and then i'll just do it one more time select and then i'm just going to create a new project id and this is going to you know go out and then create a project id for me he created a project id with the project name that i already had and then it just added the number one and that's okay i think for the demo and then in here it'll tell you if this app is going to be target for children's and in this case this is not gonna be target for children's under 13 so just go ahead and hit save and once you do that we're gonna have basically some of the major parts that we're gonna need so if we go now into we can go ahead and open up a new unity a new chrome window actually and in this one we're gonna go into dashboard unity3d.com you will be prompted to logins i'm already logged in as you guys can see here on the on the top right and you're going to see that this is going to be the multiplayer playground this is the one that i had already so we're going to go into projects and i want you to look for the one that just got created so in my case is the unity multiplayer playground one because i already had another one is it gonna be some of the major parts of you know understanding how unity gaming services work you're normally going to get a project id you're going to get a project name and also an icon and obviously billing and you know if you want to use some of the pay plans that they have some pay plans in there for or demo we're just going to use you know the free and then we're gonna be using just the relay service so if you go down to multiplayer relay and this should already be selected and just make sure that that is selected you can't really see the name of the project because it's too long but i know that it's that's the right the one that is selected and then we can go into getting started and it's going to tell you here what you need to do this is what we just did we went to services selected the organization and also the project so we're going to go there we can go next and this is going to tell you okay download the relay sdk so yeah we'll we'll do that we'll just go ahead and hit next and then you can do relay down so we're going to be activating it and then once you do that it's going to basically you know finish up the project but we're actually going to go back let's go back one more this is actually really important here just make sure you click on that and we're going to stay here as well so we'll go into here and this is really cool i didn't understand the ui at first i'm going to be honest and then once i used it i'm like okay this is what it is doing so what this is doing behind the scenes is just saying okay what kind of packages you're going to use if you were starting a project from scratch you normally will do something like netcode for game objects but we already did that so we don't want to select it we don't need to do a network profile and i'm gonna do just a relay and let's see i think that's everything i also need to do the the adapter let me see if i can find this i have that and there's also another component that i don't see at least in here let's click on generate code snippet and if it doesn't if it doesn't list it in here that's okay i'll show it to you because we're gonna need it as well it's going to be an adapter that we're going to need but anyways so you'll do that and then generally go to snippet and then we're going to be basically copying that and that's what you're going to be putting into your manifest so i already did that but i'm going to show you the other packets that you're also going to need so we go here into unity and normally what i do is i just right click on packages and then show an explorer and that way it'll open up everything in the explorer and then we can go into the manifest and then i can just open that with code and i already have in here what i need i'm going to make this bigger so you guys can see so if we look at the package the packages that we have these are going to be the three ones that you're going to need and if i paste this in here you're going to see the same version so i can undo that the one that i was looking for was this utp because we're gonna need what's called a unity transport protocol to basically reroute the information to the relay but i didn't see it for some reason we might be getting installed once you do this but just know that these three are going to be the ones that you need so you basically just put them in here and once you do that and then you save your project it's going to install everything that everything that we need so once you do that the next thing that i'm going to do is we're going to go into my network adapter here and currently we have the unit transport and we're going to use that but not on this scene we're going to use it on the main scene so we're going to leave that one alone on this one what i'm going to do is i'm going to go ahead and remove it i'm going to be adding a new one and this is going to be the unity transport and that's going to be the package that i show you that was missing that it's going to give you this that's something that i i basically had my head against the wall for a while and then i figure it out by looking at some of the demos from unity and then you're gonna change this to re relay unity transport and then everything in here is gonna stay the same we don't have to change anything else except that we need to drag and drop this into the network transport and once you do that that's going to be basically the transport that we're going to be using for our network manager so we're multiple doing multiplayer it's going to be using that transport to basically do the communication but we still need to reroute everything by doing some code allocation that i'm going to be walking you through so if we go back into scripts i also have a unity a relay manager so i'm going to go ahead and drag it and drop it here and right now all it is this is just a singleton and it's just a class i didn't implement anything because i want to show you how that works and also if you go to the canvas here we have the ui manager just know that this button is also the text box where we're capturing the code it's already being bound to the ui manager so that's something else that i added so i think that's everything that we do as far as like what we need to do here all you need to do is just make sure that you have the unity transport and we add the relay manager this can also be static if you wanted to i wanted to implement it this way because of some changes that we're going to allow to do so i'll just go ahead and double click on my relay manager here i can make it a little bit bigger and right now like you see like you see it has a relay manager and it has singleton and these singleton just come from the implementation that you see on every video and if you want to download these you know it's in the repo which is in the description of this video so there's a lot to do so i'm just gonna explain everything part by part so we're just gonna do a serializable serialized fill and then just bring in the unity engine and i'll explain everything as we as we're typing so it's gonna be basically for the environment and the environment by default is going to be production so let me show you something related to production so if we go back into the unity services and you go into your environments here unity provides you with a way to change the environment and this is really cool because if you had a qa environment a staging type environment you can go in here and basically do your testing environment so i could say okay this is going to be a staging or this is going to be qa or however you want to call it you can set up different environments so that you can do testing with those environments and and basically what happens is if you change this in here we're going to tell unity to use that environment so that when you do analytics and things like that maybe you have a different pay plan then that goes through that type of environment i haven't really used them use them other than you know i know how to handle it through code but if you guys do find it helpful i think especially if you're going to do production you're you're gonna use this for production once it's out of beta i think it's going to be very helpful so the next one that i'm gonna do is gonna be the max number of connections so by default i think right now unity is only allowing either five up to five a concurrent connections or ten concurrent connections so i i'm going to set it i think i set it to 10 and i think that works so we'll just leave it at 10. but that's only during beta ones are out of beta it's going to be you know as much as you're you know allowed to use or paying to use and then the next thing that we need to do is we need to get basically the unity transport so to do that we're going to do unity transport and i'm going to call this one transport and the way that we're going to get it is we need to get it from our network manager so i'm going to say network manager instance and no instance singleton and then this is going to allow us to access the game object and then get component and then it's going to be our unity transport so the way that it's going to work is we're going to try to get the united transport so for the first scene we won't get a unity transport so it's going to be null then we know that we we can't really use the relay we know that we can just use unit so that's why i have this one here and i can also access it below to do the allocation of the relay server and also joining again which i'm going to show you as well so the next property or or yeah property is going to be is relay and then this one we're going to say transport so i'm just going to say if it's not null and as long as the transport protocol it's going to be the relay which we have here so we have a unity transport and a relay unity transport so the one i'm going to be using is going to be the relay unity transport and if we can make these maybe i'll just bring this down so you guys can read it since i have a large font so that's going to be all the things that we need like as far as like instances so the next thing that we're gonna need is also gonna need how we're gonna be setting up the actual relay connection so i'm gonna do an sync and then in the sync i'm gonna do a test and this is gonna be getting a specific data and that's going to be relay host data and i'll show you how that looks like and then it's going to be set up relay if we go back here so let's go ahead and take a look at let me bring in this task and then i'm also going to do f12 to go to the implementation of this so when we allocate the relay service this is what we're going to be getting back we're going to get getting back a joint code and it's going to be the join code that we can access basically for another game and a client to access the the game is going to require that they provide a joint code so we're going to get a joint code we're going to get also an ip for address of the relay service also a poor an allocation id a location id by its connection data and key i'm going to be linking to the description of the uh basically to documentation about this so you guys can read it and we also have in addition to that we're going to have a relay join data and it's basically exactly the same the same contract except that it's going to have host connection data that needs to be basically passed to the transport if i say basically again bug me in the comments because i need to i need to learn to stop saying that so anyway so we have that and that data is going to come back so the next thing that i'll do is i'm going to need to basically get and i'm not going to say that again initiate let me just do that again it's going to be initialization options and i'll bring that as well it's going to be options equal new options and then in the new options we have also a second environment second environment make sure that i can do that name and you're gonna try to see okay where is this why is it not coming in because this is actually an extension that they provide and you can see that it actually brought these in and this is cool because now we can specify okay what is the environment that we're trying to set up options for so in our case it's going to be it's just going to be production okay so once we have the options that we're going to be passing in to basically initialize the unity services we can we can now do this we're gonna say unity services and then initialize a sync and this is gonna be taking a parameter and if you go and hit up arrow you're gonna see that it takes the options that we just declared there so i can just do options okay so far so good so we have options we have initialized the sync so now what we need to do is are we already authenticated so we need to make sure we check for that authentication service and then you can do control period and once you do that is you're going to get an instance it's going to be an instance and you can also determine if we're currently signing so we need to make sure that we check for that because if we don't then we need to make sure that we're signing in so i'm just going to do away authentication service and then instance and we can sign in anonymous and this is all based on the example that i got from from the ballroom and also some other documentation that unity provides so so far so good we've basically initialized we are trying to log in and i'm pretty sure that we're going to get a talking behind the scenes in here so we can talk to the relay service but we we won't really have to worry about that because all of this code is going to be doing that and then we need to also use something called allocations it's going to do a location and just do away because this is all using asynchronous calls and i'm doing i'm going to be bringing unity services that relay instance and this is going to say all the different methods that we you know that we're going to have available like join a location async lease regions if you want to look at different regions for this service you can you can find that out as well we're going to be using the create allocation async so this is going to be taking a max number of connections so we have that and like i said during beta it's going to be a small number and then out of beta it's going to be a much larger number okay so in here we need to do relay host data and it's going to be that we're just going to be creating an inline object so that we can specify all different parts and then i'm going to do key allocation key we also are going to need the port and i believe this one it's going to have to be cast to that you i use short and then i'm just gonna do allocation relay server i believe that one is poor and then the next one is going to be allocation id allocation and then obviously it's going to be a location id allocation bytes allocation and then i'll get the bytes it's really important that you don't miss any of these things because i had issues with things being you know mapping correctly so that's the last thing that you want to do is map something incorrectly and then spend hours trying to find out what the issue really is and then it's going to be connection data so we're just basically getting the information that we got from the relay service by creating a new allocation and then we're getting the data built so that we have that information in an object okay so now that we have that the next thing that we need to do is we need to request the join code and i already have that join code here which is going to be part of that object and then i'm going to be calling into a new method which is going to be part of the relay instance and i believe you already saw it because i was i was just showing you all the different options so if we do get join code sync but we need to say in this case relay host data and then we need to pass in the allocation id so this is just creating what's called an allocation once we get the allocation i'm going to get all this data back and then we're saying okay relay service can you give us a joint code because we have authenticated correctly we have and at this point we should have a joint code that we can pass to the clients and they can join basically they can join our game it sounds simple and it is simple but it took me some time to to figure it out okay so now that we have that we're at that point i need to set some information to the transport right because the transport doesn't really know anything about what's happening the transport doesn't call this automatically we have to call it so we need to tell the transport that we got some of that information back and what that information is so it's going to be relay host it's going to be the ip address we also need to say relay host data this one is going to be the port the next one is going to be the allocation id bytes the next one is going to be the actual key and make sure that you do that correctly and then the next one and i'm just reading the parameters here it's going to be the connection data bytes and then it's going to be the connection data and then as far as like the additional parameters in here like host connection data bytes that's going to be something that we pass on when we're joining a game it's not going to be something that we do currently and then what i'll do here also just say relay host data okay so this is great but i want to see what's happening so i'm going to go ahead and add a little bit of information to my log so i'm going to say logger instance log info in this case i'm going to use interpolation here if i can type that correctly relay server and starting with max connections and then i'll just pass in the max connections that we said just it's always good to have as much logging as we can as long as you know if it's not overkill then i wouldn't but in this case i want to say relay server get join code or or generate it generate a join code and we can just pass that join code i'll just go ahead and copy this here and i think that should give us everything everything we need to to set up a relay server and now we need to do the join so i'm just going to go ahead and do another method here this is also going to be a synchronous it's going to be a task this is going to be instead of the the relay host is going to be relay join data and then it's going to call it join relay we're going to need to pass in a join code because at this point we're joining the game so we need to tell the relay service okay what are we joining who are we joining and in this case it's going to be you know join co which we generated here from the from the actual server okay so now that we have that i'm just going to go ahead and copy this part right here which we're going to need again so we can do that you can also put that in a method if you don't want to duplicate the data and then in here i'll just do something a little bit different it's going to be join allocation we can say join a location if you want to just say allocation just to keep it consistent and then away we can call into our relay instance which is a singleton and then the way that it's going to work is in this case we're not getting a code we're joining so it's going to ask that we're passing the joining code which we are passing right now so once we pass that we should get an allocation and that location is going to have additional information which i could type right now but i'm just going to go ahead and copy it because i've been lazy and i show you that here so i didn't want to do it one more time but anyways all we're doing here is just building up another object and just mapping just like a map before but in this case we get additional information because now we have to pass in the host connection data because we're going to connect into the host and also the the joining code that's the only thing different in here and then what i'll do here i'll just do i have to access my transport we also need to tell it what a relay information is going to be so i'm going to say relay join data and it's going to be ip4 and then i'll do comma relay john data it's gonna be my poor and i'm gonna be surprised if i don't get any errors because i am typing everything very quick so hopeful hopefully everything is gonna work so just bear with me guys don't don't drop yet and then it's going to be on my connection data and then lastly my host connection data so if this was secure you can set this to true i i haven't used it but i would imagine if it's something to do with ssl then you would have to set that to true but normally i don't see that that is happening unless it's happening behind the scenes because the urls to the to the relay service are all generated automatically but i just wanted to show you that that was there and then i can just return the relay join data and then let's also add a little bit of logging in here i'll just add some logging at the end and then i'll just say instance log information and then in this case i'll just say that our client join game we join code and then we can just pass in the join coding here so that we know if we don't get to this point that means that something bad happened in this case so it's always good to just to keep some some of that login information available okay so that's really cool and also now if we go back into unity let's just make sure that nothing is going to blow up and we need to now that we do we did that we need to change how we start the client and also how we start the host but i want to check make sure that everything is working here if we go back in here we have the environment production and that's why i make this one uh basically a singleton monobehavior because i wanted to be able to change that here through the inspector but in the unity documentation you'll see you'll see them using basically a static class and you can do that i mean it just really depends on what you're what you're currently building okay so that's cool now let's go ahead and go back and we're gonna go and focus here on the ui manager just for a few minutes because we're gonna have to change how these works well we won't have to change how this works but we'll need to add more additional basically additional implementation so one of the bugs that i had is i was generating so basically what i was doing and i want to i want to make sure you pay attention to this because i had a lot of issues with it and the unity multiplayer team gratefully and i want to thank them they helped me through this problem so the way that i had it set up initially is i was calling into setup relay as soon as the game was starting and basically on the awake method and that's cool and that worked but i had an issue where i was getting an error saying like the joint code could not be found and it basically didn't work and it didn't work because when you set up the relay when you call this method you have to call you basically start your host right away if you don't do that i think unity on the service has a timeout or or basically like a time difference of when you can do those two calls so make sure that if you set up uh if you're setting up a relay you have to start the either the server or the host right away otherwise you're going to get a lot of issues and again i want to thank the the team at unity for helping me through that because it was really driving me crazy so if we go to the start hose this is what we're going to do here the first thing that i want to do is i want to make sure that i have my relay the relay is enabled and if it is enabled then we can basically just set it up right we're going to do away and if we're going to do away that means that we need to change this to be a sync so i'm just going to say relay manager instance and then i'm just going to do my relay and we can just set up the relay and that's basically all we need to do here because most of the implementation is in is in this guy so similar to that we're going to have to do something in the in the claim and if we go to the client we're going to do basically the same thing but in this case we're going to be joining a game so we're going to be joining in that join code remember the client has a they put it into a joint code input and that's where we're going to get it from but i always like to make sure that i typed it in before we before we do that we also need to make this a sync otherwise it's going to complain because we're using away and then i'll also do like as long as the string is no it's not null or empty then we can we will do this right want to make sure that we always check for validity before we do things so if the relay is enabled and they type in a code we're going to be joining the relay and then we're going to start the client right away so that's basically everything to get this working so let's go ahead and test it out i'm going to go ahead and start my host i did get a code and you can see that the character is also in the game so let's try and see if we can enter that coding here so it's going to be 6 bq ffn and i'm going to go ahead and start the client and if everything worked i should see client join the game with code so i can move my client here and i can also move my other client here and we can see that we have two games two players in the game and i know this is working because this is a new scene where we have the new transport i'm also going to on the server side i'm going to go ahead and execute my physics you can see that everything and everything is rendering and running correctly so that's everything that i wanted to show you guys today if you guys have any questions about these if you have other questions about future videos let me know in the comments because that's going to help me in bringing you more videos and make sure that you subscribe to the channel because that's going to allow me to keep you basically informed what i'm doing with a lot of different prototypes thank you guys
Info
Channel: Dilmer Valecillos
Views: 2,333
Rating: undefined out of 5
Keywords: unity relay server, unity relay server price, unity relay service, unity udp, unity udp server, unity udp networking, unity transport, unity multiplayer, unity multiplayer tutorial, unity multiplayer 2020, unity multiplayer 2021, how to make a multiplayer game, unity multiplayer game tutorial, multiplayer networking, new unity multiplayer, new unity multiplayer system, netcode for game objects, unity netcode tutorial, setup netcode, network manager, multiplayer game dev
Id: 82Lbho7S0OA
Channel Id: undefined
Length: 27min 52sec (1672 seconds)
Published: Thu Nov 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.