问题描述
我有这样的场景: 加入大厅的第一个用户将在其屏幕上显示一个活动按钮。一些动作后,如果玩家点击按钮,它会被禁用,然后房间里的下一个玩家会激活这个按钮,依此类推。 基于属性“转动”,我想像这样更改此按钮状态。
在此函数 Passturn
中,我首先将 LocalPlayer
的 Turn
属性设置为 false
,然后使用某种逻辑确定下一个玩家,并为该玩家设置将 Turn
属性设置为 true
。
private Passturn()
{
PunHashtable _myCustomProperties_forCurrentPlayer = new PunHashtable();
_myCustomProperties_forCurrentPlayer["Turn"] = false;
PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties_forCurrentPlayer);
foreach (Player player in PhotonNetwork.PlayerList)
{
// ... some logic to determine the next player to set the 'Turn' property to true
if (someCondition for player) {
PunHashtable _myCustomProperties_forNextPlayer = new PunHashtable();
_myCustomProperties_forNextPlayer["Turn"] = true;
player.SetCustomProperties(_myCustomProperties_forNextPlayer);
}
}
}
在 OnPlayerPropertiesUpdate
函数中,如果 Turn
属性发生变化,则根据 Turn
属性值激活或停用按钮(如果 true
- 显示按钮,如果 {{ 1}} 不显示按钮)。
false
这不能正常工作,因为它总是在 public override void OnPlayerPropertiesUpdate(Player targetPlayer,ExitGames.Client.Photon.Hashtable changedProps)
{
base.OnPlayerPropertiesUpdate(targetPlayer,changedProps);
if (targetPlayer != null) {
if (changedProps.ContainsKey("Turn"))
{
{
foreach (Player player in PhotonNetwork.PlayerList)
{
// this print will display (1,false) and (2,false) which is not correct,because the second player doesn't have de property changed
print(player.ActorNumber + player.CustomProperties["Turn"];
if ((bool)player.CustomProperties["Turn"] == true)
{
print("hey hey hey 1");
endTurnBtn.SetActive(true);
}
if ((bool)player.CustomProperties["Turn"] == false)
{
print("hey hey hey 2");
endTurnBtn.SetActive(false);
}
}
}
}
}
}
进入,当前播放的按钮将消失,但它永远不会出现在下一个播放器上。
对于这种情况,哪种方法是正确的?如何更改其他玩家屏幕上的 ui 组件?感谢您的时间!
稍后根据接受的评论进行编辑:
hey hey hey 2.
解决方法
您遍历每个玩家并检查变量,因此只要列表中的最后一个玩家的 turn 属性为 false,每个玩家都会禁用该按钮,而当最后一个玩家的 turn 属性为 true 时,每个玩家都会激活该按钮. 无论哪种方式,遍历房间中的每个玩家都是没有意义的,因为您只想更改本地显示的按钮。因此,您应该只检查本地播放器/播放器并相应地更改按钮。