unity3d lua protobufprotobuf 有丢包现象么

&>&&>&&>&&>&unity3d&Protobuf&Socket
unity3d&Protobuf&Socket
上传大小:12.4MB
unity3d应用Protobuf实现Socket通信
综合评分:4.4(46位用户评分)
所需积分:4
下载次数:225
审核通过送C币
创建者:hubing
创建者:jin80506
创建者:beau_lily
课程推荐相关知识库
上传者其他资源上传者专辑
开发技术热门标签
VIP会员动态
android服务器底层网络模块的设计方法
所需积分:0
剩余积分:720
您当前C币:0
可兑换下载积分:0
兑换下载分:
兑换失败,您当前C币不够,请先充值C币
消耗C币:0
你当前的下载分为234。
unity3d&Protobuf&Socket
会员到期时间:
剩余下载次数:
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
cloudstlife
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:Unity3D使用TCP/IP协议,传递protocol buffer消息protobuf-net - ing... - 推酷
Unity3D使用TCP/IP协议,传递protocol buffer消息protobuf-net - ing...
原文:http://my.oschina.net/faint/blog/296785
第一部分 dll
1 下面大多数内容,都是使用c#编译的dll来实现的。
2 编译为dll后,要拖放到unity3d的Assets里面,才能using到。
3 有以下类似错误,就是使用了非.net 2.0编译的dll。注意项目必须是在.net 2.0版本编译的才能正常在unity3d当中使用。
Unhandled Exception: System.TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'MyModel'
4 应该不能用MonoDevelop编译下面会提到的Serializer部分(编译不出dll,会报错)。需用vs编译。
第二部分 tcp/ip
System.IO;
System.Net.S
TcpConnector{
Connector{
HEAD_SIZE = 4;
TcpClient(ip,port);
stream = client.GetStream();
Disconnect(){
stream.Close();
client.Close();
readType(){
[] headData =
[HEAD_SIZE];
stream.Read(headData,0,headData.Length);
msgType = BitConverter.ToInt32(headData,0);
readSize(){
[] headData =
[HEAD_SIZE];
stream.Read(headData,0,headData.Length);
msgSize = BitConverter.ToInt32(headData,0);
[] readContent(
[] content =
stream.Read(content,0,content.Length);
Msg Read(){
msg.Type = readType();
msg.Size = readSize();
(msg.Size & 0) {
msg.Content = readContent(msg.Size);
[] msgContent){
[] msgTypeByte = BitConverter.GetBytes(msgType);
msgSize = HEAD_SIZE+HEAD_SIZE+msgContent.L
[] msgSizeByte = BitConverter.GetBytes(msgSize);
totalSize = HEAD_SIZE+HEAD_SIZE+msgS
[] msgByte =
[totalSize];
index = 0;
(i=0;i&HEAD_SIZE;i++){
// put msg type
(msgTypeByte.Length&i){
msgByte[index] = msgTypeByte[i];
(i=0;i&HEAD_SIZE;i++){
// put msg size
(msgTypeByte.Length&i){
msgByte[index+i] = msgSizeByte[i];
(i=0;i&msgSi++){
// put msg content
(msgTypeByte.Length&i){
msgByte[index+i] = msgContent[i];
stream.Write(msgByte,0,msgByte.Length);
stream.Flush();
主要用的是TcpClient,NetworkStream,BitConverter.
TcpClient client =
TcpClient(ip,port);
// 获取与服务器连接
NetworkStream stream = client.GetStream();
// 获取连接的流
stream.Read(buf,0,lenght);
// 读取至buf
stream.Write(buf,0,lenght);
// 写至buf
BitConverter.GetBytes(data);
// 用于将整数转为字节
BitConverter.ToInt32(data,0);
// 用于将字节转为整数
stream.Flush();
// 将流中缓存发出,而不等候
stream.Close();
client.Close();
// 关闭连接
第三部分 protobuf-net
FQ下载安装:
数据结构编译成dll:
先新建解决方案,新建库,添加下载的full/unity/dll。具体代码如下:
CSProtoData
[ProtoContract]
[ProtoMember(1)]
Int32 DataType {
[ProtoMember(2)]
Int64 DataDate {
[ProtoMember(3)]
[] DataContent {
[ProtoContract]
[ProtoMember(1)]
Int32 Index {
[ProtoMember(2)]
Int64 Value {
[ProtoMember(1)]
Int64 Rank {
[ProtoMember(2)]
TargetName {
[ProtoMember(3)]
Int64 Number {
[ProtoMember(1)]
DataType {
[ProtoMember(2)]
Int64 DataDate {
[ProtoMember(3)]
Int32 Start {
[ProtoMember(4)]
Int32 End {
编译完后,生成dll下面马上用到(同时也要拖放到unity/assets下)。
第三部分 下
因为protobuf-net的序列化和反序列化用的是jit,ios不支持jit,所以需采用编译成dll的方式来解决问题:
vs中,新建命令行程序,添加protobuf-net/full/unity/dll,添加刚生成的dll,代码如下:
ProtoSerializer
model = ProtoBuf.Meta.TypeModel.Create();
model.Add(
model.Add(
model.Add(
model.Add(
(Request),
&CSProtoSerializer&
&CSProtoSerializer.dll&
这里按运行后,会在目录下生成:CSProtoSerializer.dll,一样拖放到unity/assets下。
其中typeof()的,就是proto数据类型,在上半部分有定义的内容。
第四部分 unity代码
执行完以上步骤,unity/assets下应该有这么几个dll:
protobuf-net/full/unity/dll
proto的data的dll(第三部分)
data的序列化的dll(第三部分下,运行后生成的那个)
还有用于tcp连接的dll(第二部分)
那么实际在unity当中调用的代码则是:
System.IO;
testTcp : MonoBehaviour {
// Use this for initialization
Start () {
Connector conn =
Connector();
result = conn.Connect(
&127.0.0.1&
Debug.Log(result);
Head head=
head.DataType = 2;
head.DataDate = ;
MemoryStream memStream =
MemoryStream();
ProtoBuf.Serializer.Serialize&CSProtoData.Head&(memStream, head);
[] x = memStream.ToArray();
conn.Write(1,x);
conn.Write(1,x);
// Update is called once per frame
Update () {
新建个script,随便挂在比如camara的组件里即可。
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
娄底职业学院 大专, 后参加湖南师大自考 本科,
要主从事虚拟仿真和三维动画工作
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
服务器端代码using Susing System.Collections.GponentMusing System.Dusing System.Dusing System.Lusing System.Tusing System.Windows.Fusing System.Nusing System.Net.Susing System.IO;using System.Tusing ProtoBusing ProtoBuf.Musing ClientMusing ServerMnamespace GameServer{& & public partial class Form1 : Form& & {& & & & public Form1()& & & & {& & & & & & InitializeComponent();& & & & }& & & & private void Form1_Load(object sender, EventArgs e)& & & & {& & & & & & TcpServer();& & & & }& & & & private static byte[]& & & & private const int port = 8885;& & & & private Socket serverS& & & & private Socket clientS& & & & public void TcpServer()& & & & {& & & & & & IPAddress ip = IPAddress.Parse("127.0.0.1");& & & & & & textBox1.Text += "本机IP地址:" +& & & & & & serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);& & & & & & serverSocket.Bind(new IPEndPoint(ip, port));& & & & & & serverSocket.Listen(10);&& & & & & & textBox1.Text += "开始监听端口:" +& & & & & & Thread threadAccept = new Thread(AcceptClientConnect);& & & & & & threadAccept.Start();& & & & }& & & & private void AcceptClientConnect()& & & & {& & & & & & clientSocket = serverSocket.Accept();& & & & & &&& & & & & & if (clientSocket != null)& & & & & & {& & & & & & & & SignUpResponse s = new SignUpResponse();& & & & & & & & s.errorCode = 0;& & & & & & & & s.version = 1;& & & & & & & & MemoryStream ms = new MemoryStream();& & & & & & & & ProtoBuf.Serializer.Serialize&SignUpResponse&(ms, s);& & & & & & & & result = ms.ToArray();& & & & & & & & ms.Close();& & & & & & & & clientSocket.Send(result,result.Length,SocketFlags.None);& & & & & & & & textBox1.Text +="长度:"+ result.L& & & & & & }& & & & }& & & & private void textBox1_TextChanged(object sender, EventArgs e)& & & & {& & & & }& & }}proto协议&package ServerMmessage SignUpResponse{ optional int32 errorCode = 1; optional float version = 2;}
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'Unity3D传递ProtoBuf-net消息测试(转:AWNUXCVBN)',
blogAbstract:'proto协议客户端代码using UnityEusing System.Cusing System.Net.Susing System.IO;using System.Tusing Susing ProtoBusing System.Nusing ProtoBuf.Musing ClientMusing ServerMpublic class Client : MonoBehaviour{
private byte[] result = new byte[1024];
// Use this for initialization
void Start()
IPAddress ip = IPAddress.Parse(\"127.0.0.1\");',
blogTag:'',
blogUrl:'blog/static/85',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:5,
permalink:'blog/static/85',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'娄底职业学院 大专, 后参加湖南师大自考 本科,\n要主从事虚拟仿真和三维动画工作',
hmcon:'-1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}unity中使用protobuf(兼容ios平台) - 博客频道 - CSDN.NET
代码敲了10年,unity用了6年.标准程序猿,迷途小码农...
分类:unity
排名:千里之外
(14)(0)(4)(0)(0)(0)}

我要回帖

更多关于 unity3d protobuf3.x 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信