Game server hosting support
Integrate different hosting solutions using Cloud Code modules to manage servers.
阅读时间3 分钟最后更新于 8 天前
Multiplayer Services are compatible with many game server hosting providers to provide advanced multiplayer features. In combination with Cloud Code modules, you can allocate servers, create multiplayer sessions, and enable player backfilling.
Server allocation with a hosting provider
The easiest way to allocate a server on your preferred hosting provider is to use and configure it through Unity Matchmaker, which automatically allocates the game server. Unity's Multiplayer Services package helps with configuring the required hooks for the hosting provider you choose. The following code sample demonstrates how to start a multiplayer session on a server allocated by an hosting provider:The way the hosting provider's payload carries the#if UNITY_SERVER// The setup logic can be called when the hosting provider signals that the server is readypublic async Task SetupServer() { try { // Enter logic for retrieving the match id from the hosting provider sdk and any additional setup var matchId = "MATCH_ID"; // Initialize unity services await UnityServices.InitializeAsync(); // Sign in with a service account - Ensure this information gets stripped from client builds await ServerAuthenticationService.Instance.SignInWithServiceAccountAsync("KEY", "SECRET"); // Adapt session options to your game needs var sessionOptions = new SessionOptions() { MaxPlayers = 2 }; // Create the match session IServerSession session = await MultiplayerServerService.Instance.CreateMatchSessionAsync(matchId, sessionOptions); // Hook to any relevant session lifecycle events and integrate hosting provider signals and application quit accordingly session.Deleted += () => Application.Quit(); // Add optional logic for the hosting provider player readiness } catch (Exception e) { Debug.LogException(e); Application.Quit(); } }#endif
matchId
This code automatically creates a session for players to connect to when the server becomes allocated. Call this code as soon as the server is ready to receive players.
Authentication
You must authenticate the server with a service account usingServerAuthenticationService.Instance.SignInWithServiceAccountAsyncBackfilling
If Matchmaker allocated the server, it's possible to automatically request new players to join to backfill empty slots. These empty slots could be due to players leaving the session or because Matchmaker quickly created the match to minimize matchmaking time:This code can start the backfilling process as soon as the session is created. Additionally, this code automatically starts the backfilling process when the session has one more empty slot. Backfilling requires having the "Matchmaker Backfill Admin" permission on your service account. Refer to Matchmaker Backfill Admin for the required Matchmaker backfill API permission.// Adapt session options to your game needs var sessionOptions = new SessionOptions(){ MaxPlayers = 2};// Add backfilling configuration if you want the session to automatically set up backfill when a player leaves.// This requires having the "Matchmaker Backfill Admin" permission on your service account.sessionOptions.WithBackfillingConfiguration( enable: true, automaticallyRemovePlayers: true, autoStart: true, playerConnectionTimeout: 30, backfillingLoopInterval: 1);
It's also possible to manually start and stop the backfilling process using the following code:
Starting and stopping the backfilling of a session can be useful if certain times aren't appropriate for players to be joining (for example, if the session is about to end, or if a cinematic is in progress). Refer to WithBackfillingConfiguration() for more information on the backfilling configuration.IServerSession session = await MultiplayerServerService.Instance.CreateMatchSessionAsync(matchId, sessionOptions);// Start the backfilling if it is not already in progressawait session.StartBackfillingAsync();/// Stop the backfillingawait session.StopBackfillingAsync();