unity中的http通信
来源:互联网 发布:域名被恶意投诉举报 编辑:程序博客网 时间:2024/05/21 22:44
前言
Unity3d 是一个跨平台的引擎,在移动互联网浪潮之中,尤其是移动游戏3d方向上,Unity3d 毫无疑问是最耀眼的明星。Unity3d的http通信很简单,也非常好用,下面通过http和php sever交互来简单介绍一下。
http 提交数据原理
http 协议通过 url来获取和提交数据 。提交数据的方式 有两种,一种是get方法,一种是post方法。get一般用于告诉服务器把满足参数的数据发送给回来。
例如:get 的html代码如下:
- <form action="search.php" method ="GET">
- <username:<inputtypeinputtype="text"name="user"/><br>
- <password:<inputtypeinputtype="password "name="pwd"/><br>
- <input type="submit"value="login"/>
- </form >
post一般是将数据发送给服务器,服务器将这些数据进行处理,比如说存储到数据库。
例如:post的html 代码如下:
- <form action="login.php" method ="POST" >
- <username:<inputtypeinputtype="text"name="user"/><br>
- <password:<inputtypeinputtype="password "name="pwd"/><br>
- <input type="submit"value="login"/>
- </form >
其实区别就是提交的方式不一样,点击login按钮后,浏览器地址栏里分别显示如下:
get方法url为:http://127.0.0.1/serach.php?user=hortor&pwd=123
post方法url为:http://127.0.0.1
PHP服务端接收数据方法
服务端针对这两种方式响应的有两种方式:
get方法接收数据的方法为 $_GET[user],来接收客户端发送的user的值。
post方法接收数据的方法为 $_POST[user]也是用来接收客户端变量user的值。
UNITY WWW类使用方法
unity www 类同样对应两种提交数据的方式,常用的两个构造函数为:
static function WWW (url : String) : WWW
static function WWW (url : String, form : WWWForm) : WWW
第一个函数是通过get方法向服务器发送数据,第二个是用post提交数据,其中WWWForm有个方法叫:AddField(agr : string, value : string),来添加参数。
get例子:
- #pragma strict
- privatevar url : String = "127.0.0.1/login.php?user=test&pwd=123";
- function Start () {
- var getData : WWW = WWW(url);
- yield getData;
- if(getData.error != null) {
- Debug.Log(getData.error);
- }
- else {
- Debug.Log(getData.text);
- }
- }
post例子:
- #pragma strict
- privatevar url : String = "127.0.0.1/login.php";
- function Start () {
- var form : WWWForm = new WWWForm();
- form.AddField("user", "test");
- form.AddField("pwd", "123");
- var getData : WWW = WWW(url, form);
- yield getData;
- if(getData.error != null) {
- Debug.Log(getData.error);
- }
- else {
- Debug.Log(getData.text);
- }
- }
其中yield 这个方法是程序每一帧调用一次,当getData 执行完成,返回结果,这就相当于是异步请求数据。WWW 类有几个常用的静态变量,分别为:
1、WWW.text 返回从网页得到数据,类型为string。
2、WWW.error 返回错误信息,比如说超时,网络连接错误等问题。
- unity中的http通信
- [Unity3d]unity中http通信
- Unity网络通信之HTTP
- Unity的http通信--unity与python的django通信
- Unity的http通信--unity与python的django通信
- unity3d中的http通信
- unity3d中的http通信
- unity3d中的http通信
- unity3d中的http通信
- unity3d中的http通信
- unity3d中的http通信
- Unity3D中的http通信
- unity3d中的http通信
- Android中的Http通信
- Android中的Http通信
- Android中的Http通信
- Android中的http通信
- Java中的HTTP通信
- eclipse启动时卡住org.eclipse.jdt.debug
- zoj 3497 Mistwald 矩阵快速幂
- STM32F3的ADC使用DMA模式传输转换数据
- listview
- 双击实体弹出对话框(重载AcDbDoubleClickEdit)
- unity中的http通信
- MyEclipse 2015优化技巧
- MVC缓存依赖:文件依赖
- 详谈 Unity3D 利用 Webservice 读取数据库
- 自动重置事件+临界区实现线程间同步
- 安卓基础 论MediaPlayer的应用(一)
- iOS HTTP Range 使用
- BaseAdapter解决OOM问题
- Unity网络多玩家游戏开发教程第1章Unity自带网络功能