I am trying to get the high score here- http://wiki.unity3d.com/index.php?title=Server_Side_Highscores set up.
I ahve the DB set up the php files uploaded and all set. But I am getting errors on the md5 file.
#pragma strict
static function Md5Sum(strToEncrypt : String)
{
var encoding = System.Text.UTF8Encoding();
var bytes = encoding.GetBytes(strToEncrypt);
// encrypt bytes
var md5 = System.Security.Cryptography.MD5CryptoServiceProvider();
var hashBytes:byte[] = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
var hashString = "";
for (var i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, "0"[0]);
}
return hashString.PadLeft(32, "0"[0]);
}
And here is the JS code on the gameobject.
private var secretKey="blah"; // Edit this value and make sure it's the same as the one stored on the server
var addScoreUrl="http://www.blah.com/Splitting8s_HS/addscore.php?"; //be sure to add a ? to your url
var highscoreUrl="http://www.blah.com/Splitting8s_HS/display.php";
function Start()
{
getScores();
}
function postScore(name, score)
{
//This connects to a server side php script that will add the name and score to a MySQL DB.
// Supply it with a string representing the players name and the players score.
var hash = md5functions.Md5Sum(name + score + secretKey);
var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
// Post the URL to the site and create a download object to get the result.
hs_post = WWW(highscore_url);
yield hs_post; // Wait until the download is done
if(hs_post.error) {
print("There was an error posting the high score: " + hs_post.error);
}
}
// Get the scores from the MySQL DB to display in a GUIText.
function getScores()
{
gameObject.guiText.text = "Loading Scores";
hs_get = WWW(highscoreUrl);
yield hs_get;
if(hs_get.error)
{
print("There was an error getting the high score: " + hs_get.error);
}
else
{
gameObject.guiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
}
}
Errors…