Description: This passage is mainly about the lobby login. In this passage, the writer will demonstrate how to setup the Network Lobby System that can sit on top of any multiplay game that he has created.
Welcome to another unity 5 tutorial, in this tutorial I’m going to show you how to add a lobby system to your networked game, so in this game here I’ve got it, it’s the smooth camera follow version of the multiplayer car that we have created in the last tutorial to add a lobby system over the top, it is easier than what you might think, because unity supplies all of the code that you need.
So if we go into the asset store, what you need to look for and add to your project is network lobby and it’s free, and you need to import that in, now when you import it in the assets, you will get a lobby folder which has all the bits and pieces that you need for adding the lobby, so let’s get started.
First of all, we need a new scene, so I’m going to go to file a new scene, and we’re going to file save that scene and call it lobby, I always call it log e, I don’t know why, so there’s our scene, now enter, this scene will go into the prefabs folder of our Lobby that we’ve downloaded in there, you’ll find a lobby manager, you want to grab that Lobby manager and drag and drop it into the hierarchy, and that will bring up this interface.
Now if I go into the 2d view here, so you can see that this is the lobby created with the new Yui system, so you’ve got all your buttons there, now you can move these around as you like, so if you want that button over here, you move it, this is not a tutorial on the Yui system, but it’s elementary to change what these all look like by selecting them in the hierarchy.
Then go through and find where the images are attached, and the colors that are set to them, so for example if we want to change that top panel which is this bit here to a different color, then we will select it and go over and change it to right, let’s make it bright purple, and then you can select all of these other buttons and change their images as well.
So now we have this Lobby system, now the lobby itself is a game object, and it doesn’t destroy onload component here, which means it will get copied across to the next scene that’s loaded, so we want that on, then we can have our normal game that I’ve already created using this lobby to make the lobby work.
If you have a look what’s in there, there’s a canvas, but there’s no event system and you need an event system in your scene for the canvas objects to register clicks and changes, so with that Lobby selected, let’s click go to Yui, and we go down to a vent system and add that.
So the event system is now attached to Lobby, and it should go wherever that Lobby goes now to make the lobby system work, it’s a matter of setting a few of the exposed variable values, it’s in this lobby manager script, that’s attached to the lobby manager object that we’ve added.
The first thing that we need to add is the lobby scene and the play scene which are here, now this is the lobby scene that we’re working in, and the play scene is the one in which I’ve got the car, so it’s our main one that we had before.
Now those are two scene files, if we click on assets in the project, we’ll be able to find them, we make this a bit bigger, so here’s the lobby and here’s our main game, now before you can add those into here, those two scenes need to be in your build settings, so get to file all build settings, and as you can see there’s nothing here at the moment.
We need to add the lobby scene, so drag and drop that into there, the lobby scene needs to be the one on the top of all of these, because whatever at the top will be the one that runs first when it’s built out, so they’re the only two scenes that we need and that’s all we need to do for now there.
So shut that OK, now we grab the lobby scene, and drag and drop it over onto the lobby scene spot in the lobby manager, and then put main and drag and drop it on the play scene there, now underneath those two scenes, there’s a show lobby GUI tick box, if you tick that on when it runs, you will get the old GUI system showing up over the top of your new Yui system which you don’t want.
If you don’t have this Yui system, then you might want to use that old one, but it’s already there, so we’re going to use it, now you’ve got some values here to set up and play with, first of all you’ve got a maximum number of players that your game will allow, or that can be in your lobby, I’m going to set that to four, because I’m going to have four players, because that will be impractical to show you on the screen.
Then I’ve got the maximum players per connection which I’ll set that down to one, and I’ve got the minimum number of players, this is the number of players that the lobby will wait for to arrive at your game before the game starts to run, so we’re going to set that to two.
Now the lobby also needs a player prefab, and this has got all the player info in it, now you’ll see that that’s already populated with a c-sharp code, which is a prefab in the lobby system, so here we are going to lobby and prefab and you’ll find that.
This is player info here, if this is missing this player info, then you want to drag and drop this player prefab over onto there, now we’ll see the effects of this player info in a moment, so it’s storing the name of the player and the color, but the player chooses to display.
Then underneath that, there’s a game player prefab which has currently got nothing in there, it has an empty object, this is the player prefab that we’ve been using in our game, now our player prefab is that little orange car, so if we go back up to the main folder up the top of assets, we can see our little orange car, that is our player character to check that, that is the networked one.
If you select it, have a look at the inspector, and it will have the network transform script, and the network identity script as well as the other scripts that we have created for it, so that is the actual prefab that we require, so select lobby again, and we’re going to drag this player prefab onto this spot here.
Now if that little prefab doesn’t have the correct components attached to it such as the network ID, and it won’t accept it into that spot, now before we move on to test all of this, I want to show you one more thing in this lobby manager script, which is the pre-match count town where it’s got three as a default.
So once two players join that lobby and decide to play the game, you’ll get up on the screen a countdown, so it’ll go to match in three seconds, so you can change that value, then that’s where you will know that, that value comes from when you see it in a moment, so that’s all that we have to do to set up the lobby, it isn’t easy.
The next step is to build this out, so to do that, we go file, let me go to build settings, and then you can set the player settings if you desire, so over here I’m going to change the company name wholistic 3d, and I’ve got a product name of network cars that will do, and then we want to press build, and we’re going to build this, file out.
I’m going to save it to my desktop, and then I’ll make another few copies of the application that gets built out, so I can run it multiple times on the machine, so I’ve made a build of this onto my desktop, and I’ve copied that application file two times so that I’ve got three executable versions of that file.
Now on a Mac you have to do that if you want to run it three times, I’m not sure about Windows, but I think it’s probably the same case, you might have to do that, because if you double-click on an application icon, and it’s already running that, it will create another version of it, so I’ve got these running in the background.
Here are my copies, first let’s make this the server, so we’re working down here, in this manual connection area I’ll address this in the next tutorial, but first let’s get working on our local machine, so we’re going to create a dedicated server on this machine, you can also have this instance as a client and the server which makes no difference, but I want to show you a dedicated server.
If you click dedicated server, it will create it on local host, local host is the local server on your machine, it’s the local server on everyone’s machine, when you connect to local host or the IP address 127.0.0.1, you’re connecting to the server that the application is running on the same machine, that is no one else’s machine.
If you have this server here running on a different machine in your network, and you want to connect to it, then you’re going to have to put the IP address of that machine in here, because these are all running on the same computer, we can use 127.0.0.1, so this first one is to go to join that game, and you’ll see that we get this player.
Now you can change the color here, and you can change your player name up here, these are what are contained in that player info prefab that I have talked about, now currently whatever you set in, it is not linked to our car game, and I’ll do that in a tutorial a little bit down the track but not here, I want to show you how this works, so this is player 1.
Now when you’re ready to join, you can hit join and that makes you ready, now let’s go over to this one, let’s join now, you can see who’s in that lobby, there are two people, you’re in this case ready, but the other person’s still waiting, so this person is here, and I can change the colors, and you can see that they’re nicely synced across the network.
Then as soon as I press join, because we only need two people to be in the lobby for the game to start running, you’ll see that countdown happen, so here it is and now the game loads, now we’ve got in our car game that both players end up spawning exactly the same place, so that’s why you get a bit of jittering here.
So you can add some extra codes into your actual game itself, people don’t spawn in the same place, but it’s not a huge problem, now what you will see on the server is that you’re getting a replication of what is going on in these game worlds which is nice, and I can move these around.
So as I have said before, this is exactly the same little demonstration that we have created beforehand, and I’ve done nothing to it, all we’ve done is that we have created that Lobby scene now the lobby itself, remember that I have said that it doesn’t destroy on load, so that means that it’s still here, and it’s not shown at the moment.
But if you hit the Escape key, it will bring up your Lobby, so you can get out of the game, and go back to the lobby if you wish, so let me show you where that is for the escape key before we finish up with this tutorial.
So I’m going to shut these down quickly, if I flick back to unity, and in unity you can also run another instance from here, but let’s have a quick look at this top panel which seems to disappear when the game is running.
If we open up that code and have a look down in here, you’ll see that it gives you a hint, if you press the Escape key, it will toggle the visibility, so when I first run this, what happens to my header and where’s my back button, and by having a look through this code, this is the code that comes with it, I am able to determine that it’s obviously the escape key.
So don’t be afraid to dig through the code and see what’s attached to, if you’re in doubt of what’s going on, because that’s the only way that you’re going to figure out how this is working, so that’s it for this tutorial, and that’s how you set up the lobby for a local connection and on local networks and that type of thing.
In the next part of this tutorial series, I’m going to show you how to use the unity server, the cloud-based servers to add your game into there and play a game from there, so I hope that i helpful for getting the lobby manager up and working in your own games, or even on top of the sample orange car multiplayer game that I’ve created, and I’ll talk to you in the next tutorial.