I am using the asset Dreamlo to integrate a leaderboard into my game. When it comes to uploading the scores I have no problem, however I can not seem to get the scores into the game to make an actual leader board. I am using the C# code attached here, and it seems to work, with one exception: it only changes the text to show the lowest score in the highscore list. I can not seem to figure out why it is only showing one score. Any help will be greatly appreciated as I have been stuck on this for awhile.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LeaderboardTest : MonoBehaviour
{
public Text leaderboardText;
const string privateCode = "zRsjl1URjU6bDwG8Lgj4bQKSlXLn400kuCaDmtug7sCA";
const string publicCode = "5626900d6e51b610ec03dd17";
const string webURL = "http://dreamlo.com/lb/";
public Highscore[] highscoresList;
void Awake ()
{
DownloadHighscores();
}
public void DownloadHighscores() {
StartCoroutine("DownloadHighscoresFromDatabase");
}
IEnumerator DownloadHighscoresFromDatabase() {
WWW www = new WWW(webURL + publicCode + "/pipe/");
yield return www;
if (string.IsNullOrEmpty (www.error)) {
FormatHighscores(www.text);
}
else {
print ("Error Downloading: " + www.error);
}
}
void FormatHighscores(string textStream) {
string[] entries = textStream.Split(new char[] {'
'}, System.StringSplitOptions.RemoveEmptyEntries);
highscoresList = new Highscore[entries.Length];
for (int i = 0; i <entries.Length; i ++) {
string[] entryInfo = entries*.Split(new char[] {'|'});*
_ highscoresList = new Highscore(username,score);_
print (highscoresList_.username + ": " + highscoresList*.score);*_
* //this line will change the ui text*
leaderboardText.text = (highscoresList .username + ": " + highscoresList .score);
* }*
* }*
}
public struct Highscore {
* public string username;*
* public int score;*
* public Highscore(string username, int score) {
username = username;
score = score;*
* }*
}
Well I contacted the creator of Dreamlo and I got some help from him. This is the script I am now using and it works beautifully. I just figured I would post this for anyone who like me needs help with this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LeaderboardTest : MonoBehaviour
{
public Text text;
public Text newHighScoreTexts;
public Transform holder;
const string privateCode = "zRsjl1URjU6bDwG8Lgj4bQKSlXLn400kuCaDmtug7sCA";
const string publicCode = "5626900d6e51b610ec03dd17";
const string webURL = "http://dreamlo.com/lb/";
public Highscore[] highscoresList;
void Start ()
{
DownloadHighscores();
}
public void DownloadHighscores() {
StartCoroutine("DownloadHighscoresFromDatabase");
}
IEnumerator DownloadHighscoresFromDatabase() {
WWW www = new WWW(webURL + publicCode + "/pipe/");
yield return www;
if (string.IsNullOrEmpty (www.error)) {
FormatHighscores(www.text);
}
else {
print ("Error Downloading: " + www.error);
}
}
void FormatHighscores(string textStream) {
string[] entries = textStream.Split(new char[] {'
'}, System.StringSplitOptions.RemoveEmptyEntries);
highscoresList = new Highscore[entries.Length];
for (int i = 0; i <entries.Length; i ++) {
string[] entryInfo = entries*.Split(new char[] {'|'});*
_ highscoresList = new Highscore(username,score);_
* newHighScoreTexts = (Instantiate (text, new Vector3 (0,0,0), new Quaternion (0,0,0,0), holder));*
* newHighScoreTexts.name = ("Score " + i.ToString());*
print (highscoresList_.username + ": " + highscoresList*.score);*_
* //this line will change the ui text*
newHighScoreTexts.text = (highscoresList_.username + ": " + highscoresList*.score);
}
}*_
}
public struct Highscore {
* public string username;*
* public int score;*
* public Highscore(string username, int score) {
username = username;
score = score;*
* }*
}
Hi guys, I’m sorry for busting in like this, but i’m struggling for days to implement this leaderboard in my game .
I tried it like this,doesn’t work :
Any help will be great guys ! Thank you
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GController : MonoBehaviour {
public static GController instance;
public GameObject gameOverText;
public Text Score;
public Text Highscore;
public bool gameOver = false;
public float scrollSpeed = -1.5f;
public int score = 0;
public int highscore;
public string username;
void Awake ()
{
string username = GetComponent<InputName> ().username;
//<<<<HERE, i have the username inputfield in InputName script,
which is in main menu,scene0),
// and this script(GController) is in the game(scene1)
//and the Dreamlo scripts are in TopPlyers,(scene2), ( if it helps)
this.highscore = PlayerPrefs.GetInt("HighScore");
Highscore.text = "HighScore: " + highscore.ToString ();
PlayerPrefs.Save ();
if (instance == null) {
instance = this;
} else if (instance != this)
{
Destroy (gameObject);
}
}
void Update ()
{
if (gameOver == true && Input.GetMouseButtonDown (0))
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
}
public void NugScored()
{
if (gameOver)
{
return;
}
score++;
if (score > highscore)
{
highscore = score;
PlayerPrefs.SetInt("HighScore", score);
}
if (score > 7) {
scrollSpeed = scrollSpeed + -7.0f;
}
Score.text = "Score: " + score.ToString ();
Highscore.text = "HighScore: " + highscore.ToString ();
Highscores.AddNewHighscore (username, score); // <<<< HERE
}
public void NugDied()
{
gameOverText.SetActive (true);
gameOver = true;
}
}