我整理了一些冠以Unity连接Php的代码,和大家一起分享一下,希望大家能够用得到:
  1. function Login() {

  2. var form = new WWWForm(); //创建一个WWWForm对象。

  3. form.AddField( “myform_hash”, hash ); //add your hash code to the field myform_hash, check that this variable name is the same as in PHP file

  4. form.AddField( “myform_nick”, formNick );

  5. form.AddField( “myform_pass”, formPassword );

  6. var w = WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form

  7. yield w; //we wait for the form to check the PHP file, so our game dont just hang

  8. if (w.error != null) {

  9. print(w.error); //if there is an error, tell us

  10. } else {

  11. var return_data=w.data;

  12. if(return_data==“success”){

  13. Application.ExternalEval(“window.location.href='game.php?username=”+formNick+“‘”);

  14. }else if(return_data==“npe”){

  15. print(“Test ok”);

  16. formText = “用户名或密码不能为空!”; //here we return the data our PHP told us

  17. }else if(return_data==“nfn”){

  18. formText=“用户名不存在!”;

  19. }else if(return_data==“npw”){

  20. formText=“用户名或密码不正确!”;

  21. }

  22. w.Dispose(); //clear our form in game

  23. }

  24. formNick = “”; //just clean our variables

  25. formPassword = “”;

  26. }
复制代码