unity 简单使用protobuf

研究了下protobuf    用我的理解 解释下 protobuf是和json,xml 一样的存储数据的, 而它比json等小 在传送数据时更轻  一堆白话 不去用百度上边的专业术语 让一些没接触的人 更方便理解

那么如何在untiy里面用呢 我就不用给你讲那么多操作了 直接给你现成的。 当然你也可以自己去git上下载,然后编译。

第一

        要在unity的Plugins文件夹下导入protobuf的库protobuf-net.dll 我下面的网盘地址提供了这个文件

http://pan.baidu.com/s/1gfePUKV

第二 

你要去学习protobuf 可以看看这篇文章      http://blog.csdn.net/csdn_cjt/article/details/75151019   主要看看标量数值类型和如何编写一个 .proto 文件   我这里给大家提供一个proto文件   PersonInfo.proto    网盘地址是  http://pan.baidu.com/s/1bpcWsJ9

第三

    要把这个PersonInfo.proto 转换成c#文件 需要Protogen.exe 我这里也提供给大家 这是问一个朋友Blue要的现成的 网盘地址是 http://pan.baidu.com/s/1eRC1LIU 然后把转换的.cs文件放到你的项目里(注意自己写个文件夹专门放这一类c# 就是.cs文件)

      这个过程的操作是 要用DOS命令       请注意看下面的图unity 简单使用protobuf_第1张图片

第四

        下面就是C#敲点代码的事情了

       首先你要搭建一个用protobuf序列化反序列化的类  ProtobufHelper.cs 就像下面这样

using System.Text;
using System.IO;
using ProtoBuf;

namespace Protobuf
{
    class ProtobufHelper
    {
        /// 
        /// 序列化成string
        /// 
        /// 
        /// 
        /// 返回字符串
        public static string Serialize(T t)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Serializer.Serialize(ms, t);

                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }

         /// 
         /// 将消息序列化为二进制的方法
         /// 
         /// 要序列化的对象
         public static  byte[] Serialize1(T t){
            using (MemoryStream ms = new MemoryStream())
            {
                Serializer.Serialize(ms, t);

                //定义二级制数组,保存序列化后的结果
                byte[] result = new byte[ms.Length];
                //将流的位置设为0,起始点
                ms.Position = 0;
                //将流中的内容读取到二进制数组中
                ms.Read (result, 0, result.Length);
                return result;
            }
         }

        /// 
        /// 序列化到文件
        /// 
        /// 
        /// 
        /// 
        public static void Serialize(string path, T data)
        {
            using (Stream file = File.Create(path))
            {
                Serializer.Serialize(file, data);
                file.Close();
            }
        }

        /// 
        /// 根据字符串反序列化成对象
        /// 
        /// 
        /// 
        /// 
        public static T DeSerialize(string content)
        {
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(content)))
            {
                T t = Serializer.Deserialize(ms);
                return t;
            }
        }

        /// 
        /// 根据文件流返回对象
        /// 
        /// 
        /// 
        /// 
        public static T DeSerialize(Stream filestream)
        {
            return Serializer.Deserialize(filestream);
        }


     

         

    }
}

当你要发送数据的时候 比如用socket给服务器发送数据的时候 

就可以这样用


ClientSocket.Send(ProtobufHelper.Serialize(new Person() { Name = this.PlayName, Gender = true, Msg = message }));

同样接受的消息用上面的ProtobufHelper里面的方法去反序列化服务器接受的信息

当然你自己搭建个socket或者http测试下


基本上研究了半天就弄了这么点东西 看了一些博客的东西借鉴了一些东西,看了一些操作。

谢谢先人的分享,我也走在分享的道路上






你可能感兴趣的:(unity3d,protobuf)