1、下载protobuf-net

2、创建Unity工程,创建一个Plugins文件夹,将protobuf-net解压把里面得protobuf-net放到Plugins

3、创建一个名为mcs的文本文件,里面写上-unsafe

4、重启Unity

5、编译自动生成cs代码工具

protogen.exe就是刚才生成的

6、编写.proto文件

message.proto里写入

message TeamCharacterOne
{
required uint64 CharacterId = 1;
required string CharacterName = 2;
required int32 RoleId = 3;
required int32 Level = 4;
required int32 Ladder = 5;
required int32 FightPoint = 6;
optional int32 QueueResult = 7;
}

  

7、 生成.cs代码

创建一个proto.bat文件文件

里面写入

@echo off
rem 查找文件
for /f "delims=" %%i in ('dir /b ".\*.proto"') do echo %%i
rem 转cpp for /f "delims=" %%i in ('dir /b/a "*.proto"') do protoc -I=. --cpp_out=. %%i
for /f "delims=" %%i in ('dir /b/a "*.proto"') do protogen -i:%%i -o:%%~ni.cs
pause

8、把代码放入Unity工程

9、写测试代码

using message;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization
void Start () {
var a = new TeamCharacterOne();
a.CharacterId = 10;
a.CharacterName = "fdsafd";
var b = Serialize(a); var data = Deserialize<TeamCharacterOne>(b);
Debug.Log(data.CharacterName);
} // Update is called once per frame
void Update () { } byte[] Serialize(object o)
{
using (MemoryStream ms = new MemoryStream())
{
ProtoBuf.Serializer.Serialize(ms, o);
byte[] result = new byte[ms.Length];
ms.Position = 0;
ms.Read(result, 0, result.Length); return result;
}
} T Deserialize<T>(byte[] b)
{
using (MemoryStream ms = new MemoryStream())
{ ms.Write(b, 0, b.Length);
ms.Position = 0;
return ProtoBuf.Serializer.Deserialize<T>(ms);
}
}
}

  

Unity3D protobuf-net使用方式的更多相关文章

  1. 在ios android设备上使用 Protobuf (使用dll方式)

    http://game.ceeger.com/forum/read.php?tid=13479 如果你的工程可以以.Net 2.0 subset模式运行,请看这个帖子中的方法. 地址:http://g ...

  2. (转)在ios android设备上使用 Protobuf (使用dll方式)

    自:http://game.ceeger.com/forum/read.php?tid=13479 如果你的工程可以以.Net 2.0 subset模式运行,请看这个帖子中的方法. 地址:http:/ ...

  3. 编写Unity3D着色器的三种方式

    不管你会不会写Unity3D的shader,估计你会知道,Unity3D编写shader有三种方式,这篇东西主要就是说一下这三种东西有什么区别,和大概是怎样用的. 先来列一下这三种方式: fixed ...

  4. 投影方式- Unity3D游戏开发培训

    投影方式- Unity3D游戏开发培训   作者:Jesai 2018-02-12 20:33:13 摘  要 透视投影是3D渲染的基本概念,也是3D程序设计的基础.掌握透视投影的原理对于深入理解其他 ...

  5. 【Unity3D基础】让物体动起来①--UGUI鼠标点击移动

    背景 首先还是先声明自己是比较笨的一个人,总是找不到高效的学习方法,目前自己学习Unity3D的方式主要是两种,一种是直接看高质量的源码,另一种是光看不行还要自己动手,自己写一些有代表性的小程序,这也 ...

  6. google protobuf ios开发使用

    简介: protobuf 即 google protocol buffer 是一种数据封装格式协议: 比如其他经常用的xml,json等格式:protobuf的优势是效率高,同样的一份数据使用prot ...

  7. Protocol Buffers(Protobuf)开发者指南---概览

    Protocol Buffers(Protobuf)开发者指南---概览 欢迎来到protocol buffers的开发者指南文档,protocol buffers是一个与编程语言无关‘.系统平台无关 ...

  8. protobuf 向前兼容向后兼容

    http://blog.163.com/jiang_tao_2010/blog/static/12112689020114305013458/ 不错的protobuf.. protobuf的编码方式: ...

  9. Linux下protobuf的编译与安装【各种奇葩问题】

    1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 2.1 ...

  10. Linux下protobuf的编译与安装

    1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 将下载 ...

随机推荐

  1. Fedora26 tftp-server设置

    安装tftp-server yum install -y  tftp-server 启动软件 systemctl start tftp.socket systemctl enable tftp.soc ...

  2. ttl传输中过期

    上renren时遇到一问题,突然间就无法登陆,看了下网络,正常呀,别的网站完全ok,就这不成,所以就ping了一下做以校验:如下图示:传输中过期ttl,这问题少见,新鲜呀:赶紧查了查:原来可能是产生了 ...

  3. 关于Struts2的文件上传

    要实现Struts2框架的文件上传,需要用到2个jar包 commons-fileupload-1.2.2.jar commons-io-2.0.1.jar 由于文件解析Struts2内部已经帮我们做 ...

  4. VS2008编译错误:error C2065: 'PMIB_TCPSTATS' : undeclared identifier c:\program files (x86)\microsoft sdks\windows\v7.0a\include\iphlpapi.h 411

    安装了VS2008编译之前的程序,结果出现了编译错误,以为是VS2008的Sp1补丁没装好,重装补丁后还是不行,编译错误如下: 双击错误会定位在iphlpapi.h中, 一个可行的解决办法是:把iph ...

  5. C#爬网页时“远程服务器返回错误: (403) 已禁止”解决方法

    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri); //这个一定要加上,在某些网站没有会发生"远程服务器返回错误: ...

  6. 关于 wsdl2Java 自动生成客户端调取webservice接口

    webservice地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl wsdl2Java 自动生成类名: 客户端调 ...

  7. jquery获取元素颜色css('color')的值返回RGB

    css代码如下: a, a:link, a:visited { color:#4188FB; } a:active, a:focus, a:hover { color:#FFCC00; } js代码如 ...

  8. Java实现高效的枚举元素集合

    Set是Java集合类的重要组成部分,它用来存储不能重复的对象.枚举类型也要求其枚举元素各不相同.看起来枚举类型和集合是很相似的.然而枚举类型中的元素不能随意的增加.删除,作为集合而言,枚举类型非常不 ...

  9. Linux wget 命令下载文件

    wget是Linux系统中用来下载文件的工具,其功能还是比较多的,能够下载单个文件,也可以分段下载,下面小编将针对wget命令的用法给大家做个实例介绍. 实例1 :下载单个文件 # wget http ...

  10. 【ArcGIS】WebAdaptorIIS 安装前准备及配置Portal For ArcGIS的问题解决

    1.计算机全名配置 2.IIS-服务器证书配置 3.端口绑定 备注:配置Portal For ArcGIS总会提示计算机域名.全名错误.完全限定域名,可能就是没有进行第一步操作 4.Portal目录