查看: 1437|回复: 2
打印 上一主题 下一主题

Warensoft Unity3D通信库使用向导2

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2012-6-19 09:10:23 |只看该作者 |倒序浏览
Warensoft unity3d通信库使用向导2-利用UnityHttpClient类实现Http通信


用Warensoft.Unity.Communication.Client.UnityHttpClient类可以实现Http Get请求以及POST请求,并可以使用简单的获取本次请求所对对应的响应。可以使用该类完全代替内置的WWW类。



推荐使用UnityHttpClient替代WWW的原因有以下几点:



               1>WWW类的使用不符合微软命名规范



               2>大量并发使用WWW类时会抛出Too Many Threads的异常.UnityHttpClient已经在内部对并发线程数量进行了控制.




使用UnityHttpClient类,代码更加简洁



下面通过代码展示如何使用UnityHttpClient类:



下载Warensoft Unity3D通信库

该类库是Codeplex上的开源项目,地址是:http://wucl.codeplex.com,从上面下载类库的最新版本,下载后得到两个DLL文件,其中Warensoft.Unity.Communication.dll就是我们在本Demo中用的DLL文件,另外一个Warensoft.DataService.dll是数据服务库,在后面的章节中会讲解如何使用.


2.为了客户端代码能成功演示,下面在VisualStudio中建立一个网站,当做Http服务器,为了演示UnityHttpClient对文件的请求,需要在网站中添加一个名为Test.xml的XML文件,以及一个test.png的PNG图,如下图所示:














3.为了演示通过Get方式请求数据,需要在网站中添加一个GetTest.aspx页面,其对应的ASPX.CS中的代码如下所示:



using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient; public partial class GetTest : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){//该代码功能是通过查询字符串CustomerID的值去查询该客户对应的公司名,//并将查找到的公司返回给客户端string customerid = this.Request.QueryString["CustomerID"];if (customerid!=null ){string companyName = "";using (SqlConnection con=new SqlConnection ("server=.;database=northwind;uid=sa;pwd=sa")){var cmd = con.CreateCommand();cmd.CommandText = "select companyname from customers where customerid=@customerid";cmd.Parameters.AddWithValue("@customerid",customerid);con.Open();var result = cmd.ExecuteScalar();if (result !=null ){companyName = result.ToString();}}this.Response.Write(companyName);this.Response.End();}}}





4.为了演示通过POST方式发送数据,添加一个PostTest.aspx,其对应的CS文件代码如下所示:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient; public partial class PostTest : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){//该代码功能是通过获取客户端以Post方式发送的CustomerID的值去查询该客户//对应的公司名,并将查找到的公司返回给客户端if (this.Request .ContentLength!=0){//获取POST的数据流var strm = this.Request.InputStream;//建立缓冲区byte[]buffer=new byte[this.Request .ContentLength];//将POST过来的数据读取出来,并且存储在buffer中strm.Read(buffer,0,buffer .Length );//将二进制数据转化为字符串string customerid = System.Text.Encoding.UTF8.GetString(buffer);string companyName = "";using (SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa")){var cmd = con.CreateCommand();cmd.CommandText = "select companyname from customers where customerid=@customerid";cmd.Parameters.AddWithValue("@customerid", customerid);con.Open();var result = cmd.ExecuteScalar();if (result != null){companyName = result.ToString();}}this.Response.Write(companyName);this.Response.End();}}}



5.在Unity3D中建立一个新的项目,建立一个文件夹改名为Plugins,并将Warensoft.Unity.Communication.dll拷贝到该文件夹下面,如下图所示:





6.在Project中添加一个CS脚本,并将其命名为HttpTest.cs,其代码如下所示:



using UnityEngine;using System.Collections;using Warensoft.Unity.Communication.Client;using System;using System.Xml; public class HttpTest : MonoBehaviour { UnityHttpClient httpClient = null;private Texture2D image;    void Start () {      //启动时将httpClient初始化this.httpClient = UnityCommunicationManager.CreateInstance().GetHttpClient();this.httpClient.Error += new EventHandler<HttpRequestErrorEventArgs>(httpClient_Error);    } void httpClient_Error(object sender, HttpRequestErrorEventArgs e){print(e.ResponseText );}int initStep = 0;    void Update () {if (this.initStep ==0&&this.httpClient!=null ){ //获取XML文件this.httpClient.BeginGetHttpContent("http://localhost:17737/test11/test.xml",//后面的方法是本次异步Http请求成功并响应后的回调方法new Action<XmlDocument>((doc) =>{//打印XML文件print("response xml content:");print(doc.OuterXml);}));//获取图片this.httpClient.BeginGetHttpContent("http://localhost:17737/test11/test.png",//后面的方法是本次异步Http请求成功并响应后的回调方法new Action<Texture2D>((img) =>{this.image = img;}));//获取纯文本//通过客户ID查询公司名//GET方式this.httpClient.BeginGetHttpContent("http://localhost:17737/test11/GetTest.aspx?CustomerID=ALFKI",//后面的方法是本次异步Http请求成功并响应后的回调方法new Action<string>((stringResult) =>{//打印公司名print("Get the company name of alfki:" + stringResult);}));//获取纯文本//通过客户ID查询公司名//POST方式byte[] contentBuffer = System.Text.Encoding.UTF8.GetBytes("ALFKI");this.httpClient.BeginPost("http://localhost:17737/test11/PostTest.aspx", contentBuffer,(response) =>{//打印公司名print("ost the company name of alfki:" + response.StringContent);});this.initStep = 1;}     }void OnGUI(){if (this.image !=null ){GUI.DrawTexture(new Rect (0,0,this.image .width,this.image.height),this.image);}}}



7.将该cs文件拖放到该场景的主摄像机上,如下图所示:






分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

沙发
发表于 2012-6-19 09:46:42 |只看该作者
Warensoft Unity3D通信库使用向导1   

Warensoft Unity3D通信库使用向导5


Warensoft Unity3D通信库使用向导4


  


Warensoft Unity3D通信库使用向导3




Warensoft Unity3D通信库使用向导2
回复

使用道具 举报

.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

板凳
发表于 2012-12-6 09:51:51 |只看该作者
楼主的帖子很棒哦!果断推荐一下,很喜欢哦!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-20 15:32 , Processed in 0.083956 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部