コード例 #1
0
    public override void OnPhotonPlayerDisconnected(PhotonPlayer otherPlayer)
    {
        Util.Log($"Player {otherPlayer.ID} disconnected.");

        var cms = FindObjectOfType <HudNotifications>();

        if (cms != null)
        {
            cms.AddMessage($"'{otherPlayer.NickName}' left");
        }

        if (!PhotonNetwork.isMasterClient)
        {
            return;
        }

        // Inefficient and global, but whatever...
        foreach (UserBody body in FindObjectsOfType <UserBody>())
        {
            if (body.GetComponent <PhotonView>().OwnerActorNr == otherPlayer.ID)
            {
                PhotonNetwork.Destroy(body.gameObject);
            }
        }

        // Reclaim all actors owned by this player. A bit surprised photon doesn't
        // do this for us..
        foreach (VoosActor actor in voosEngine.EnumerateActors())
        {
            if (actor.reliablePhotonView != null && actor.reliablePhotonView.OwnerActorNr == otherPlayer.ID)
            {
                // Why not call actor.RequestOwnership here? That would result in
                // nothing happening, because the other player is definitely gone. But
                // calling view.RequestOwnership() directly will go through the normal
                // Photon flow, bypassing all our logic, and Photon should just grant us
                // ownership.
                actor.reliablePhotonView.RequestOwnership();
            }
        }

        // Broadcast a message saying the player disconnected.
        PlayerLeftMessage playerLeftMessage = new PlayerLeftMessage();

        playerLeftMessage.id       = otherPlayer.ID;
        playerLeftMessage.nickName = otherPlayer.NickName;
        voosEngine.BroadcastMessage("PlayerLeft", playerLeftMessage);
    }