Hello everyone. I need to make a highscore table for my game but im not even sure where to begin. Can someone point me in the right direction. I do have a bit of experience with the unityscript so it wont be to much of a problem im just not to sure where to even start. Its not going to be a networking highscore table just a local one. Can someone help thanks!
This script may help I guess.
Dont just copy paste it.
Try to understand it, then edit as per your requirment. The script is in C# as I prefer C# for development.
using UnityEngine;
using System.Collections;
public class HighScore: MonoBehaviour
{
public bool levelComplete;
public string highscorePos;
public int score;
public int temp;
void Start ()
{
score=0;
}
void OnLevelComplete ()
{
levelComplete=true;
score=10000; //values from your scoring logic
for(int i=1; i<=5; i++) //for top 5 highscores
{
if(PlayerPrefs.GetInt("highscorePos"+i)<score) //if cuurent score is in top 5
{
temp=PlayerPrefs.GetInt("highscorePos"+i); //store the old highscore in temp varible to shift it down
PlayerPrefs.SetInt("highscorePos"+i,score); //store the currentscore to highscores
if(i<5) //do this for shifting scores down
{
int j=i+1;
PlayerPrefs.SetInt("highscorePos"+j,temp);
}
}
}
}
void OnGUI()
{
if(levelComplete)
{
for(int i=1; i<=5; i++)
{
GUI.Box(new Rect(100, 75*i, 150, 50), "Pos "+i+". "+PlayerPrefs.GetInt("highscorePos"+i));
}
}
}
}
Instead of adding “score = temp;” at the end try
if(i<5) //do this for shifting scores down
{
int j=i+1;
score = PlayerPrefs.GetInt("highscorePos"+j); //Try and put this here
PlayerPrefs.SetInt("highscorePos"+j,temp);
}
that should chain track the scores and allow for a proper leader board.
Obviously, you will have to use some sort of GUI to show it (unless its some sort of 3D thing), here’s some basic guidelines for the UnityGUI (not saying that you need to use UnityGUI, GUI Textures are great too).
I would go about making a builtin array, of say, 10 integers, and these will be your highscores. Mathf has its Max function, can be used for sorting. There’s also the javascript array which has a sort function, but I’m not 100% sure that it works with numbers, and not just words.
Now, saving your highscores, PlayerPrefs which should do the trick.
This was kind of a “shotgun” approach. I may have missed some vital information, as I did not spend too long on this answer, but it should give you some sort of guide or concept on how to do this.
Good Luck! I hope it helps.
Check out NGUI. I mainly code in C# but you can also use JS with the plugin. It’s an awesome GUI plugin. Saves a lot of time and has tons of features. You could easily create a low-draw-call GUI using NGUI and use their built in functions to access all the features of your high score table. As far as coding it goes, just save the high scores to PlayerPrefs. See the Unity Documentation regarding playerprefs.