Photon Unity Networking(PUN)试图跟踪每个团队的玩家

时间:2015-11-18 22:58:56

标签: unity3d photon

我想要发生的事情是当玩家连接到他们挑选红色或蓝色球队(1或2)的房间时 每支球队的最大球员数为5,我通过使用

等变量跟踪球员
int currentRedPlayers = 0;
int maxRedPlayers = 5;
int currentBluePlayers = 0;
int MaxBluePlayers = 5;

我在通过网络同步时遇到了很多麻烦。 目前我正在做的是当你加入Room时,你使用PhotonNetwork.Instantiate为自己生成一个GameManagerObject。 假设您是第一个加入服务器的人,那么变量currentRedPlayers和currentBluePlayers将被设置为0.然后当您选择一个团队时(假设您选择了红队) currentRedPlayers将跳转到1,因此4个变量将如下所示。

currentRedPlayers = 1;
maxRedPlayers = 5;
currentBluePlayers = 0;
MaxBluePlayers = 5;

这有效。

现在说第二名球员加入,他们选择蓝队,他们的变量将是

currentRedPlayers = 0;
maxRedPlayers = 5;
currentBluePlayers = 1;
maxBluePlayers = 5;

我试图让两个客户更新彼此GameManagerObject的方法是添加一个光子视图并让它观察对象的脚本" GameManager" 这是剧本:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour
{
    public int currentRedPlayers = 0;
    public int maxRedPlayers = 5;
    public int currentBluePlayers = 0;
    public int maxBluePlayers = 5;

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting)
        {
            //player sends info to network data
            stream.SendNext(currentRedPlayers);
            stream.SendNext(currentBluePlayers);
        }
        else
        {
            //player recieves info from network data (isReading)
            currentRedPlayers = (int)stream.ReceiveNext();
            currentBluePlayers = (int)stream.ReceiveNext();
        }
    }
}

这应该更新每个客户端的GameManagerObject,以便每个人都将变量读取为

currentredPlayers = 1;
maxRedplayers = 5;
currentBluePlayers = 1;
maxBluePlayers = 5;

但是它只会显示自己的,如果我在Unity编辑器中运行客户端1而客户端2作为构建和运行运行,我可以在层次结构中的客户端1中看到它显示为每个玩家实例化GameManagerObjects,并且它显示了正确的值

int currentRedPlayers = 1;
int maxRedPlayers = 5;
int currentBluePlayers = 0;
int MaxBluePlayers = 5;

currentRedPlayers = 0;
maxRedPlayers = 5;
currentBluePlayers = 1;
maxBluePlayers = 5;

但他们不会互相更新。 (这就是我希望它看起来像)

int currentRedPlayers = 1;
int maxRedPlayers = 5;
int currentBluePlayers = 1;
int MaxBluePlayers = 5;

对不起,如果这是一个新手问题,但我非常喜欢PUN,我花了无数个小时试图找到不同的方法来解决这个问题,但这是关于尽可能接近,我已经撞墙了。任何人都可以告诉我如何让每个客户的对象互相更新,以便他们能够以正确的方式读取房间中的当前玩家?

1 个答案:

答案 0 :(得分:1)

您现在可能已找到答案,但对于其他任何人,只需使用customproperties。

using System.Linq;

public void team()
{
     int redteam = 0, blueteam = 0; 
     PhotonNetwork.playerList.ForEach(x => { if (x.customproperties[teamproperty] == "red") redteam++; else if (x.customproperties[teamproperty] == "blue") bluetime++; });
}