我正在研究如何连接到 Unity 中的光子服务器,但是当我使用强制关闭或按钮关闭光子服务器时 我试图让代码在底部运行,它与 OnLeftLobby 回调函数配合得很好,但是 OnDisconnected 回调函数中,代码无法执行并结束
public void DisconnectedPhoton()
{
PhotonNetwork.Disconnect();
}
public override void OnDisconnected(DisconnectCause cause)
{
pv.RPC("LeftPhoton", RpcTarget.All, myPlayer.ViewID);
}
使用
OnPlayerLeftRoom
功能,其中包括离开的玩家。每当玩家离开房间(断开连接)或变得不活动(如果您的游戏支持)时,都会调用此函数。当连接的玩家收到此回调时,让他们清理属于断开连接的玩家的本地对象。
// This function is called on all connected clients when a player disconnects or becomes inactive
//
void OnPlayerLeftRoom(Player otherPlayer)
{
if (otherPlayer.IsInactive)
{
// Player is inactive, allow them to rejoin or handle some other way
}
else
{
// Clean up the disconnected players objects
}
}
注意:如果玩家不是处于非活动状态,则在回调发生时,离开的玩家将不会出现在 Room.Players 字典中。