Reputation: 137
I Needed To Develop LeaderBoard For Storing Details(means Scores) of Players in Games.Just Displaying players Scores on LeaderBoard in UNITY3D.so plz help me i dont have any idea. in below code Social Platforms NameSpace is there but i dont know how start and how to implement LeaderBoard in unity3d.
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.SocialPlatforms;
public class LBoard : MonoBehaviour
{
ILeaderboard leaderBoard;
// Use this for initialization
void Start ()
{
leaderBoard = Social.CreateLeaderboard();
}
// Update is called once per frame
void Update ()
{
}
}
Upvotes: 0
Views: 9929
Reputation: 186
You need to create an IComparable class and add details like name and score , compare by score.
public class PlayerScore : IComparable<PlayerScore>
public string name;
public int score;
public int CompareTo(PlayerScore other)
{
return this.score.CompareTo(other.score);
}
You also need a list
public static List<PlayerScore> scoreIndex = new List<PlayerScore>(5);
You need some way of getting input from user to add name.
When adding score, create object of iComparer class and set name, score etc.
PlayerScore a = new PlayerScore();//create instance
a.name = PlayerStats.playerName;//set name
a.score = PlayerStats.score;//and score
scoreIndex.Add(a);
Then add new object to List and sort list, List.Sort();. if you want reversed then add reverse().
scoreIndex.Sort ();
scoreIndex.Reverse ();
Save list to player prefs e.g.
PlayerPrefs.SetString("name0" , scoreIndex[0].name);
PlayerPrefs.SetInt("score0" , scoreIndex[0].score);
PlayerPrefs.SetString("name1" , scoreIndex[1].name);
PlayerPrefs.SetInt("score1" , scoreIndex[1].score);
To display names and scores, create 3dText objects for names/scores and place a script like
public class PlayerNameHS : MonoBehaviour
public int pos;
void Start ()
{
renderer.material.color = Color.black;
TextMesh t = (TextMesh)gameObject.GetComponent(typeof(TextMesh));
t.text = PlayerPrefs.GetString("name" + pos);
}
void Update ()
{
}
}
Set Pos for each object.Do the same for scores with score script.
At the start of the game add player prefs into list or you will get an error when trying to retrieve names/scores. needs to be same amount as list size.
PlayerScore a = new PlayerScore();
a.name = PlayerPrefs.GetString("name0");
a.score = PlayerPrefs.GetInt("score0");
yourScript.scoreIndex.Add(a);
PlayerScore b = new PlayerScore();
b.name = PlayerPrefs.GetString("name1");
b.score = PlayerPrefs.GetInt("score1");
yourScript.scoreIndex.Add(b);
Don't know if I'm explaining this well, but you basically need to add playerprefs to list, add comparable scores to list, sort the list, save the list, display the saved list. I'm new to this so take it easy with criticism ;)
Upvotes: 0
Reputation: 5928
If you mean leaderboard like a local high scores table, you would need two functions: AddScore and a function that gets the high scores. (note this example is in C#)
function AddScore(string name, int score){
int newScore;
string newName;
int oldScore;
string oldName;
newScore = score;
newName = name;
for(int 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 = "";
}
}
}
And then to get the highscores:
void GetHighScores()
{
for(int i = 0; i < 10; i++)
{
Debug.Log(PlayerPrefs.GetString(i + "HScoreName") + " has a score of: " + PlayerPrefs.GetInt(i + "HScore"));
}
}
If you want to create a networked/online leaderboard, you need to use something like GameFly (take a look at that example).
Upvotes: -1