自从UNet贬值以来,我对Photon还是比较陌生。我在获取和设置本地自定义属性时遇到了麻烦。我正在尝试选择两个不同的团队(玩家和天使)。每个玩家都是作为旁观者开始的。一定比例的玩家被选为天使,其余的则被分配为玩家。我可以设法获取并设置随机选择的玩家的属性,但似乎无法为其余玩家分配值。下面的代码段。
private IEnumerator TeamBalance()
{
angelCount = Mathf.Floor(PhotonNetwork.PlayerList.Length * angelPercent);
currentAngels = angelCount;
for (int i = 0; i < angelCount;)
{
int index = Random.Range(0, PhotonNetwork.PlayerList.Length);
if (PhotonNetwork.PlayerList[index].CustomProperties["team"].ToString() == "spectator")
{
PhotonNetwork.PlayerList[index].CustomProperties["team"] = "angel";
i++;
}
}
foreach (var player in PhotonNetwork.PlayerList)
{
if (player.CustomProperties["team"].ToString() == "spectator")
{
player.CustomProperties["team"] = "player";
}
}
yield return null;
}
[3名玩家的最终结果最终选择了1个天使,但仍剩下2名观众。
您需要使用Player.SetCustomProperties函数来设置属性,而不是直接分配属性。这样,PUN可以跟踪所做的更改并正确更新。https://doc-api.photonengine.com/en/pun/v2/class_photon_1_1_realtime_1_1_player.html#a0c1010eda4f775ff56f8c86b026be41e