环境:
Unity2017.3.0f3
Mysql5.7.20
Wampserver64
在Wampserver64 的www目录下新建名为unity的文件夹。
1. unity3d通过get方式请求php
1、1Unity端的代码
using UnityEngine;
using System.Collections;
public class phpUnity1 : MonoBehaviour
{
private string url = "http://localhost/unity/phpunity1.php?id=1&cid=123456"; //带get参数id和cid的url
void OnGUI()
{
if (GUILayout.Button("get php"))
{
StartCoroutine(OnGet());
}
}
IEnumerator OnGet()
{
WWW www = new WWW(url);
yield return www;
if (www.error != null)
{
print("php请求错误: 代码为" + www.error);
}
else
{
print("php请求成功" + www.text);
}
}
}
1、2 php端代码(php名字为phpunity1.php)
<?php
if(isset($_GET["id"])
&& isset($_GET["cid"]))
{
echo "get请求成功,id值为:".$_GET["id"].",cid值为:".$_GET["cid"];
}
?>
结果:
2. unity3d通过post方式请求php.
2、1 Unity端的代码
using UnityEngine;
using System.Collections;
public class phpUnity2: MonoBehaviour
{
private string url = "http://localhost/unity/phpunity2.php"; //
void OnGUI()
{
if (GUILayout.Button("Post php"))
{
StartCoroutine(OnGet());
}
}
IEnumerator OnGet()
{
//表单
WWWForm form = new WWWForm();
form.AddField("id", 1);
form.AddField("cid", 123456);
WWW www = new WWW(url, form);
yield return www;
if (www.error != null)
{
print("php请求错误: 代码为" + www.error);
}
else
{
print("php请求成功" + www.text);
}
}
}
2、2 php端代码(php名字为 phpunity2.php)
<?php
if(isset