游戏服务器托管支持
多人游戏服务会话与 游戏服务器托管(Multiplay) 兼容。多人游戏服务包为游戏服务器托管的 SQP 查询协议 提供开箱即用的支持。
在游戏服务器托管上分配服务器的最简单方法是使用并通过 Unity 匹配器 配置它,它会自动分配游戏服务器。Unity 的多人游戏服务包有助于配置游戏服务器托管中所需的挂钩。
以下代码演示了如何在服务器上启动会话
var sessionManagerOptions = new MultiplaySessionManagerOptions()
{
SessionOptions = new SessionOptions()
{
MaxPlayers = 10
}
};
var sessionManager = await MultiplayerServerService.Instance.StartMultiplaySessionManagerAsync(sessionManagerOptions);
var session = sessionManager.Session;
Note: When using Matchmaker, even though the maximum number of players in a match is defined on the configuration, it is necessary to set it here, as well. Make sure that the maximum number of players defined here is at least as large as the one defined on the Matchmaker.
此代码会自动为玩家创建一个会话,以便在服务器分配后连接。
参考
- StartMultiplaySessionManagerAsync 了解有关服务器配置的更多信息。
- IMultiplaySessionManager 了解有关
MultiplaySessionManager
功能的更多信息。
身份验证
请参阅游戏服务器文档 此处。
服务器就绪
如果在构建配置中启用了游戏服务器托管的 服务器就绪 功能,则可以使用以下代码将服务器设置为准备接收玩家连接
await sessionManager.SetPlayerReadinessAsync(true);
当使用匹配器时,如果启用了该功能,则只有在服务器就绪后才会将玩家分配到服务器。
回填
如果服务器是使用匹配器分配的,则可以自动请求新玩家加入,以 回填 由于玩家离开会话或匹配是急切创建以最大程度地减少匹配时间而导致的空闲插槽
var sessionManagerOptions = new MultiplaySessionManagerOptions()
{
SessionOptions = new SessionOptions()
{
MaxPlayers = 10
}.WithBackfillingConfiguration(enable: true, autoStart: true)
};
此代码会在游戏服务器托管分配服务器后立即启动回填过程(如果会话未满)。此外,此代码会在会话存在一个或多个空闲插槽时自动启动回填过程。
Note: If MaxPlayers
defined here is not equal to the one in the matchmaking configuration, backfilling does not work as expected.
也可以使用以下代码手动启动和停止回填过程
var sessionManager = await MultiplayerServerService.Instance. StartMultiplaySessionManagerAsync(sessionManagerOptions);
await sessionManager.Session.StartBackfillingAsync(); // Start the backfilling if it is not already in progress
await sessionManager.Session.StopBackfillingAsync(); /// Stop the backfilling
启动和停止会话的回填在某些时间段内对于玩家来说可能不合适(例如,如果会话即将结束,或者正在播放过场动画)。
参考 WithBackfillingConfiguration() 了解有关回填配置的更多信息。