Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class highscorecontroller : MonoBehaviour {
- public Text input;
- string secretkey = "SuperSecretPasswordString"; //change this to secure your connection
- string hsShowURL = "http://localhost/unity/show.php";
- string highsoreAddURL = "http://localhost/unity/add.php";
- // Use this for initialization
- void Start () {
- StartCoroutine(GetScores());
- }
- public void save()
- {
- //search for the inputfield and get the playerinput
- string PlayerName = GameObject.Find("InputField").GetComponent<Text>().text;
- string playername = input.text;
- StartCoroutine(PostScores("boi", 1000));
- }
- // Using namn and score to connect to SQL DB and save info
- IEnumerator PostScores(string name, int score)
- {
- string hash = Md5sum(name + score + secretkey);
- //create form with the info connected to webpage POST
- WWWForm form = new WWWForm();
- form.AddField("name", name);
- form.AddField("score", score);
- form.AddField("hash", hash);
- //connect and send the form to the post on webpage
- WWW www = new WWW(highsoreAddURL, form);
- yield return www; //Wait untill connection completes
- if (www != null)
- {
- Debug.Log("Error: " + www.error);
- }
- else
- gameObject.GetComponent<Text>().text = www.text;
- }
- //get the scores from SQL Database adn display in highscore textbox
- IEnumerator GetScores()
- {
- gameObject.GetComponent<Text>().text = "Loading Scores";
- WWW highscoreget = new WWW(hsShowURL);
- yield return highscoreget;
- if(highscoreget.error != null)
- {
- Debug.Log("There was an error getting the score:" + highscoreget.error);
- }
- else
- {
- //get the information from the show.php file
- gameObject.GetComponent<Text>().text = highscoreget.text;
- }
- }
- public string Md5sum(string strToEncrypt)
- {
- System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
- byte[] bytes = ue.GetBytes(strToEncrypt);
- //encrypt
- System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] hashbytes = md5.ComputeHash(bytes);
- string hashString = "";
- //Convert to encrypted string
- for (int i = 0; i < hashbytes.Length; i++)
- {
- hashString += System.Convert.ToString(hashbytes[i], 16).PadLeft(2, '0');
- }
- return hashString.PadLeft(32, '0');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment