How to setup Global Matchmaking for Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
unity provides easy global matchmaking using their relay and lobby services i created this lobby system you see on the screen for you to go through in your own time inspect how it's all done so that you can easily implement it into your game you can find the project files in the description but for this video we'll be going over a simplified script just to teach you how it all works okay so to get started we'll just need a few prerequisites go to windows package manager and you'll be installing lobby and relay so these will be in unity registry you'll probably also want to install netcode for gameobjects that's in beta at the moment so you won't find it in unity registry you'll just need to add it by git earl here i'll put the link in the description and on screen once you've got those installed just close that and now once you installed those you've probably got a little dialog box asking you to link a project if you did follow through those prompts if you didn't go to windows general services and click general settings here and it'll ask you to select an organization you might have to create one if you haven't already go create project id and this will actually create a project on the unity dashboard for you after you've done that go actually click the dashboard button here you'll have to sign in if you haven't already and now from here go to multiplayer click relay get started and then just follow through the prompts here i'm going to pretty much go through all of this so you can read it anyway but you don't have to turn on relay next finish and then do the exact same thing for lobby let's go next next turn it on next finished cool bananas and then you can just exit out of the dashboard here now you will have to restart your editor just for the token to refresh so i'll be back in one moment okay so i've just got a relay example here and this will just touch relay and we won't even talk about lobby for a moment so let's go through this simple uh flow so to use most of the unity services you will need to authenticate two unity services so in awake i'm just calling authenticate now in case you're unaware unity services is the umbrella which holds a whole bunch of amazing multiplayer services sorry for example if you want to save your player data online you can do that if you want to run sensitive server-side code you can use cloud code there's a whole economy system uh obviously there's the netcode and the lobby and the relay which we're going over at this video but it's an absolutely amazing stack of tech so do check it out link in the description so all we're doing is initializing the unity services and then signing in anonymously now signing anonymously is a really frictionless way to get your players into the game and start playing you don't have to prompt them to log in straight away or register an account with a username and email or use their google account you can just let them start playing straight away and then maybe later on if they've played for a while you can say hey would you like to back up your account maybe link it to your google account or your steam account or something like that sorry but for this i'm just going to log in anonymously now from there i've got two buttons join and create so these two functions here and this is actually a good time to explain exactly what a relay service is so if you ever played a multiplayer game right back in the beginning of when multiplayer games were just starting to come out you may remember that your friend would give you their ip address and then you'd go on your client and then put their ip address in and then hope that they had set up their firewall rules and they had forwarded their ports properly what relay does is it acts as a proxy between you and other players so the relay service will be here you'll connect to the relay service all the other clients will connect to the relay service and basically your connection will just like propagate back down to them what this means is you don't need to know your friend's ip address or any other information you just need to know where the relay service is which is obviously very simple and the way we initialize this flow is we create an allocation on the relay service which is as simple as this so all we're doing is we're sending in how many players we would like this uh game to be i'm just saying it's going to be five for now but you can set it whatever you want additionally you can add an optional argument here which is the region if you just leave it as null it will do a whole bunch of quality of service checks on a whole bunch of regions and find the best one for you automatically but you may have actually locked a region to your user base so it might be oceana or the americas or whatever so you'd put that in there i'm just going to leave it now for now once you have created your allocation you will now request a join code from the allocation now what this join code allows you to do is give it to your friends or whoever and they can use this to join up with your allocation that you have just made which is what they're doing here when they click join game that is all they need for you two to connect you don't need ips you don't need to forward ports or anything like that just to join code once you have both grabbed your allocations you now need to actually utilize it by utilizing it i mean applying it to our transport so one thing to note if you're wanting to use unity relay you will also need to use unity transport so it's not uh transport agnostic and i'm pretty sure in the same way if you wanted to use the steam relay you would need to use the steam transport and same goes for the epic and the epic transport so to apply it it's slightly different on the host and client but basically we're just sending in a whole bunch of information from the allocation now this is quite verbose and i'm assuming most of the time people are going to be using these exact same details from the allocation so it would be nice to have like a little helper function or something saying apply allocation to transport or something and basically you just send in the allocation but maybe in the future sometime and on the client you'll say set client relay data one thing to make sure just because this tripped me up for a little while uh make sure you're setting host relay data on the host and client relay data on the client and then followed by that you just start host and start client just as you normally would with any netcode game only difference now is that we're going to be using a transport through the relay system right so if we if we didn't actually set these uh things here and we just said start client start host we would not be connecting to each other's game unless we're actually sitting on the same computer anyway continuing on we just start host as we normally would and we also start our client as we normally would only now we've got like this uh these special settings applied to our transport so we'll be connecting through that relay service and we can play together so let's actually test that out i've got my client here and i'm going to open up my parallel sync and create a clone here if you need more information about this i did cover this in the netcode video but it's just a way to test locally your multiplayer game without building it every single time super duper handy once that's done just open in a new editor okay i've got my second client opened up here on my main client i'll click play create and that will be the join code up there on the client i will enter the join code join and there we go we've got our two guys now this seems no different to the previous netcode video right we could do this on the netcode video but this would actually work globally you don't have to be on the same computer not very easy to test that obviously to show you but take my word so that is relay unity has sponsored this video so i'd like to take a moment to talk about how excited i am personally about the rollout of unity's gaming services never have we had everything in one place to make a multiplayer game previously you'd need to use a third-party networking library like photon fish or mirror for the real time component for the back end you'd need to fire up your own server and database or use a third-party backend as a service for things like authentication saving player data to the cloud or managing your economy now it's as simple as installing a few official packages and away you go all the systems synergize really well with one another and quite honestly have some of the best documentation i've ever seen along with sample apps to get you going not only that but the free tier thresholds are also high enough that you won't pay a cent during development i've now covered netcode relay and lobby you can count on me covering the remaining services too so jump in and have a play link in the description now you may be thinking taro that's a bit archaic that we have to i have to give this join code to my friend right and yes that is correct which is where lobby comes in so i am going to just swap this around a little bit i'm going to remove these buttons and just add this play button here and instead of this relay sample i am going to make this my uh simple match making example all good enough simple matchmaking and there's my buttons here cool so now we've just got a play button and the reason i'm doing this is to just simplify the flow a bit we're kind of going to have a like an automated you click play it will look for a lobby to join if there's one available and join it if there isn't one then we'll create our own lobby and wait for other people to join so let's open this simple matchmaking script and it's only a little bit different joining these two things together so as usual i am authenticating as soon as we press that button but this is a little bit different just for local testing so we're using parallel sync here and lobby requires the user to be different if if you're going in with the same user which will be the case if you're on the same system lobby will say there's already someone in here by that id go away so what we can do here is hook into parallel sync's custom arguments so in parallel clone manager you'll see we've got this argument here and if you create a whole bunch of clones you can make the next argument client two client three whatever so all we're doing is we're saying if we were in the unity editor and this version is a clone please use my argument otherwise use primary so every id is going to be different now and then we just sign in anonymously as usual so let's just close that once we're signed in we're going to be creating a lobby here and first we're going to try quick join a lobby so that example that i showed you right at the start that's a full-fledged lobby example which you can go look through you can actually browse the lobbies you can pick the one that you want and go into it but this flow that i'm showing you now is like a really quick fire flow so lobbies has this nice helper function here called quick join lobby so you can put in some filters here if you want i'm leaving it blank so we'll just find the first lobby available join it and then return it to us here from there we can actually grab the allocation code from there and now as you can see here we're joining the allocation and we're grabbing the join code from some lobby data and that lobby data was set so the person who created the lobby as you can see here we we've joined that we've we've created the allocation we've grabbed the join code and when we create the lobby here we are actually sending in this some data which is the join code right we're sending in the join code so that's effectively what the lobby is doing for us it is a way to join us all together to share the join code easily like that that that's probably its primary purpose obviously uh lobbies are good for creating customized games so you might be looking for a king of the hill game or you might be looking for games specifically on lan or whatever like lobby is a good way to to aggregate all these games and like filter it nicely but the primary benefit of it is adding this join code so that you can join together which is a little bit better than sending it over whatsapp right then again we're just setting our uh details to the transport just like that just like we did in the last one and then starting the game and just down here doing the same the only difference here is now that we've got a lobby on our hands the lobby will automatically shut down after 30 seconds of inactivity sorry for example if your game needs 10 people five people have joined that's obviously activity as they're joining but if now if we're just waiting for 30 seconds for someone else to join it will shut down so what the host can do is start a co-routine that will just ping the lobby every 15 seconds keep it alive and then we're just starting the host like normal starting the client like normal and now we're all in so with with just this one click here and just a little bit of cleanup on destroy we'll stop all the co routine so if we were the host we're going to stop the heartbeat if the lobby is actually not null if we're the owner of the lobby let's actually delete the lobby if it's just us then let's remove ourselves from the lobby that's good for preventing like phantom players so they've left they've left ages ago shut down the game but the players kind of like just sitting there waiting to time out so obviously you just want to get rid of the player as soon as they leave cool so let's actually check out the flow uh just press play and you can say look it said no lobbies available for quick join it's then done all those quality of service checks on all the different regions because we didn't hard code the region found the one which is uh asia south east and then created our lobby with obviously that join code so now across with our other client we can simply press play and this one will actually find the lobby automatically and join us straight to it so that is how easy and this is like a perfect flow for a mobile game right if you've got a mobile game and you just you you don't want to make any manual rooms you just want to always make sure that if there's uh five people or 50 people playing your game concurrently you just want them all constantly filling up rooms like all of them just playing together all the time without any setup right so that's what this script would be good for if you're looking for a more feature-rich lobby go check out the project that i shared at the start of this video all the code is free i drew a bunch of assets for it too so feel free to use those in your prototypes and stuff but yeah hopefully this was helpful hopefully this has shown you the full complete picture of how to create a multiplayer game with global matchmaking if you enjoyed this and want to see some more advanced tutorials on multiplayer let me know in the comments i'll be happy to do that yeah see you next time bye
Info
Channel: Tarodev
Views: 65,596
Rating: undefined out of 5
Keywords: unity lobby, unity relay, relay, matchmaking, unity multiplayer, how to make a multiplayer game, unity multiplayer tutorial, multiplayer game dev, unity networking, new unity multiplayer system, global matchmaking, online game, hwo to make an online game, unity multiplayer tutorial 2022, fishnet, mirror, unity multiplayer 2020
Id: fdkvm21Y0xE
Channel Id: undefined
Length: 12min 26sec (746 seconds)
Published: Sat Jul 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.