我最近承担了Photon中的自定义属性的任务。我已经能够弄清楚如何set自定义属性,而不是get自定义属性。我的哈希表在我的播放器控制器脚本中,而我设置(以及想要获取)属性的地方在一个循环脚本中。
从RoundSystem:
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
}
从PlayerController:
[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个公民)玩家)。另外,如果您看到任何可以改进的意大利面条代码,请随时告诉我。 :)
使用Player.CustomProperties
词典访问播放器的自定义属性。
foreach (var item in PhotonNetwork.PlayerList)
{
if (item.CustomProperties.ContainsKey("team"))
{
Debug.Log(item.CustomProperties["team"]);
}
}
此外,RoundSystem可以实现IInRoomCallbacks接口并收听OnPlayerPropertiesUpdate来捕获团队更新的确切时间。 https://doc-api.photonengine.com/en/pun/v2/interface_photon_1_1_realtime_1_1_i_in_room_callbacks.html