如果游戏的主机丢失,那么游戏无法继续。主机可能会丢失,因为玩家离开,主机进程死亡或崩溃,主机的机器被关闭,或因为主机的网络连接丢失。
“主机迁移”功能允许其中一个远程客户端成为新的主机,这样多人游戏就可以继续。
在启用主机迁移的多人游戏中,同伴的地址将分配给游戏中的同伴。当主机丢失时,一个对等体可以成为新的主机。其他同伴然后连接到新的主机,游戏可以继续。
新的组件可以放入使用Unity Networking HLAPI的多人游戏中,并且允许游戏在原始主机丢失后继续使用新主机。以下是编辑器检查器中NetworkMigrationManager
的屏幕截图。它显示了当前的迁移状态。
NetworkMigrationManager
提供了一个简单的用户界面,类似于NetworkManagerHUD
。此用户界面用于测试和原型设计;一个真正的游戏将实现一个用于主机迁移的定制用户界面,并且可能定制逻辑 - 可能自动选择新主机而不需要来自用户的输入。
场景中所有联网对象上的SyncVars
和SyncLists
的状态在主机迁移期间保持不变。这也适用于对象的自定义序列化数据。
当主机丢失时,游戏中的所有玩家对象都被禁用。然后,当其他客户端重新加入新主机上的新游戏时,这些客户端的相应玩家将在主机上重新启用,并在其他客户端上重新生成。所以在主机迁移期间没有玩家状态数据丢失。
注意:只有在客户端可用的数据将在主机迁移期间保留。如果只有服务器上存在数据,则该数据将不可用于成为新主机的客户端。因此,在主机迁移后,主机上未存储在SyncVars
或SyncLists
中的任何数据都将不可用。
当客户端成为新主机时,将为所有联网对象调用回调函数OnStartServer
。
在新主机上,NetworkMigrationManager
使用函数BecomeNewHost
从当前ClientScene
中的状态构建联网服务器场景。
启用了主机迁移的游戏中的对等方由其connectionId
在服务器上标识。当客户端重新连接到游戏的新主机时,此connectionId
将传递到新主机,以便它可以将此客户端与连接到旧主机的客户端相匹配。此Id在ClientScene
上设置为“reconnectId”。
非玩家对象
具有客户端权限的非玩家对象也通过主机迁移进行处理。每个客户端所拥有的对象都被禁用,并以与玩家对象相同的方式重新启用。
当选择新主机并且同级重新连接到它时,它们提供它们的“oldConnectionId”以标识它们是哪个对等体。这允许新的主机将这个重新连接的客户端匹配到相应的玩家对象。
旧主机使用零的特殊oldConnectionId
重新连接 - 因为它没有与旧主机的连接,所以它是旧主机。有一个常量ClientScene.ReconnectIdHost
这个。
使用内置用户界面时,oldConnectionId
会自动设置。可以使用NetworkMigrationManager.Reset
或ClientScene.SetReconnectId
手动设置。
主机迁移流程
NetworkHostMigrationManager上的回调函数
// called on client after the connection to host is lost. controls whether to switch scenes
NetworkConnection conn,
out SceneChangeOption sceneChange)
// called on host after the host is lost. host MUST change scenes
protected virtual void OnServerHostShutdown()
// called on new host (server) when a client from the old host re-connects a player
NetworkConnection newConnection,
GameObject oldPlayer,
int oldConnectionId,
short playerControllerId)
// called on new host (server) when a client from the old host re-connects a player
protected virtual void OnServerReconnectPlayer(
NetworkConnection newConnection,
GameObject oldPlayer,
int oldConnectionId,
short playerControllerId,
NetworkReader extraMessageReader)
// called on new host (server) when a client from the old host re-connects a non-player object
protected virtual void OnServerReconnectObject(
GameObject oldObject,
int oldConnectionId)
// called on both host and client when the set of peers is updated
protected virtual void OnPeersUpdated(
PeerListMessage peers)
// utility function called by the default UI on client after connection to host was lost, to pick a new host.
public virtual bool FindNewHost(
out NetworkSystem.PeerInfoMessage newHostInfo,
out bool youAreNewHost)
// called when the authority of a non-player object changes
protected virtual void OnAuthorityUpdated(
GameObject go,
bool authorityState)
约束
当主机断开连接时,仅存在于服务器(主机)上的数据将会丢失。为了使游戏能够正确执行主机迁移,必须将重要数据分发给客户端,而不是秘密保存在服务器上。
这适用于直接连接游戏。需要额外的工作才能与媒人和中继服务器一起工作。
?