在使用Http通信的时候,发现Unity自带的WWW不是非常好用,为什么呢?一、局限性比较大,使用不方便
二、WWW类的使用不符合微软的命名规范
三、在大量并发使用WWW类会发生"Too Many Threads"的异常
于是找到传说中的Warensoft通信库,它完全没有上述问题,使用起来非常方便高效~
现在教大家一下使用的方法,非常简单~
Warensoft通信库是一个很强大的库(Made In China,赞一个!),它包括了数据库访问、Socket通信和Http通信等常用通信模块,当然今天我们要介绍的正主是它的Http通信部分...
(库DLL文件的下载请看附件)
(更多介绍请访问作者博客:http://www.cnblogs.com/warensoft/)
好了 讲了一大堆废话 进入正题
----------------------------------------------------------------- 我是分割线 Begin --------------------------------------------------------------------
一、你需要
1、一个IIS或者Apache等任何可以运行用来动态网页的东东(本例中使用Visual Studio 2012, .Net(C#)网站作为网页端进行测试)
2、Unity3D (本例中使用的是4.0版本)
3、Warensoft.Unity.Communication.Client.dll(Warensoft通信库DLL,下载请看附件)
二、网页端
1、
LoginCheck.aspx (用于测试Post方法,试验实例:登录验证)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class LoginCheck : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- //结果
- string response = "null";
- //验证POST参数
- if (Request.Form.Count > 0)
- {
- //获取POST内容
- string request = Request.Form[0];
- //截取用户名密码
- string user = request.Split(',')[0];
- string pass = request.Split(',')[1];
- //验证
- if (user == "gm" && pass == "123456")
- response = "1"; //验证正确返回用户ID
- else
- response = "0"; //用户名或密码错误
- }
- //输出结果
- Response.Write(response);
- Response.End();
- }
- }
复制代码2、
GetRoleList.aspx (用于测试Get方法获取String,试验实例:根据用户ID获取角色列表)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class GetRoleList : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- //角色列表
- string roleList = "null";
- //取得用户ID
- string userID = Request.QueryString["UID"].ToString();
- if (userID == "1")
- {
- roleList = "GM01,男,战士,99;9RIA,女,法师,1";
- }
- //输出结果
- Response.Write(roleList);
- Response.End();
- }
- }
复制代码3、
ServerList.xml (用于测试Get方法获取XML文档,试验实例:获取服务器列表)
- <?xml version="1.0" encoding="utf-8" ?>
- <Servers>
- <Server ip="192.168.1.1" port="8888" type="电信">青云山</Server>
- <Server ip="192.168.1.2" port="8888" type="网通">须弥山</Server>
- <Server ip="192.168.1.3" port="8888" type="移动">焚香谷</Server>
- <Server ip="192.168.1.4" port="8888" type="双线">狐岐山</Server>
- </Servers>
复制代码三、Unity3D
1、在Project视图的
Asset根目录右键Import New Asset...,选择下载到的"Warensoft.Unity.Communication.Client.dll"文件,将其导入到项目中
2、在Asset目录视图中右键Create->C# Script,取名为Login
3、打开Login.cs,在MonoDevelop视图左边找到Solution解决方案视图,展开References,你会发现,Warensoft.Unity.Communication.Client.dll已经自动被引用进来了
4、
Login.cs (编译的时候可能会有编码格式问题,如果Unity编译出错,请删掉所有中文注释;将代码中的“
http://localhost:14069/”替换成你自己的WEB地址)
- using UnityEngine;
- using System;
- using System.Collections;
- using Warensoft.Unity.Communication.Client;
- public class Login : MonoBehaviour
- {
- //账号
- private string _user;
- //密码
- private string _pass;
- //用户ID
- private string _userID;
-
- void Start ()
- {
- //初始化数据
- _user = "";
- _pass = "";
- _userID = "0";
- }
- void OnGUI()
- {
- //绘制界面
- GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 50, 20), "User");
- _user = GUI.TextField(new Rect(Screen.width / 2 + 50, Screen.height / 2, 100, 20), _user, 20);
- GUI.Label(new Rect(Screen.width / 2, Screen.height / 2 + 30, 50, 20), "Pass");
- _pass = GUI.PasswordField(new Rect(Screen.width / 2 + 50, Screen.height / 2 + 30, 100, 20), _pass, '*', 20);
-
- //进入游戏按钮
- if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 2 + 60, 150, 20), "Enter Game"))
- {
- //post参数是以byte[]形式发送,所以要将字符串转为byte[]
- byte[] postParms = System.Text.Encoding.UTF8.GetBytes(_user + "," + _pass);
-
- //获取UnityHttpClient实例
- UnityHttpClient httpClient = UnityCommunicationManager.CreateInstance().GetHttpClient();
-
- //开始以POST方式提交数据
- httpClient.BeginPost("http://localhost:14069/LoginCheck.aspx", postParms, (response)=>{
- //数据返回,回调函数
- _userID = response.StringContent;
- if (_userID == "null")
- Debug.Log("Error");
- else
- {
- if (_userID == "0")
- Debug.Log("User or Pass is wrong");
- else
- Debug.Log("Login Successed.UserID is " + _userID);
- }
- });
- }
-
- //获取服务器列表按钮
- if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 2 + 90, 150, 20), "Get Server"))
- {
- //获取UnityHttpClient实例
- UnityHttpClient httpClient = UnityCommunicationManager.CreateInstance().GetHttpClient();
-
- //开始以GET方式提交数据(返回结果类型为XmlDocument)
- httpClient.BeginGetHttpContent("http://localhost:14069/ServerList.xml", new Action<System.Xml.XmlDocument>((doc)=>{
- Debug.Log(doc.OuterXml);
- }));
- }
-
- //获取角色列表按钮
- if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 2 + 120, 150, 20), "Get RoleList"))
- {
- //获取UnityHttpClient实例
- UnityHttpClient httpClient = UnityCommunicationManager.CreateInstance().GetHttpClient();
-
- //开始以Get方式提交数据(返回结果类型为string)
- httpClient.BeginGetHttpContent("http://localhost:14069/GetRoleList.aspx?UID=" + _userID, new Action<string>((list)=>{
- Debug.Log(list);
- }));
- }
- }
- }
复制代码四、测试脚本
1、将Login.cs脚本拖动到主相机上
2、在User框中输入"gm",在密码框中输入"123456"
3、依次点击"Enter Game","Get Server","Get RoleList"三个按钮
4、在Console视图中查看结果
----------------------------------------------------------------- 我是分割线 End --------------------------------------------------------------------
OK,赶快去试试吧~
有时间的话会再写个进阶篇,关于交互数据的处理(JSON)
吐槽一下:9RIA的代码编辑器真不敢恭维啊,各种没有缩进,各种没有高亮显示,严重影响阅读啊,有木有啊,求加强!
嗯。。暂时就这样了,祝学习愉快!
附件回复可见,哈哈,跟某兄学的。既能活跃气氛,又能赚银子,一举两得!
好吧 5秒回复带走
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。