Unity Multiplayer SDK
The Multiplayer SDK is a Unity package that provides a set of APIs and tools to build multiplayer games and applications. It consolidates some other SDK offerings, specifically:
In addition to being able to use the consolidated SDKs above as normal with their usual dependencies (ie: UTP for Relay), the Multiplayer SDK also provides new high-level APIs that provide service integrations. This version includes the following integrations:
- matchmaking into peer-hosted or dedicated game server matches
- configured via Matchmaker queues
- joining and leaving matches
Using the Multiplayer SDK
The Multiplayer SDK depends on the Operate Core SDK.
To use the SDK you must initialize the Operate Authentication SDK.
Import packages
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Multiplayer;
Initialize the SDK
try
{
await UnityServices.InitializeAsync();
// You can also sign-in using a different method.
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
catch (Exception e)
{
Debug.Log(e);
}
Matchmaker
Create the Match Handler
In order to start matchmaking, you must first create a MatchHandler instance. Supply the MatchHandler instance with the MatchOptions that you want to use for matchmaking.
// See the MatchOptions class for more options.
var matchOptions = new MatchOptions
{
// Specify the queue name you've configured via Matchmaker.
QueueName = "quque_name",
// The maximum number of players that can join the match.
MaxPlayers = 2,
// Optional. Pool attributes passed to the Matchmaking service.
TicketAttributes = new Dictionary<string, object>()
// Optional. A hook that is called before the host starts on peer-to-peer. Use this to set up logic specific to your game.
PreHostStartHook = PreHostStartHook
};
// Create the match handler.
var matchHandler = MultiplayerService.Instance.CreateMatchHandler(matchOptions);
Start Matchmaking + Join Match
The MatchHandler instance is used to start matchmaking, join a match, and leave a match. In order to Join a Match, a match must first be found via StartMatchmaking.
try
{
// Creates the Matchmaking request, and waits for a match to be found.
await matchHandler.StartMatchmaking();
// Joins the match.
await matchHandler.JoinMatch();
}
catch (Exception e)
{
Debug.Log(e);
}
Leaving a match / Cancel Matchmaking
Additional MatchHandler methods are available to leave a match or cancel matchmaking.
// Leaves the current match.
await matchHandler.LeaveMatch();
// Cancels the Matchmaking request.
await matchHandler.CancelMatchmaking();