简体   繁体   中英

Ranking and Placement for Scoreboard in Unity3D

I am a newbie and I am stuck in this problem. I can the players stats, scores and names but I can't make the scoreboard work properly. I have worked 2 days trying to figure it out now I am asking you guys.

I have top 10 scoreboard but I cant make the placement. Higher score should have higher placement.

This is my code:

int PlayerCount = PlayerSystem.Players.Count;

    if(PlayerCount == 1)
    {
        Score[0].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
    }

    if (PlayerCount == 2)
    {
        if(PlayerSystem.Players[0].Score > PlayerSystem.Players[1].Score)
        {
            Score[0].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
            Score[1].text = PlayerSystem.Players[1].Name + ": " + PlayerSystem.Players[1].Score.ToString();
        }

        else if(PlayerSystem.Players[1].Score > PlayerSystem.Players[0].Score)
        {
            Score[1].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
            Score[0].text = PlayerSystem.Players[1].Name + ": " + PlayerSystem.Players[1].Score.ToString();
        }

    }

I commented over 200 lines of code because it didnt worked. But I hope you get the idea. Thank you if you readed my post. I really apreciate it if you help me how to do it. Thank you.

First sort the player score.

using System.Linq;

....

List<Player> Players = PlayerSystem.Players.OrderByDescending(p=>p.Score).ToList();

Then assign the scores in a loop to your GUI.

for(int i=0; i<10; ++i)
{
    var player = PlayerSystem.Players[i];
    Score[i].text = player.Name + ": " + player.Score;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM