我最近承担了 Photon 中自定义属性的任务。我已经能够弄清楚如何设置自定义属性,但无法获取自定义属性。我的哈希表在我的播放器控制器脚本中,而我设置(以及我想要获取)属性的位置在循环脚本中。
从圆形系统:
private IEnumerator TeamBalance()
{
angelCount = Mathf.Floor(PhotonNetwork.PlayerList.Length * angelPercent);
currentAngels = angelCount;
currentPlayers = PhotonNetwork.PlayerList.Length;
foreach (var item in PhotonNetwork.PlayerList)
{
var itemPhotonView = (PhotonView)item.TagObject;
itemPhotonView.RPC("SetPlayerTeam", item, citiString);
}
for (int i = 0; i < angelCount;)
{
var item = PhotonNetwork.PlayerList[Random.Range(0, PhotonNetwork.PlayerList.Length)];
var itemPhotonView = (PhotonView)item.TagObject;
if (/* random player selected's, AKA, item's team == citiString */)
{
itemPhotonView.RPC("SetPlayerTeam", item, angelString);
i++;
}
}
yield return null;
//the reason this is in an IEnumerator with 'yield return null'
//is because I plan to add a waiting period once I figure this out
//it's for the game loop
}
从玩家控制器:
[PunRPC]
public void SetPlayerTeam(string teamString)
{
//in the class: private ExitGames.Client.Photon.Hashtable playerProperties;
if (!playerProperties.ContainsKey("team"))
{
playerProperties.Add("team", teamString);
}
playerProperties["team"] = teamString;
PhotonNetwork.LocalPlayer.SetCustomProperties(playerProperties);
}
在回合开始时,一定比例(在本例中为 1/3)的玩家被选为“天使”。此处的检查是必要的,因为在多个天使的情况下,您不希望已经存在的天使算作新的变化。(此外,如果我要使用自定义属性,一般了解如何获取自定义属性可能很重要。)如果我不包括在 RoundSystem 中的检查,结果是 2 个公民和 1 个天使(在 3球员)。此外,如果您看到任何可以改进的意大利面条代码,请不要犹豫告诉我。:)