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

发布于 2025-01-26 15:36:39 字数 293 浏览 2 评论 0原文

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

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

I did this but it prints one name multiple times. How do I make sure it prints one name only one at a time:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2025-02-02 15:36:39

您目前正在呈指数次迭代。对于每个玩家,您再次迭代所有播放器,并用当前播放器覆盖所有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;
    }
}

You are currently iterating exponentially. For every player you again iterate all players and overwrite all UI texts with the current player.

What you want is iterating only once

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文