PUN 2 获取和设置自定义属性
PUN 2 Getting and Setting Custom Properties
自从 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
自从 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