Hi,
Can any one help me with the logical code (C# ‘Most appreciated’) to get the High Score list of top 5 or top 10 Players Using Playerprefs.
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();
}
}
}
it is simple copy the code to a file named HighScore. uncomment the like line inside function Update() and run it once.
now if u stop it and run again you will see the output in debug log.
To use this in your own code, just attach the file (after commenting the line in update) to some game object. then keep the reference of the object in you code and call the SaveHighScore() function to post score and use GetHighScore to retrive it.
your code should look like this
using UnityEngine;
using System.Collections;
public class CustomClass:MonoBehaviour
{
public HighScore _highScore; //you have to link the object here
int score;
List<Scores> highScoreList; // whn u retrive saved score
void Update()
{
//condition for posting score
_highScore.SaveHighScore("Player name",score);
//when you want to get the saved high score.
highScoreList=_highScore.GetHighScore();
}
}
i have script for displaying highest five scores but names are not coming properly
and scores are affected(by -10) if any new score is added in the list
if(Score > PlayerPrefs.GetFloat(“HighScore”))
{
PlayerPrefs.SetFloat(“HighScore”,Score);
// PlayerPrefs.SetString("NewName",PlayerPrefs.GetString("CurrentName"));
}
if(Score < PlayerPrefs.GetFloat("HighScore") && Score > PlayerPrefs.GetFloat("Score"))
{
PlayerPrefs.SetFloat("Score",Score);
// PlayerPrefs.SetString("NewName1",PlayerPrefs.GetString("CurrentName"));
}
if(Score < PlayerPrefs.GetFloat("Score") && Score > PlayerPrefs.GetFloat("Score2"))
{
PlayerPrefs.SetFloat("Score2",Score);
// PlayerPrefs.SetString("NewName2",PlayerPrefs.GetString("CurrentName"));
}
if(Score < PlayerPrefs.GetFloat("Score2") && Score > PlayerPrefs.GetFloat("Score3"))
{
PlayerPrefs.SetFloat("Score3",Score);
//PlayerPrefs.SetString("NewName3",PlayerPrefs.GetString("CurrentName"));
}
if(Score < PlayerPrefs.GetFloat("Score3") && Score > PlayerPrefs.GetFloat("Score4"))
{
PlayerPrefs.SetFloat("Score4",Score);
//PlayerPrefs.SetString("NewName4",PlayerPrefs.GetString("CurrentName"));
}
can any one solve this proble?
please update this code
thanks