您可以通过 (C# + Unity) 中的三件事来实现数据发送/接收功能。它的Co-routine、WWW、WWWForm
以下两者的演示:
获取请求:
using UnityEngine;
public class GetRequestDEmo : MonoBehaviour {
void Start () {
string url = "http://exampleWeb.com/myServerScript.php?var1=value2&var2=value2";
WWW www = new WWW(url);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Result!: " + www.text);// contains all the data sent from the server
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
}
对于发布请求:
public class PostRequestDemo : MonoBehaviour {
void Start () {
string url = "http://exampleWeb.com/myServerscript.php";
WWWForm form = new WWWForm();
form.AddField("var1", "value1");
form.AddField("var2", "value2");
WWW www = new WWW(url, form);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.text);// contains all the data sent from the server
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
}
如果你也想要服务器端代码(PHP),我会推荐你this Unity Wiki code。或者你也可以观看this Video.