Coltmannnen

Untitled

Apr 7th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. public class highscorecontroller : MonoBehaviour {
  2.  
  3. public Text input;
  4. string secretkey = "SuperSecretPasswordString"; //change this to secure your connection
  5. string hsShowURL = "http://localhost/unity/show.php";
  6. string highsoreAddURL = "http://localhost/unity/add.php";
  7.  
  8. // Use this for initialization
  9. void Start () {
  10.  
  11. StartCoroutine(GetScores());
  12.  
  13. }
  14.  
  15. public void save()
  16. {
  17.  
  18. //search for the inputfield and get the playerinput
  19.  
  20. string PlayerName = GameObject.Find("InputField").GetComponent<Text>().text;
  21. string playername = input.text;
  22. StartCoroutine(PostScores("boi", 1000));
  23. }
  24.  
  25. // Using namn and score to connect to SQL DB and save info
  26. IEnumerator PostScores(string name, int score)
  27. {
  28. string hash = Md5sum(name + score + secretkey);
  29. //create form with the info connected to webpage POST
  30. WWWForm form = new WWWForm();
  31. form.AddField("name", name);
  32. form.AddField("score", score);
  33. form.AddField("hash", hash);
  34. //connect and send the form to the post on webpage
  35. WWW www = new WWW(highsoreAddURL, form);
  36.  
  37. yield return www; //Wait untill connection completes
  38.  
  39. if (www != null)
  40. {
  41. Debug.Log("Error: " + www.error);
  42. }
  43. else
  44. gameObject.GetComponent<Text>().text = www.text;
  45. }
  46.  
  47. //get the scores from SQL Database adn display in highscore textbox
  48. IEnumerator GetScores()
  49. {
  50. gameObject.GetComponent<Text>().text = "Loading Scores";
  51. WWW highscoreget = new WWW(hsShowURL);
  52. yield return highscoreget;
  53.  
  54. if(highscoreget.error != null)
  55. {
  56. Debug.Log("There was an error getting the score:" + highscoreget.error);
  57. }
  58. else
  59. {
  60. //get the information from the show.php file
  61. gameObject.GetComponent<Text>().text = highscoreget.text;
  62. }
  63.  
  64. }
  65.  
  66.  
  67. public string Md5sum(string strToEncrypt)
  68. {
  69. System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
  70. byte[] bytes = ue.GetBytes(strToEncrypt);
  71.  
  72. //encrypt
  73. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  74. byte[] hashbytes = md5.ComputeHash(bytes);
  75.  
  76. string hashString = "";
  77. //Convert to encrypted string
  78. for (int i = 0; i < hashbytes.Length; i++)
  79. {
  80. hashString += System.Convert.ToString(hashbytes[i], 16).PadLeft(2, '0');
  81. }
  82. return hashString.PadLeft(32, '0');
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment