Hello,
EDIT: I have find out this, but there’s one last thing - read my last post please.
I am finalizing my game and there’s the last thing I want to add - online highscore ranking with usernames. I want to add an option, where the player can create his username before playing, then his highscore would be stored and published online.
I have writed the highscore thing with this tutorial:
but my game don’t have the username option yet. I don’t know how to add it and connect it to the highscore script. Is there aynone that knows how to make it or can provide links to some tutorials related to that topic?
Here is the script (C#):
“Highscores”:
using UnityEngine;
using System.Collections;
public class HighScores : MonoBehaviour {
const string privateCode = "here goes the private code";
const string publicCode = "here goes the public code";
const string webURL = "http://dreamlo.com/lb/";
public Highscore[] highscoresList;
static HighScores instance;
DisplayHighscores highscoresDisplay;
void Awake()
{
instance = this;
highscoresDisplay = GetComponent<DisplayHighscores>();
}
public static void AddNewHighscore(string username, int score)
{
instance.StartCoroutine (instance.UploadNewHighscore (username, score));
}
IEnumerator UploadNewHighscore(string username, int score)
{
WWW www = new WWW (webURL + privateCode + "/add/" + WWW.EscapeURL (username) + "/" + score);
yield return www;
if (string.IsNullOrEmpty (www.error))
{
print ("Upload Successful");
DownloadHighscores();
}
else
{
print ("Error Uploading: " + www.error);
}
}
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);
highscoresDisplay.OnHighscoresDownloaded(highscoresList);
}
else
{
print ("Error Downloading: " + www.error);
}
}
void FormatHighscores(string textStream)
{
string[] entries = textStream.Split (new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
highscoresList = new Highscore[entries.Length];
for (int i = 0; i <entries.Length; i ++)
{
string[] entryInfo = entries[i].Split(new char[] {'|'});
string username = entryInfo[0];
int score = int.Parse (entryInfo[1]);
highscoresList[i] = new Highscore(username,score);
print (highscoresList[i].username + ": " + highscoresList[i].score);
}
}
}
public struct Highscore
{
public string username;
public int score;
public Highscore(string _username, int _score)
{
username = _username;
score = _score;
}
}
“DisplayHighscores”:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DisplayHighscores : MonoBehaviour {
public Text[] highscoreText;
HighScores highscoreManager;
// Use this for initialization
void Start ()
{
for (int i =0; i < highscoreText.Length; i ++)
{
highscoreText[i].text = i+1 + ". Fetching...";
}
highscoreManager = GetComponent<HighScores> ();
StartCoroutine ("RefreshHighscores");
}
public void OnHighscoresDownloaded (Highscore[] highscoreList)
{
for (int i =0; i < highscoreText.Length; i ++)
{
highscoreText[i].text = i+1 + ". ";
if (highscoreList.Length > i)
{
highscoreText[i].text += highscoreList[i].username + " - " + highscoreList[i].score;
}
}
}
IEnumerator RefreshHighscores()
{
while (true)
{
highscoreManager.DownloadHighscores();
yield return new WaitForSeconds(30);
}
}
}
“HighscoreTest”:
using UnityEngine;
using System.Collections;
public class HighscoreTest : MonoBehaviour {
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
int score = Random.Range(0,100);
string username = "";
string alphabet = "abcdefghijklmnoprstuwxyz";
for (int i = 0; i < Random.Range(5,10); i ++)
{
username += alphabet[Random.Range(0,alphabet.Length)];
}
HighScores.AddNewHighscore(username,score);
}
}
}