我想要发生的是当玩家连接到他们选择团队红色或蓝色(1 或 2)的房间时,每个团队的最大玩家是 5,我通过使用变量来跟踪玩家,例如
int currentRedPlayers = 0;
int maxRedPlayers = 5;
int currentBluePlayers = 0;
int MaxBluePlayers = 5;
我在通过网络同步它时遇到了很多麻烦。目前我正在做的是当你加入房间时,你使用 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;
但相反,它只会显示他们自己的,如果我在统一编辑器中运行客户端 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 时,我是一个非常新手,我花了无数个小时试图找到不同的方法来解决这个问题,但这和我来的时候差不多我碰壁了。谁能告诉我如何让每个客户的对象相互更新,以便他们以正确的方式阅读房间中的当前玩家?