Parties
Note: Parties uses the Unity Lobby service to group players together. As a result, the Lobby service is a requirement of Parties.
Parties shows how to create a party experience in your game. A party is a group of players, formed by players, that persists for a game session (from game launch to game exit). A party persists across multiple game # states, such as lobby, matchmaking, and gameplay. Unlike a friends list (which is a permanent grouping of players), a party only lasts for the duration of a single game session.
Before continuing, download and install the Unity Hub and a supported version of the Unity Editor. Parties supports the following Unity Editor versions:
- Unity 2020.3 LTS or later
Requirements
Before getting started with Parties, you must meet the following requirements:
- Install the Unity Lobby SDK.
- Your Unity project is onboarded to the Lobby Service.
- TextMesh Pro essentials.
- In the Editor, go to Window > Textmesh Pro > Import TMP Essential Resources.
High-level workflow
A typical party member’s journey using Parties is as follows:
- Player A starts a Party
- Player A sends an invite code to Player B
- Player B Joins Party using the code
- Player A and B are both in a Party, and they start Match 1
- Match 1 ends, and the Party still exists so that Players A and B can play together again in Match 2
Concepts
Parties
A party is a group of players, formed by players, that persists for a game session (from game launch to game exit).
Party leader
A party leader is the host of a party. They manage the party and can add and kick out players.
Party member
A party member is the player who is part of the party. Party members can add other players by sending an invite code.
Manage parties
Use the following Lobby code samples from the LobbyManager to carry out Party operations:
Create a party
async Task<(bool success, Lobby lobby)> TryCreateLobbyAsync()
{
try
{
var currentPlayer = new Unity.Services.Lobbies.Models.Player(AuthenticationService.Instance.PlayerId);
var partyLobbyOptions = new CreateLobbyOptions()
{
IsPrivate = true,
Player = currentPlayer,
};
Lobby lobby = await LobbyService.Instance.CreateLobbyAsync("new lobby",
maxPlayers: 4,
partyLobbyOptions);
//... The party has been created and joined
return (true, lobby);
}
catch (LobbyServiceException e)
{
Debug.LogException(e);
return (false, null);
}
}
Assign another player as the party lead (as the party lead)
async Task<(bool success, Lobby lobby)> TrySetHostAsync(string newHostPlayerId, Lobby lobby)
{
// Only the host can modify the host
if (lobby.HostId != AuthenticationService.Instance.PlayerId)
return (false, lobby);
try
{
var setHostOptions = new UpdateLobbyOptions()
{
HostId = newHostPlayerId
};
return (true, await LobbyService.Instance.UpdateLobbyAsync(lobby.Id, setHostOptions));
}
catch (LobbyServiceException e)
{
Debug.LogException(e);
return (false, lobby);
}
}
Invite players to / Join a party
You need to create a method for sending invites to players. However, in lieu of the ability to send invites to players, players can share the LobbyCode via text communication.
async Task<(bool success, Lobby lobby)> TryJoinLobbyAsync(string joinCode)
{
try
{
var currentPlayer = new Unity.Services.Lobbies.Models.Player(AuthenticationService.Instance.PlayerId);
var joinOptions = new JoinLobbyByCodeOptions()
{
Player = currentPlayer,
};
return (true, await LobbyService.Instance.JoinLobbyByCodeAsync(joinCode, joinOptions));
}
catch (LobbyServiceException e)
{
Debug.LogException(e);
return (false, null);
}
}
Leave a party
async Task<bool> TryLeaveLobbyAsync(Lobby lobby)
{
try
{
return await TryRemoveFromParty(AuthenticationService.Instance.PlayerId, lobby);
}
catch (LobbyServiceException e)
{
Debug.LogException(e);
return false;
}
}
Remove a player from a party (as the party lead)
async Task<bool> TryRemoveFromPartyAsync(string playerID, Lobby lobby)
{
try
{
await LobbyService.Instance.RemovePlayerAsync(lobby.Id, playerID);
return true;
}
catch (LobbyServiceException e)
{
Debug.LogException(e);
return false;
}
}
Visit the Support page to learn how to contact the Unity Lobby support team.