Unity3D中的简单记分牌,用于排名和排名第一、第二和第三



我正在制作一个简单的统一记分板,它可以通过按钮加减分数,但我不知道如何将分数从第一、第二和第三位排列。

就像大多数比赛一样,我只想展示前三名的最高得分。尽管这个例子只显示了两个分数,但这个程序可能需要对多达10个分数进行排名。

有什么建议吗?

public class Scores : MonoBehaviour
{
public Text scoreText;
public Text scoreText2;
public Text firstPlace;
public Text secondPlace;
public Text thirdPlace;
public int score;
public int score2;
public void Addition()
{
score++;
scoreText.text = "" + score;
}
public void Subtraction()
{      
score--;
scoreText.text = "" + score;
}
private void Update()
{
if (score > score2)
{
firstPlace.text = "Texas A&M";
secondPlace.text = "University of Houston";
thirdPlace.text = "LSU"
}
}
}  

实现这一点的更好方法是字典:

// Create a dictionary
public Dictionary <float,string> scores = new Dictionary<float ,string>();
// Add scores you want to add
scores.Add(1,"SomeText");
scores.Add(4,"SomeOtherText");
scores.Add(3,"SomeOtherText");
// convert the keys into a list
float[] order = scores.Keys.ToList();
// sort array in reverse order
order.Sort.Reverse();
/// print the order
Console.Log("First Place"+scores[order[0]]);
Console.Log("Second Place"+scores[order[1]]);
Console.Log("Third Place"+scores[order[2]]);


参考文献:
https://www.dotnetperls.com/sort-dictionary
https://www.geeksforgeeks.org/different-ways-to-sort-an-array-in-descending-order-in-c-sharp/

请原谅我的拼写错误。

最新更新