Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 光子联合网络(PUN)试图跟踪每支球队的球员_Unity3d_Photon - Fatal编程技术网

Unity3d 光子联合网络(PUN)试图跟踪每支球队的球员

Unity3d 光子联合网络(PUN)试图跟踪每支球队的球员,unity3d,photon,Unity3d,Photon,我想要发生的是当球员连接到房间时,他们选择红色或蓝色(1或2) 每个队的最大球员数是5名,我通过使用变量(如 int currentRedPlayers = 0; int maxRedPlayers = 5; int currentBluePlayers = 0; int MaxBluePlayers = 5; 我在通过网络同步这件事上遇到了很多麻烦。。 目前,我正在做的是,当您加入房间时,您可以使用PhotonNetwork.Instantiate为自己生成一个GameManagerObje

我想要发生的是当球员连接到房间时,他们选择红色或蓝色(1或2) 每个队的最大球员数是5名,我通过使用变量(如

int currentRedPlayers = 0;
int maxRedPlayers = 5;
int currentBluePlayers = 0;
int MaxBluePlayers = 5;
我在通过网络同步这件事上遇到了很多麻烦。。 目前,我正在做的是,当您加入房间时,您可以使用PhotonNetwork.Instantiate为自己生成一个GameManagerObject。 假设您是第一个加入服务器的,那么变量CurrentRedPlayer和CurrentBluePlayer将设置为0。然后当你选择一支球队时(假设你选择了红队) CurrentRedPlayer将跳转到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中看到,它显示了每个玩家的两个GameManageRobject实例,并将它们的正确值显示为

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

但它们不会互相更新。(这就是我想要的样子)


我很抱歉,如果这是一个新手问题,但我是一个非常新手当谈到双关语,我花了无数个小时试图找到不同的方法来解决这个问题,但这是接近我来了,我撞到墙。有人能告诉我如何让每个客户的对象互相更新,以便他们能够以正确的方式读取房间中当前的玩家吗

您现在可能已经找到了答案,但对于其他人,只需使用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++; });
}

你应该使用rpc,谢谢:)我以前尝试过使用rpc,但它只会更新一个客户端,然后我放弃了,并尝试了我在OP中发布的内容。现在你已经为我指出了正确的方向,我做了一些研究并修复了问题!谢谢,如果你问lol,我会告诉你更多,但如果你修好了,那太好了!:)
int currentRedPlayers = 1;
int maxRedPlayers = 5;
int currentBluePlayers = 1;
int MaxBluePlayers = 5;
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++; });
}