Unity Lobbies
This guide will help you create a Unity project using Edgegap's lobby system and Mirror as its networking solution.
We will be using a sample game from our GitHub as a base project. You will also need an account on Egdgegap to follow along.
This guide was written using Unity 2021.3.16f1 LTS
on Windows to produce all Unity builds.
Deploying the lobby system
To begin, you will need to deploy a service API to manage your lobbies by sending some requests to Edgegap's API. To do so, you will to get an API token from our dashboard.
If this is your first time using our platform, we strongly recommend following [this documentation](/docs/getting-started/first-experience) first and foremost, which will walk you through how to get an API token.
Once you have your token, we will be using Postman to send our requests. Once you have finished dowloading and setting it up, go in your Postman workspace, select the Collections
tab and create a new blank collection named Edgegap API
. Then, click on on your collection and select the Authorization
tab; This is where we will put the API token, which will be added to every request in that collection. Make sure to set it up as follows:
Type
: select theAPI Key
option;Key
: write down "authorization
";Value
: paste theAPI token
value that you obtained from the Edgegap dashboard. Make sure to keep thetoken
keyword;Add to
: select theHeader
option.
Save these settings by clicking the Save
button, or use the ctrl + s
keys.
Afterwards, right-click on your collection and click Add request
. For our first request, we want to ask Edgegap to create a new Lobby Service
, so name it accordingly. Then, set it up as follows:
URL
: write down "https://api.edgegap.com/v1/lobbies
";Request type
: From the dropdown next to the url, selectPOST
;Select the
Body
tab :Select the
raw
option;From the dropdown at the end of the radio buttons, select
JSON
;In the text box, enter the following value, changing
[LOBBY_SERVICE_NAME]
to the name you want:
When selecting a unique name for your lobby service, we recommend using a short name that contains only lowercase letters.
Save your request settings by clicking the Save
button, or use the ctrl + s
keys. Click on the Send
button, and you should get a successful 200 OK
response. Next, we will create a second request to deploy the lobby service you just created; This will make it available to use in your game. Use the following settings and save them afterwards:
URL
: write down "https://api.edgegap.com/v1/lobbies:deploy
";Request type
: From the dropdown next to the url, selectPOST
;Select the
Body
tab :Select the
raw
option;From the dropdown at the end of the radio buttons, select
JSON
;In the text box, enter the following value, changing
[LOBBY_SERVICE_NAME]
to the name of your service:
When it has successfully been deployed after sending the request, you will be able to see it on your dashboard under the Relay tab, in Relay Profiles
. It will be assigned a URL
, which we will be using in our game to send requests to. To get the URL of your new service, you can create a third request with these settings and replacing [LOBBY_SERVICE_NAME]
with the right value again:
URL
: write down "https://api.edgegap.com/v1/lobbies/[LOBBY_SERVICE_NAME]
";Request type
: From the dropdown next to the url, selectGET
;
After sending the request, the response should look as follows:
The lobby service URL links to a different API from Edgegap's. To see what requests can be sent to it, you can append /swagger/doc.json
at the end of the service's URL and open it in your web browser. For a better viewing experience, you may want to paste the raw data of that page into a tool like Swagger Editor.
Editing the project
The next steps will take place within the Unity project; Among other things, we will be writing new code to send requests to the lobby service that we just deployed, and creating a new scene in our game that will allow players to view all available lobbies, join one, or even create their own before switching over to a match with everyone in the lobby. The latter will replace the current offline scene, GamesList
.
The URL used in the following requests is the one you got after deploying the lobby service. You do not need the Edgegap API token for these either.
Open the project in the Unity Editor, then create a new scene called LobbiesList
under Assets/FPS_Demo/Scenes
. For the purposes of this guide, set up the scene to look and act as follows. Any script you may need to add can be put under a new folder placed in Assets/FPS_Demo/Scripts
.
Network Manager
Add a NetworkManager
prefab to your scene, which can be found under Assets/FPS_Demo/Prefabs
. In the MyNetworkManager
script component, set the Offline Scene
to your new scene and Online Scene
to the one named Main
. Additionally, set the HUD
to your scene's canvas.
We also set the Timeout
value of the EdgegapTransporter
script component to a higher value. This ensures that the players are able to connect to the relay after the host has done so first. However, it is not the best way to do so, and you should instead attempt to connect again after getting a Timeout warning message.
Lobby list display
A toggle that switches the displayed UI to the list of available lobbies.
Switching to it from another display will refresh the list of lobbies.
A toggle that switches the displayed UI to the creation of a new lobby.
A textInput for the player to enter their name.
A value must be set to be able to join a lobby.
In this sample project, we use the player's display name as an ID during API requests. In an actual project, you should use a value that is unique to each player instead.
A scrollView to list all available lobbies.
A specific lobby.
We display its name, current player count and maximum player capacity.
Clicking on it sends a
POST
request to/lobbies:join
with the appropriate body, then switches the displayed UI to that of a joined lobby.
A button to update the list of lobbies.
Clicking on it calls a function that sends a
GET
request to/lobbies
, then updates the list of lobbies with the response.
A space to display error messages and the likes (ie. Unable to join the lobby, etc).
A button to close the application.
Creating lobby display
In this example, we will only be setting the lobby's name and maximum player capacity, although it is possible to set other values such as a list of tags.
A textInput for the lobby's name.
A textInput and buttons for the lobby's capacity
The buttons decrease/increase the value in the input.
A validation is made to ensure the value is equal to at least 2.
A button to create the lobby.
It will not be interactable if either the player or the lobby's names are empty.
Clicking on it calls a function that sends a
POST
request to/lobbies
with the appropriate body, then switches the displayed UI to that of a joined lobby.
Joined lobby display
When switching over to this display, a GET
request to /lobbies/{lobby_id}
is immediately called.
There is no way as of yet to automatically detect when the lobby updates or when players join or leave, so this display automatically calls the API and refreshes itself every few seconds.
These toggles/buttons will show a prompt asking if the player wishes to leave the lobby while in this display.
Confirming the choice calls a function that sends a
POST
request tolobbies:leave
with the appropriate body, then switches the displayed UI to the corresponding toggle/close the application.
Another button to exit the lobby, which works the same way as the options in #1.
It will switch the displayed UI to the list of lobbies.
If the host leaves the lobby, a `DELETE` request to `lobbies/{lobby_id}` will instead need to be sent. Every other player that had joined it will then need to be disconnected by sending a `lobbies:leave` request.
The lobby's name.
The lobby's ID.
The player's name cannot be modified while in this display.
A scrollView to list all players who joined.
A specific player.
If the player is the lobby's host, an icon is displayed and the player appears at the top of the list.
A button to start the match.
Only the host may click it.
Clicking it calls a function that sends sends a
POST
request to/lobbies:start
with the appropriate body. Afterwards, certain steps need to be taken to connect the players and switch over the game scene.
At this point, your class that contains all the request functions should look like this:
Note that these functions only retrieve the information from the lobby service. Updating the HUD should be done in the function these were called from, such as clicking the `Refresh` button in the `List Display`.
You will also need to create the classes for each type of request response, based on the lobby service's API's documentation that you viewed earlier with swagger
. Those should be as follows:
Lobbies Class
Lobby Class
Annotation Class
Assignment Class
Port Class
Player Class
Starting the match
After calling the API route to start the lobby, you will need to wait for the lobby to set up its assignment, which allows the players to be connected together through a relay. It will set up a port for the server (used by the host), a port for the clients (used by the other players), an ID for the session created for the lobby, as well as an ID for every player in it. Until those are available, you will need to refresh the lobby every few seconds.
Once these have been set, you can put these values into the EdgegapTransporter
instance, then have the NetworkManager
call the right function to start the match depending on whether the player is the host or not. Your code should look like this, and can be placed in the same class as the other request functions:
The game will then switch over to the scene you set as the Online Scene
in the NetworkManager
GameObject, and everyone in the lobby will be able to play together It is possible for a player to join after the lobby has started, if the capacity allows it.
You can get the final version of this sample with all of the edits on our GitHub.
Last updated
Was this helpful?