Photon Unity Networking (PUN) trying to keep track of players on each team
我想发生的是,当玩家连接到他们选择红队或蓝队(1或2)的房间时
每个团队的最大玩家数为5,我通过使用诸如
之类的变量来跟踪玩家
1 2 3 4 | int currentRedPlayers = 0; int maxRedPlayers = 5; int currentBluePlayers = 0; int MaxBluePlayers = 5; |
我在通过网络进行同步时遇到了很多麻烦。
当前,我正在做的是,当您加入Room时,使用PhotonNetwork.Instantiate为自己生成了一个GameManagerObject。
假设您是第一个加入服务器的人,变量currentRedPlayers和currentBluePlayers将设置为0。然后,当您选择一个团队时(假设您选择了红色团队)
currentRedPlayers将跳至1,因此4个变量将如下所示。
1 2 3 4 | currentRedPlayers = 1; maxRedPlayers = 5; currentBluePlayers = 0; MaxBluePlayers = 5; |
这有效。
现在说第二位玩家加入,他们选择了蓝队,他们的变量将是
1 2 3 4 | currentRedPlayers = 0; maxRedPlayers = 5; currentBluePlayers = 1; maxBluePlayers = 5; |
我试图让两个客户端相互更新GameManagerObject的方法是添加一个光子视图,并使其观察对象的脚本" GameManager "。
这是脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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,以便每个客户端将读取变量为
1 2 3 4 | currentredPlayers = 1; maxRedplayers = 5; currentBluePlayers = 1; maxBluePlayers = 5; |
但是,相反,它只会显示自己的内容,如果我在统一编辑器中运行客户端1并以"构建并运行"的身份运行客户端2,则我可以在层次结构中的客户端一个中看到它同时显示了每个玩家的GameManagerObjects实例,以及它显示其正确的值为
1 2 3 4 | int currentRedPlayers = 1; int maxRedPlayers = 5; int currentBluePlayers = 0; int MaxBluePlayers = 5; |
和
1 2 3 4 | currentRedPlayers = 0; maxRedPlayers = 5; currentBluePlayers = 1; maxBluePlayers = 5; |
但是他们不互相更新。 (这就是我想要的样子)
1 2 3 4 | int currentRedPlayers = 1; int maxRedPlayers = 5; int currentBluePlayers = 1; int MaxBluePlayers = 5; |
很抱歉,如果这是一个新手问题,但是在PUN方面我是一个非常新手,我已经花费了无数的时间试图找到不同的方法来解决此问题,但这已经很接近了当我来了,我撞墙了。谁能告诉我我如何让每个客户的对象互相更新,以便他们可以正确的方式读取房间中的当前玩家?
您现在可能已经找到了答案,但是对于其他任何人,只需使用customproperties。
1 2 3 4 5 6 7 | 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++; }); } |