光子统一foreach循环

时间:2016-02-14 16:01:43

标签: c# unity5 photon

非常简单的问题

如何让这个循环显示所有玩家,因为它只显示我的玩家。 你可以看到我尝试开始新的一行。

当我做除了播放器列表之外的同一个场所我使用其他列表它会返回另一个播放器,所以为什么播放器列表不显示所有人:S

void openCloseScore()
{
    if (Input.GetKey (KeyCode.Tab)) 
    {
        ScoreBoard.SetActive (true);
        foreach(PhotonPlayer player in PhotonNetwork.playerList) 
        {
            ScoreboardTxt.text = "\r\nPlayerName:  " + player.ToString();
        }
    } 
    else 
    {
        ScoreBoard.SetActive(false);
    }
}

2 个答案:

答案 0 :(得分:3)

可能你应该使用+=来连接所有玩家的数据。

ScoreboardTxt.text += "\r\nPlayerName:  " + player.ToString();

如果没有+= ScoreboardTxt.text,则只会包含有关最后一位玩家的数据。

答案 1 :(得分:0)

你只是展示了最后一位玩家,而不是你的玩家。 您应该添加到Text属性,而不是重新生成它。

void openCloseScore(){
  if (Input.GetKey (KeyCode.Tab)) {
      ScoreBoard.SetActive (true);
      foreach(PhotonPlayer player in PhotonNetwork.playerList) {
          //  ... change next line ...
          ScoreboardTxt.text += "\r\nPlayerName:  " + player.ToString(); // <------- change = to += in this line
    }
  } else {
    ScoreBoard.SetActive (false);
  }
}
相关问题