unity3d和php后台简单交互

这篇博客介绍了如何在Unity2017.3.0f3环境下,使用GET和POST方式与PHP进行数据交互。通过Wampserver64和Mysql5.7.20,作者展示了在www目录下创建unity文件夹,并提供了dbconfig.php和login.php的代码示例,用于数据库操作和登录功能。最后提到,实际开发中通常会用XML或JSON进行更复杂的数据交换。
摘要由CSDN通过智能技术生成

环境:
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值