Saving a list of highscores to a players device is not too complicated.
For a quicker implementation we will use PlayerPrefs which is a native unity class.
Start with making your default names and scores, have these as separate text elements so it makes it easier to edit them through script.
Once you have all your elements in position we will also add an input field, make it cover the entire name field completely. I also added a button at the bottom to submit the name for the scoreboard.
Now that you have all the base elements in place, let us get to making it function.
First we need to make a script , I called mine ScoreBoard attach this to your scoreboard canvas.
We will need to add all the scores and names to the script so we can edit them. For that we will use a [SerializeField]private or public List<Text>(or TMP_Text if you want to use textmeshpro) name them appropriately
A list of variables we will need
List<int> _score = new List<int>();
This is our list we will be filling our highscores into.
List<string> _scoreNames = new List<string>();
This is our list we will be filling our highscore names into.
This we will be storing a score in temporarily so we can change it’s position.
This we will be using this to store the position of the highscore to set.
This we will be using to store a name temporarily while we change its position down.
String scoreList && scoreNameList
This are strings we will be using to build our save from.
This is what our code looks like so far.
Next we set our highscores
This is a public class so we can attach it to our submit button
We take in a int(score) and a string(name)
This is what our code will look like.
Let’s walk through it, We are using a for loop to cycle through all of the highscores in our highscore list we set earlier, we continue to cycle through while we are less then the count our list of scores, remember lists and arrays start at 0, so we cycle through this from 0–9 inclusively since there will be 10 items in our list.
We then use an if statement to see if the highscores position is equal to or greater then our cycle through the loop, if not we hit the end of our for loop and increment our looping value and start again, if our value is equal or greater we then add to our tempname and tempscore the score and name currently in the list position.
We then replace the score and name with our values we received from calling the class.
Instead of making a third name and score variable to hold the temp values we use our class ones to hold the temp value we just removed.
we then build a string of Score+our loop value so Score0 through Score9 and ScoreName0 through ScoreName9
this saves us using 10 string values to hold all the save names.
Then using PlayerPrefs.SetInt and SetString we set our score and name.
We then go back to the start of the for loop and cycle till we hit 10 cycles.
Next we load our scores, we can also make a class that checks if there are any highscores and set some defaults if so.
For the loadscores we start by clearing the current highscore list and names, this way we are not adding duplicate sets.
Then we go through the entire set of scores saved in a for loop using the same method as saving, but this time we use GetInt and SetInt.
We check to see if there were any scores loaded when we call loadscores, if we don’t find any then we initialise a set of default scores, starting from 11000 and taking 1000 off for each score. we then save these all to our PlayerPrefs, if you want to check that it is saving these properly you can set the loaded array as public and check in inspector.
Now that we have all of our highscores loaded. You also have made your game that you get the score to set in. When you get to the end of the level or whatever you are scoring, lets check to see if it is a highscore.
We make ourselves a public class so it can be called on a trigger.
We then load our scores into the list, this is for if we change scenes or change the highscores some other way.
Then we go through all the scores and check our score against the values, if we find it is higher then a score, we set that position to change and we set our for loop to end.
We then return our position to replace, this is so we can set our inputfield to the correct position.
On the script that we called the highscoreboard from, we attach our InputField or TMP_InputField either as public of [SerializeField].
We then check to see if the highscore high better then one we already have, thats why we had a value that was outside the highscores, in our case we had a default value of 99 .
so if it is not 99 then it is a value we want to be able to set. so we set our InputField to the right spot in our list. We then also SetActive it, as it is hidden by default. We then turn off all other inputs so the player is not moving while typing their name.
We then call back to our scoreboard with the SetHighScore method earlier
This then sets the names to our score board, going through each list of text we set at the start.