- Home /
Show Top 10 Of 1 GameType
Hello Everyone i have Game that contain mini-Games and all Games put the Score in 1 list
public List highscore;
foreach(Scores _score in highscore)
{
GUILayout.BeginHorizontal();
GUILayout.Label(_score.name, "NameStyle");
GUILayout.Label(""+_score.score, "ScoreStyle");
GUILayout.Label(""+_score.GameType, "ScoreStyle");
GUILayout.EndHorizontal();
}
how i can Only Show the highscore depending on the "GameType" for Example : Puzzle
Only Show The Puzzle Score . dont show other game like Tennis,
Just go trough your collection and search for gametype you want. Filer out your first 10 matches and display them.
Answer by gregzo · Jul 28, 2013 at 09:32 AM
Hi, It seems your Scores class ( or struct? ) already contains a GameType member. So why don't you just check the GameType, and display only if it matches your predicate? I don't know what type your Scores.GameType member is, I'll assume it's a string :
void DisplayScoresOfType ( string type, int maxDisplayedScores )
{
int counter = 0;
foreach(Scores _score in highscore)
{
GUILayout.BeginHorizontal();
if ( _score.GameType == type )
{
counter++;
GUILayout.Label(_score.name, "NameStyle");
GUILayout.Label(""+_score.score, "ScoreStyle");
GUILayout.Label(""+_score.GameType, "ScoreStyle");
}
GUILayout.EndHorizontal();
if ( counter == maxDisplayedScores )
{
break;
}
}
}
Ofcourse, if you have more than 10 elements of each type, you want to end the loop.
Thanks. but it didnt work and No Erros
i use this Code to Get the HighScore i Putted in Start Function
highscore = HighScore$$anonymous$$anager._instance.GetHighScore();
i should put that in the button?
if(GUILayout.Button("Tennis")){
highscore = HighScore$$anonymous$$anager._instance.GetHighScore();
DisplayScoresOfType("Tennis",10);
}
if(GUILayout.Button("Soccer")){
highscore = HighScore$$anonymous$$anager._instance.GetHighScore();
DisplayScoresOfType("Soccer",10);
}
Edit 3
works. thank you but i have other problem i wonder if u can help me
that i have a HighScore Background + GUI.Label when the window Size Change, everything change his position , how i can make it static position or just realizable in good way ? sry for my bad english
Answer by dan19 · Jul 28, 2013 at 11:07 AM
You can also use Linq. For instance, to show the Top 10 scores for game type "tennis", just replace this line:
foreach(Scores _score in highscore)
{
//...
with this:
foreach(Scores _score in highscore.Where((arg) => arg.GameType == "Tennis")
.OrderBy((arg) => -arg.score)
.Take(10))
{
//...
and also add "``using System.Linq;``" in the beginning of your script.
Of course, I assumed that GameType fields are strings. And score fields are ints. Adjust appropriately if they aren't.