使用C#调用php脚本(Unity)

我对Unity和PHP都很陌生,我目前正在开发一个项目,我可以使用PHP将MySQL数据库中的数据解析为Unity。

我最初想尝试启用一种方法,用户可以更改php脚本并使其能够选择不同的数据表,但我被告知,在php脚本中列出所有变量并从中调用它可能更安全相应的统一;

Display.php的

$table = mysql_real_escape_string($_GET['table'], $db); if ($table == "shoes") { $query = "SELECT * FROM `shoes` ORDER by `price` ASC LIMIT 10"; elseif ($table == "sneakers") { $query = "SELECT * FROM `sneakers` ORDER by `price` ASC LIMIT 10"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); $num_results = mysql_num_rows($result); for($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo $row['shopname'] . "\t" . $row['price'] . "\n"; } 

我在调用php并选择我想要选择的表时遇到了麻烦,我对此很新,所以如果这对你们来说似乎完全无能,我会道歉。

这是我的Unity脚本;

HSController.cs

 void Start() { StartCoroutine(GetScores()); } // remember to use StartCoroutine when calling this function! IEnumerator PostScores(string name, int score) { string hash = Md5Sum(name + score + secretKey); string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash; WWW hs_post = new WWW(post_url); yield return hs_post; // Wait until the download is done if (hs_post.error != null) { print("There was an error posting the high score: " + hs_post.error); } } IEnumerator GetScores() { gameObject.guiText.text = "Loading..."; WWW hs_get = new WWW(highscoreURL); yield return hs_get; if (hs_get.error != null) { 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. } } 

任何帮助或正确方向上的一点都会很棒!

亲切的问候

让我尝试将其重写为一个工作示例:

C#

 void Start() { StartCoroutine(GetData()); } IEnumerator GetData() { gameObject.guiText.text = "Loading..."; WWW www = new WWW("http://yoururl.com/yourphp.php?table=shoes"); //GET data is sent via the URL while(!www.isDone && string.IsNullOrEmpty(www.error)) { gameObject.guiText.text = "Loading... " + www.Progress.ToString("0%"); //Show progress yield return null; } if(string.IsNullOrEmpty(www.error)) gameObject.guiText.text = www.text; else Debug.LogWarning(www.error); } 

PHP

  

希望这可以帮助!