Serverless Multiplayer Game
Build a multiplayer game with a game lobby, without using a server to host your game.
Read time 8 minutesLast updated 17 hours ago
Multiplayer games are a popular game mechanic that allow multiple players to interact and play games together. This sample demonstrates how to create or join a Lobby Service game lobby and compete in a simple arena-style game where players collect coins for points.

Prerequisites
To use this sample use case, you must download and install the UGS Use Cases project in your Unity project.Overview
During the course of the sample, players can perform the following actions:- Randomize their names.
- Create public and private lobbies for others to join.
- Join lobbies either from a list of public lobbies or by entering a secret key to join private ones.
- View other players in the lobby.
- Signal they are ready to start the game.
- Play a multiplayer game using Relay Service and Netcode for GameObjects (NGO) to facilitate player movement and collect coins for points.
- View information such as winners, scores, and personal high stats at the end of the game.
- In the Unity Editor Project window, select Assets > Use Case Samples > Serverless Multiplayer Game, then double-click to open the sample scene.
ServerlessMultiplayerGameSample.unity - Press Play to enter Play mode.
Initialization
When the scene loads, the following initialization steps occur:- Sample-related managers are set to DontDestroyOnLoad so they can remain intact in all scenes of the sample.
- The Profanity Manager is initialized, causing all valid player names and lobby words to be propagated to the allow list so only permitted words will be presented to the player.
- UGS is initialized and the player signed in with the correct profile name.
- Remote Config is read to configure coin spawning, game duration, etc.
- The main menu is shown so the player can choose to host or join a game, randomize his/her name, change UGS profile name to use, etc.
Functionality
Set up computer to test multiplayer
You must be able to play multiple instances of this sample to test this use case. The following are some techniques to facilitate multiplayer testing:- Clone the repository again (or copy the project folder) to another folder. You can then open both/all instances in the Unity Editor simultaneously and enter Play Mode in each instance to test the sample.
- Use ParrelSync to clone the project. This is the preferred method because changes made to the base project automatically propagate to all clones for testing. Note that this is not a Unity product so we recommend reviewing the documentation and license agreement before using.
Profile IDProfile-1Scenes
This use case contains multiple scenes to divide the sample into smaller pieces, allowing developers to only review the implementation they're interested in learning.Note
Always begin Play Mode with theServerlessMultiplayerGameSampleServerlessMultiplayerGameSample scene
The sample begins with theServerlessMultiplayerGameSampleLobbyLobbyLobby scene
Once a lobby has been created or joined on theServerlessMultiplayerGameSampleLobbyGameServerlessMultiplayerGameSampleGame scene
After a minimum of two players have joined the lobby and all players have signaled ready, theGameServerlessMultiplayerGameSampleServerlessMultiplayerGameSampleNotable manager classes
This use case has a few notable manager classes that facilitate different aspects of the sample. TheProfanityManagerGameManagerProfanity Manager
This sample implements a Profanity Manager to ensure that the player and lobby names created by one player, but visible to all players, do not include any offensive language. TheProfanityManagerProfanityManager Serverless Multiplayer Game/Scripts/Menu/ProfanityManager.csServerless Multiplayer Game/Scripts/Menu/PlayerNameManager.csServerless Multiplayer Game/Scripts/Menu/LobbyNameManager.csProfanityManager.IsValidPlayerNameProfanityManager.IsValidLobbyNameGame Network Manager
Being a multiplayer game, this sample requires a Game Network Manager to facilitate game communication between players. The Game Network Manager is instantiated on the host when theGameGameSceneManagerStart()- Starting the host/client
- Creating player avatars for all players
- Coordinating countdowns and game timers
- Allowing players to move to collect coins once countdown is complete
- Communicating with other managers when needed, such as when scoring
- Coordinating game over by propagating the final score and waiting for all players to acknowledge receipt
- Issuing request that all players return to main menu
NetworkObject.SpawnWithOwnershipGameNetworkManagerServerless Multiplayer Game/Scripts/Game/GameNetworkManager.csSetup
Requirements
To replicate this use case, you'll need the following Unity packages in your project:Package | Role |
|---|---|
| Authentication | Automatically sign in the user anonymously to keep track of their data on the server side. This sample also uses the Authentication service to switch profiles to simulate different players to facilitate multiplayer testing. |
| Cloud Save | Stores the player's name, win/games counts and high scores. |
| Deployment | Provides a cohesive interface to deploy assets for Cloud Services. |
| Lobby | Handles creating or joining lobbies so players can gather to start a game. The lobby also stores each player's ready flag, as well as the Relay Join Code, which is needed to join the Relay Service allocation to facilitate real-time gameplay. |
| Netcode for GameObjects | Spawn network objects such as player avatars and coins, synchronize movement and despawn network objects as needed. |
| Relay | Creates/joins a multiplayer allocation to facilitate real-time gameplay with the creator of the lobby acting as relay service host. |
| Remote Config | Maintain game settings such as coin spawning frequency, coin wave sizes, game duration, etc. |
Cloud Save
This sample uses Cloud Save to store each player's lifetime stats, which include name, win count, total game count and high scores. Note that using different profiles results in each having separate UGS data, so each player sees different stats. The following is an example of a player's Cloud Save data:{ "playerName": "Erika", "highScores": [33, 27, 17], "winCount": 14, "gameCount": 15}
Unity Cloud services configuration
To replicate this sample scene's setup in your own Unity project, configure the following items:- Remote Config values
Using the Deployment package
To deploy configurations using the Deployment package:- Open the Deployment window.
- Check in .
Serverless Multiplayer Game - Click .
Deploy Selection
Using the Unity Dashboard
You can use the Unity Dashboard to manually configure your services by project and environment. Refer to the following sections to configure this sample.Remote Config
Set up the following config values in the Unity Dashboard:Key | Type | Description | Value |
|---|---|---|---|
| JSON | List of game settings for all possible games, based on the number of players in the game. |
|
Field | Description |
|---|---|
| Number of players in specified configuration. Valid values are 2, 3 and 4 and there must be 1 of each to specify game configuration for each count of players. |
| Length of game in seconds. All games currently last 60 seconds, but these values could be changed for longer games with more players. |
| How long to wait to spawn first set of coins. This permits players to get their bearings before the initial wave of coins appears. |
| How long do coins remain in play before despawning. This adds urgency because coins can disappear, requiring players to hurry to capture them in time. |
| How long to wait after a wave of coins is spawned before spawning the next wave. Coins spawn in waves so players have multiple options of coins to attempt to collect. |
| How many clusters of one coin (that is, individual coins) should be spawned in each wave. There is usually one cluster of one, so a player with no better option can at least try for the single coin. |
| How many clusters of two coins (that is, pairs of coins) should be spawned in each wave. There is usually at least one cluster of two coins so one of the players can have an opportunity to score two points while the other player only gets one. |
| How many clusters of three coins to spawn per wave. Two-player games don't need clusters of three, but games with more players can add three coin clusters. |