如何在光子网络统一中仅在此处打印唯一名称

How to print only unique names here in photon network unity

我这样做了,但它多次打印一个名字。我如何确保它一次只打印一个名字:

foreach(Player player in PhotonNetwork.PlayerList)
{
    if(race_entered)
    {
        for(int i = 0; i <= PhotonNetwork.PlayerList.Length; i++)
        {
            player_name[i].text = player.NickName;
        }
    }
}

您目前正在以指数方式迭代。对于每个玩家,您再次迭代所有玩家并用当前玩家覆盖所有 UI 文本。

你想要的只是迭代一次

if(race_entered)
{
    // cache since property access might be expensive
    var players = PhotonNetwork.PlayerList;

    // Note btw for iterating collections you always want an index 
    // "< Length" instead of "<= Length"
    for(int i = 0; i < players.Length; i++)
    {
        var player = players[i];
        player_name[i].text = player.NickName;
    }
}