没有合适的资源?快使用搜索试试~ 我知道了~
首页C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
34 下载量 192 浏览量
更新于2023-03-03
评论 3
收藏 92KB PDF 举报
主要介绍了C#使用Protocol Buffer(ProtoBuf)进行Unity的Socket通信的实例,Protocol Buffer是Google开发的数据格式,也是除了XML和JSON之外人气第三高的^^需要的朋友可以参考下
资源详情
资源评论
资源推荐
C#使用使用Protocol Buffer(ProtoBuf)进行进行Unity中的中的Socket通信通信
主要介绍了C#使用Protocol Buffer(ProtoBuf)进行Unity的Socket通信的实例,Protocol Buffer是Google开发的数据
格式,也是除了XML和JSON之外人气第三高的^^需要的朋友可以参考下
首先来说一下本文中例子所要实现的功能:
基于ProtoBuf序列化对象
使用Socket实现时时通信
数据包的编码和解码
下面来看具体的步骤:
一、一、Unity中使用中使用ProtoBuf
导入DLL到Unity中,
创建网络传输的模型类:
using System;
using ProtoBuf;
//添加特性,表示可以被ProtoBuf工具序列化
[ProtoContract]
public class NetModel {
//添加特性,表示该字段可以被序列化,1可以理解为下标
[ProtoMember(1)]
public int ID;
[ProtoMember(2)]
public string Commit;
[ProtoMember(3)]
public string Message;
}
using System;
using ProtoBuf;
//添加特性,表示可以被ProtoBuf工具序列化
[ProtoContract]
public class NetModel {
//添加特性,表示该字段可以被序列化,1可以理解为下标
[ProtoMember(1)]
public int ID;
[ProtoMember(2)]
public string Commit;
[ProtoMember(3)]
public string Message;
}
在Unity中添加测试脚本,介绍ProtoBuf工具的使用。
using System;
using System.IO;
public class Test : MonoBehaviour {
void Start () {
//创建对象
NetModel item = new NetModel(){ID = 1, Commit = "LanOu", Message = "Unity"};
//序列化对象
byte[] temp = Serialize(item);
//ProtoBuf的优势一:小
Debug.Log(temp.Length);
//反序列化为对象
NetModel result = DeSerialize(temp);
Debug.Log(result.Message);
}
// 将消息序列化为二进制的方法
// < param name="model">要序列化的对象< /param>
private byte[] Serialize(NetModel model)
{
try {
//涉及格式转换,需要用到流,将二进制序列化到流中
using (MemoryStream ms = new MemoryStream()) {
//使用ProtoBuf工具的序列化方法
ProtoBuf.Serializer.Serialize<NetModel> (ms, model);
//定义二级制数组,保存序列化后的结果
byte[] result = new byte[ms.Length];
//将流的位置设为0,起始点
ms.Position = 0;
//将流中的内容读取到二进制数组中
ms.Read (result, 0, result.Length);
return result;
}
} catch (Exception ex) {
Debug.Log ("序列化失败: " + ex.ToString());
return null;
}
}
// 将收到的消息反序列化成对象
// < returns>The serialize.< /returns>
// < param name="msg">收到的消息.</param>
private NetModel DeSerialize(byte[] msg)
{
try {
using (MemoryStream ms = new MemoryStream()) {
//将消息写入流中
ms.Write (msg, 0, msg.Length);
//将流的位置归0
ms.Position = 0;
//使用工具反序列化对象
NetModel result = ProtoBuf.Serializer.Deserialize<NetModel> (ms);
return result;
}
} catch (Exception ex) {
Debug.Log("反序列化失败: " + ex.ToString());
return null;
}
}
}
using System;
using System.IO;
public class Test : MonoBehaviour {
void Start () {
//创建对象
NetModel item = new NetModel(){ID = 1, Commit = "LanOu", Message = "Unity"};
//序列化对象
byte[] temp = Serialize(item);
//ProtoBuf的优势一:小
Debug.Log(temp.Length);
//反序列化为对象
NetModel result = DeSerialize(temp);
Debug.Log(result.Message);
}
// 将消息序列化为二进制的方法
// < param name="model">要序列化的对象< /param>
private byte[] Serialize(NetModel model)
{
try {
//涉及格式转换,需要用到流,将二进制序列化到流中
using (MemoryStream ms = new MemoryStream()) {
//使用ProtoBuf工具的序列化方法
ProtoBuf.Serializer.Serialize<NetModel> (ms, model);
//定义二级制数组,保存序列化后的结果
byte[] result = new byte[ms.Length];
//将流的位置设为0,起始点
ms.Position = 0;
//将流中的内容读取到二进制数组中
ms.Read (result, 0, result.Length);
return result;
}
} catch (Exception ex) {
Debug.Log ("序列化失败: " + ex.ToString());
return null;
}
}
// 将收到的消息反序列化成对象
// < returns>The serialize.< /returns>
// < param name="msg">收到的消息.</param>
private NetModel DeSerialize(byte[] msg)
{
try {
using (MemoryStream ms = new MemoryStream()) {
//将消息写入流中
ms.Write (msg, 0, msg.Length);
//将流的位置归0
ms.Position = 0;
//使用工具反序列化对象
NetModel result = ProtoBuf.Serializer.Deserialize<NetModel> (ms);
return result;
}
} catch (Exception ex) {
Debug.Log("反序列化失败: " + ex.ToString());
return null;
}
}
}
二、二、Unity中使用中使用Socket实现时时通信实现时时通信
通信应该实现的功能:
服务器可以时时监听多个客户端
服务器可以时时监听某一个客户端消息
服务器可以时时给某一个客户端发消息
首先我们需要定义一个客户端对象
using System;
using System.Net.Sockets;
// 表示一个客户端
public class NetUserToken {
//连接客户端的Socket
public Socket socket;
//用于存放接收数据
public byte[] buffer;
public NetUserToken()
{
buffer = new byte[1024];
}
// 接受消息
// < param name="data">Data.< /param>
public void Receive(byte[] data)
{
UnityEngine.Debug.Log("接收到消息!");
}
// 发送消息
//< param name="data">Data.< /param>
public void Send(byte[] data)
{
}
}
using System;
using System.Net.Sockets;
// 表示一个客户端
public class NetUserToken {
//连接客户端的Socket
public Socket socket;
//用于存放接收数据
public byte[] buffer;
public NetUserToken()
{
buffer = new byte[1024];
}
// 接受消息
// < param name="data">Data.< /param>
public void Receive(byte[] data)
{
UnityEngine.Debug.Log("接收到消息!");
}
剩余10页未读,继续阅读
weixin_38531788
- 粉丝: 4
- 资源: 914
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0