打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Unity3D Http通信之Warensoft通信库的使用
 在使用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方法,试验实例:登录验证)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. public partial class LoginCheck : System.Web.UI.Page
  8. {
  9.     protected void Page_Load(object sender, EventArgs e)
  10.     {
  11.         //结果
  12.         string response = "null";
  13.         //验证POST参数
  14.         if (Request.Form.Count > 0)
  15.         {
  16.             //获取POST内容
  17.             string request = Request.Form[0];
  18.             //截取用户名密码
  19.             string user = request.Split(',')[0];
  20.             string pass = request.Split(',')[1];
  21.             //验证
  22.             if (user == "gm" && pass == "123456")
  23.                 response = "1";     //验证正确返回用户ID
  24.             else
  25.                 response = "0";     //用户名或密码错误
  26.         }

  27.         //输出结果
  28.         Response.Write(response);
  29.         Response.End();
  30.     }
  31. }
复制代码
2、GetRoleList.aspx  (用于测试Get方法获取String,试验实例:根据用户ID获取角色列表)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. public partial class GetRoleList : System.Web.UI.Page
  8. {
  9.     protected void Page_Load(object sender, EventArgs e)
  10.     {
  11.         //角色列表
  12.         string roleList = "null";
  13.         //取得用户ID
  14.         string userID = Request.QueryString["UID"].ToString();

  15.         if (userID == "1")
  16.         {
  17.             roleList = "GM01,男,战士,99;9RIA,女,法师,1";
  18.         }

  19.         //输出结果
  20.         Response.Write(roleList);
  21.         Response.End();
  22.     }
  23. }
复制代码
3、ServerList.xml   (用于测试Get方法获取XML文档,试验实例:获取服务器列表)
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Servers>
  3.   <Server ip="192.168.1.1" port="8888" type="电信">青云山</Server>
  4.   <Server ip="192.168.1.2" port="8888" type="网通">须弥山</Server>
  5.   <Server ip="192.168.1.3" port="8888" type="移动">焚香谷</Server>
  6.   <Server ip="192.168.1.4" port="8888" type="双线">狐岐山</Server>
  7. </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地址)
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using Warensoft.Unity.Communication.Client;

  5. public class Login : MonoBehaviour
  6. {
  7.         //账号
  8.         private string _user;
  9.         //密码
  10.         private string _pass;
  11.         //用户ID
  12.         private string _userID;
  13.        
  14.         void Start ()
  15.     {
  16.                 //初始化数据
  17.         _user = "";
  18.                 _pass = "";
  19.                 _userID = "0";
  20.         }

  21.     void OnGUI()
  22.     {
  23.                 //绘制界面
  24.                 GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 50, 20), "User");
  25.                 _user = GUI.TextField(new Rect(Screen.width / 2 + 50, Screen.height / 2, 100, 20), _user, 20);
  26.                 GUI.Label(new Rect(Screen.width / 2, Screen.height / 2 + 30, 50, 20), "Pass");
  27.                 _pass = GUI.PasswordField(new Rect(Screen.width / 2 + 50, Screen.height / 2 + 30, 100, 20), _pass, '*', 20);
  28.                
  29.                 //进入游戏按钮
  30.                 if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 2 + 60, 150, 20), "Enter Game"))
  31.                 {
  32.                         //post参数是以byte[]形式发送,所以要将字符串转为byte[]
  33.                         byte[] postParms = System.Text.Encoding.UTF8.GetBytes(_user + "," + _pass);
  34.                        
  35.                         //获取UnityHttpClient实例
  36.                         UnityHttpClient httpClient = UnityCommunicationManager.CreateInstance().GetHttpClient();
  37.                        
  38.                         //开始以POST方式提交数据
  39.                         httpClient.BeginPost("http://localhost:14069/LoginCheck.aspx", postParms, (response)=>{
  40.                                 //数据返回,回调函数
  41.                                 _userID = response.StringContent;
  42.                                 if (_userID == "null")
  43.                                         Debug.Log("Error");
  44.                                 else
  45.                                 {
  46.                                         if (_userID == "0")
  47.                                                 Debug.Log("User or Pass is wrong");
  48.                                         else
  49.                                                 Debug.Log("Login Successed.UserID is " + _userID);
  50.                                 }
  51.                         });
  52.                 }
  53.                
  54.                 //获取服务器列表按钮
  55.                 if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 2 + 90, 150, 20), "Get Server"))
  56.                 {
  57.                         //获取UnityHttpClient实例
  58.                         UnityHttpClient httpClient = UnityCommunicationManager.CreateInstance().GetHttpClient();
  59.                        
  60.                         //开始以GET方式提交数据(返回结果类型为XmlDocument)
  61.                         httpClient.BeginGetHttpContent("http://localhost:14069/ServerList.xml", new Action<System.Xml.XmlDocument>((doc)=>{
  62.                                 Debug.Log(doc.OuterXml);
  63.                         }));
  64.                 }
  65.                
  66.                 //获取角色列表按钮
  67.                 if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 2 + 120, 150, 20), "Get RoleList"))
  68.                 {
  69.                         //获取UnityHttpClient实例
  70.                         UnityHttpClient httpClient = UnityCommunicationManager.CreateInstance().GetHttpClient();
  71.                        
  72.                         //开始以Get方式提交数据(返回结果类型为string)
  73.                         httpClient.BeginGetHttpContent("http://localhost:14069/GetRoleList.aspx?UID=" + _userID, new Action<string>((list)=>{
  74.                                 Debug.Log(list);
  75.                         }));
  76.                 }
  77.     }
  78. }
复制代码

四、测试脚本
1、将Login.cs脚本拖动到主相机上
2、在User框中输入"gm",在密码框中输入"123456"
3、依次点击"Enter Game","Get Server","Get RoleList"三个按钮
4、在Console视图中查看结果


----------------------------------------------------------------- 我是分割线 End --------------------------------------------------------------------

OK,赶快去试试吧~

有时间的话会再写个进阶篇,关于交互数据的处理(JSON)
吐槽一下:9RIA的代码编辑器真不敢恭维啊,各种没有缩进,各种没有高亮显示,严重影响阅读啊,有木有啊,求加强!
嗯。。暂时就这样了,祝学习愉快!


附件回复可见,哈哈,跟某兄学的。既能活跃气氛,又能赚银子,一举两得!
好吧 5秒回复带走

游客,如果您要查看本帖隐藏内容请回复

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C++也能实现组件化和响应式开发!Klee案例分享
python可视图matplotlib 2D/3D图表,让你快速掌握制作常用的图表
【Unity面试篇】Unity 面试题总结甄选 |C#基础篇 | ❤️持续更新❤️
Expression Blend实例中文教程(2) - 界面快速入门
初学iOS6 中的Core Image技术 | Ray Wenderlich
Win10开发入门:UWP通用应用程序项目结构分析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服