· 6 years ago · Jul 11, 2019, 01:20 AM
1using UnityEngine;
2using System.Collections;
3
4public class HSController : MonoBehaviour
5{
6 private string secretKey = "mySecretKey"; // Edit this value and make sure it's the same as the one stored on the server
7 public string addScoreURL = "http://localhost/unity_test/addscore.php?"; //be sure to add a ? to your url
8 public string highscoreURL = "http://localhost/unity_test/display.php";
9
10 void Start()
11 {
12 StartCoroutine(GetScores());
13 }
14
15 // remember to use StartCoroutine when calling this function!
16 IEnumerator PostScores(string name, int score)
17 {
18 //This connects to a server side php script that will add the name and score to a MySQL DB.
19 // Supply it with a string representing the players name and the players score.
20 string hash = MD5Test.Md5Sum(name + score + secretKey);
21
22 string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
23
24 // Post the URL to the site and create a download object to get the result.
25 WWW hs_post = new WWW(post_url);
26 yield return hs_post; // Wait until the download is done
27
28 if (hs_post.error != null)
29 {
30 print("There was an error posting the high score: " + hs_post.error);
31 }
32 }
33
34 // Get the scores from the MySQL DB to display in a GUIText.
35 // remember to use StartCoroutine when calling this function!
36 IEnumerator GetScores()
37 {
38 gameObject.guiText.text = "Loading Scores";
39 WWW hs_get = new WWW(highscoreURL);
40 yield return hs_get;
41
42 if (hs_get.error != null)
43 {
44 print("There was an error getting the high score: " + hs_get.error);
45 }
46 else
47 {
48 gameObject.guiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
49 }
50 }
51
52}