I just want a simple highscores board, that will appear when the game is over... e.g.
1. Player name 99999
2. my name 88888
...
...
...
...
...
...
I just want a simple highscores board, that will appear when the game is over... e.g.
1. Player name 99999
2. my name 88888
...
...
...
...
...
...
Server based highscores:
http://www.unifycommunity.com/wiki/index.php?title=Server_Side_Highscores
Pretty straightforward.
@MikezNesh
For a local highscore save data on PlayerPrefs (if it's not full) Do this:
function AddScore(name : String, score : int){
var newScore : int;
var newName : String;
var oldScore : int;
var oldName : String;
newScore = score;
newName = name;
for(i=0;i<10;i++){
if(PlayerPrefs.HasKey(i+"HScore")){
if(PlayerPrefs.GetInt(i+"HScore")<newScore){
// new score is higher than the stored score
oldScore = PlayerPrefs.GetInt(i+"HScore");
oldName = PlayerPrefs.GetString(i+"HScoreName");
PlayerPrefs.SetInt(i+"HScore",newScore);
PlayerPrefs.SetString(i+"HScoreName",newName);
newScore = oldScore;
newName = oldName;
}
}else{
PlayerPrefs.SetInt(i+"HScore",newScore);
PlayerPrefs.SetString(i+"HScoreName",newName);
newScore = 0;
newName = "";
}
}
}
To display the high score set up a GUI to display the text and int in a table and store the information from player prefs in variables. Initialise the high score table by calling AddScore at the start of your game.
var 1stScore : int;
var 1stName : String;
var 2ndScore : int;
var 2ndName : String;
etc...
function UpdateHScore(){
1stScore = PlayerPrefs.GetInt(0HScore);
1stName = PlayerPrefs.GetString(0HScoreName);
etc....
}
Alternatively use PlayerPrefsX from the wiki:
http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs
So in using this on an android game, it’ll store the players name just fine, but it won’t store the score - it simply puts a 0 in there
Hi this is a very simple high score system for saving a int score and a name. Hope the code is clear
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// High score manager.
/// Local highScore manager for LeaderboardLength number of entries
///
/// this is a singleton class. to access these functions, use HighScoreManager._instance object.
/// eg: HighScoreManager._instance.SaveHighScore("meh",1232);
/// No need to attach this to any game object, thought it would create errors attaching.
/// </summary>
public class HighScoreManager : MonoBehaviour
{
private static HighScoreManager m_instance;
private const int LeaderboardLength = 10;
public static HighScoreManager _instance {
get {
if (m_instance == null) {
m_instance = new GameObject ("HighScoreManager").AddComponent<HighScoreManager> ();
}
return m_instance;
}
}
void Awake ()
{
if (m_instance == null) {
m_instance = this;
} else if (m_instance != this)
Destroy (gameObject);
DontDestroyOnLoad (gameObject);
}
public void SaveHighScore (string name, int score)
{
List<Scores> HighScores = new List<Scores> ();
int i = 1;
while (i<=LeaderboardLength && PlayerPrefs.HasKey("HighScore"+i+"score")) {
Scores temp = new Scores ();
temp.score = PlayerPrefs.GetInt ("HighScore" + i + "score");
temp.name = PlayerPrefs.GetString ("HighScore" + i + "name");
HighScores.Add (temp);
i++;
}
if (HighScores.Count == 0) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Add (_temp);
} else {
for (i=1; i<=HighScores.Count && i<=LeaderboardLength; i++) {
if (score > HighScores [i - 1].score) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Insert (i - 1, _temp);
break;
}
if (i == HighScores.Count && i < LeaderboardLength) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Add (_temp);
break;
}
}
}
i = 1;
while (i<=LeaderboardLength && i<=HighScores.Count) {
PlayerPrefs.SetString ("HighScore" + i + "name", HighScores [i - 1].name);
PlayerPrefs.SetInt ("HighScore" + i + "score", HighScores [i - 1].score);
i++;
}
}
public List<Scores> GetHighScore ()
{
List<Scores> HighScores = new List<Scores> ();
int i = 1;
while (i<=LeaderboardLength && PlayerPrefs.HasKey("HighScore"+i+"score")) {
Scores temp = new Scores ();
temp.score = PlayerPrefs.GetInt ("HighScore" + i + "score");
temp.name = PlayerPrefs.GetString ("HighScore" + i + "name");
HighScores.Add (temp);
i++;
}
return HighScores;
}
public void ClearLeaderBoard ()
{
//for(int i=0;i<HighScores.
List<Scores> HighScores = GetHighScore();
for(int i=1;i<=HighScores.Count;i++)
{
PlayerPrefs.DeleteKey("HighScore" + i + "name");
PlayerPrefs.DeleteKey("HighScore" + i + "score");
}
}
void OnApplicationQuit()
{
PlayerPrefs.Save();
}
}
public class Scores
{
public int score;
public string name;
}
to check if this work check the following script, just add this to a game object and you will be able to check its functionality
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MenuController : MonoBehaviour {
string name="";
string score="";
List<Scores> highscore;
// Use this for initialization
void Start () {
EventManager._instance._buttonClick += ButtonClicked;
highscore = new List<Scores>();
}
void ButtonClicked(GameObject _obj)
{
print("Clicked button:"+_obj.name);
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.Label("Name :");
name = GUILayout.TextField(name);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Score :");
score = GUILayout.TextField(score);
GUILayout.EndHorizontal();
if(GUILayout.Button("Add Score"))
{
HighScoreManager._instance.SaveHighScore(name,System.Int32.Parse(score));
highscore = HighScoreManager._instance.GetHighScore();
}
if(GUILayout.Button("Get LeaderBoard"))
{
highscore = HighScoreManager._instance.GetHighScore();
}
if(GUILayout.Button("Clear Leaderboard"))
{
HighScoreManager._instance.ClearLeaderBoard();
}
GUILayout.Space(60);
GUILayout.BeginHorizontal();
GUILayout.Label("Name",GUILayout.Width(Screen.width/2));
GUILayout.Label("Scores",GUILayout.Width(Screen.width/2));
GUILayout.EndHorizontal();
GUILayout.Space(25);
foreach(Scores _score in highscore)
{
GUILayout.BeginHorizontal();
GUILayout.Label(_score.name,GUILayout.Width(Screen.width/2));
GUILayout.Label(""+_score.score,GUILayout.Width(Screen.width/2));
GUILayout.EndHorizontal();
}
}
}
I used the code above and I feel like it goes in to a loop uncontrolled. Here is an example of what happens. If the player gets a new score that is higher then the highest score then it wipes out the old score and replaces it with the highest.
So:
Dan : 75
Dan : 50
Dan : 25
Dan : 25
Dan : 25
Becomes:
Dan : 100
Dan : 100
Dan : 100
Dan : 100
Dan : 100