When building multiplayer games, setting up a robust system for player authentication and lobby creation is crucial. Unity provides a suite of services designed to aid developers in implementing these functionalities, namely the Unity Authentication Service (UAS) and the Lobby service. This guide will walk you through the process of authenticating players and creating a game lobby in Unity.
Player Authentication with Unity Authentication Service
To authenticate to the Lobby service, you need to identify players uniquely. Unity recommends using its Authentication Service, which provides a player ID and an access token.
One way to achieve this is through anonymous sign-in, which allows a player to start interacting with your game without requiring any input from them. The SignInAnonymouslyAsync() method facilitates this. If the session token is cached on the SDK, the method recovers the existing player's credentials; otherwise, it creates a new anonymous player.
async Task SignInAnonymouslyAsync()
{
try
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
Debug.Log("Sign in anonymously succeeded!");
// Shows how to get the playerID
Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
Please note that if a player uninstalls and then reinstalls the game, the anonymous account is not recoverable unless it's linked to a platform-specific account.
Creating a Lobby with Unity Lobby Service
When a player creates a new lobby, they become the host. The Lobby service allows hosts to set several properties for their lobbies, including lobby name, visibility, size, password, initial custom lobby data, and initial player data.
You can create both public and private lobbies. Public lobbies do not require a lobby code to join and are visible in query results for anyone to join. On the other hand, private lobbies are not visible in query results and require the lobby code or ID to be manually provided to new players.
Here is an example of creating a public lobby:
string lobbyName = "new lobby";
int maxPlayers = 4;
CreateLobbyOptions options = new CreateLobbyOptions();
options.IsPrivate = false;
Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
And here is how to create a private lobby:
string lobbyName = "new lobby";
int maxPlayers = 4;
CreateLobbyOptions options = new CreateLobbyOptions();
options.IsPrivate = true;
Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
You can also add lobby data during lobby creation. Lobby data can be anything like map IDs, game modes, or other game-related information. The Lobby service allows you to create lobby data with indexed string or numeric values, facilitating filtering and ordering of lobbies based on these indexes in queries.
Here is an example of creating a lobby with player data for the host:
string lobbyName = "new lobby";
int maxPlayers = 4;
CreateLobbyOptions options = new CreateLobbyOptions();
// Ensure you sign-in before calling Authentication Instance.
// See IAuthenticationService interface.
options.Player = new Player(
id: AuthenticationService.Instance.PlayerId,
data: new Dictionary<string, PlayerDataObject>()
{
{
"ExampleMemberPlayerData", new PlayerDataObject(
visibility: PlayerDataObject.VisibilityOptions.Member, // Visible only to members of the lobby.
value: "ExampleMemberPlayerData")
}
});
Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
Managing Active and Inactive Lobbies
Lobbies become inactive if they haven't been updated or sent a heartbeat request in the past 30 seconds. You can configure this timeout period. Inactive public lobbies don't appear in query results, and both public and private inactive lobbies are automatically deleted. They can be reactivated by updating them or sending a heartbeat request.
Remember, there is a rate limit of 5 heartbeat requests per 30 seconds, capping users at hosting 5 lobbies at a time.
This guide provides a foundational understanding of player authentication and lobby creation in Unity. Remember, the specific implementation may vary based on your game's requirements. Always refer to Unity's documentation for a comprehensive understanding.