public void CheckForHighScore(){
finall = PlayerPrefs.GetInt("score");// taking score variable from another script to pass in ScoreBoard
for (int x = 0; x < highScores.Length; x++){
if (finall > highScoreValues[x]){
for (int y = highScores.Length - 1; y > x; y--) { //sorting
highScoreValues[y] = highScoreValues[y - 1];
}
highScoreValues[x] = finall;
DrawScores();
SaveScore();
PlayerPrefs.DeleteKey("score");
break;
}
}
}
void DrawScores() {
for (int x = 0; x < highScores.Length; x++) { // just adding score to string
highScores[x].text = highScoreValues[x].ToString();
}
}
大家好。如何从ScoreBoard数组中删除重复项?试图将这段代码放在排序循环下,但实际上什么也没发生。我也尝试了其他方法,但是它们无法正常工作。任何帮助表示赞赏。
/*for (int j = 1; j < x; j++)
{
if (highScoreValues[j] == highScoreValues[x])
break;
else
highScoreValues[x] = finall;
}*/
答案 0 :(得分:0)
您可以使用linq。 如果高分是一个对象,则必须重写Equals和GetHashCode
public override bool Equals(object obj)
{
// Insert the Code to override
}
public override int GetHashCode()
{
// insert the Code to override
}
请确保GetHashCode为每个对象返回唯一的整数。这样一来,您可以在此处查看更多信息:Correct way to override Equals() and GetHashCode()
如果highScore是基元,则不需要重写方法。
统一highScore之后,您可以使用以下代码:
var sorted = highScores.OrderByDescending(h => h).Distinct()。ToArray();
答案 1 :(得分:-1)
List<*Insert type of score here*> cache = new List<*Insert type of score here*>();
for(int index = 0; index < highScores.Length; ++index)
{
if(cache.Contains(highScores[index]))
{
highScores.RemoveAt(index);
--index; // Every time you have to remove an item, it will slot the next item
// into that index and subtract 1 from highScores length. Thus you
// will have to decriment the index in order to check the item that
// gets pushed into that index of your highScore Array. You many
// need to check the syntax if highScores is not a List. But
// hopefully this helps :)
}
else
{
cache.Add(highScores[index]);
}
}
}
}