Unity手游之路<一>C#版本Protobuf

一个游戏包含了各种数据,包括本地数据和与服务端通信的数据。今天我们来谈谈如何存储数据,以及客户端和服务端的编码方式。根据以前的经验,我们可以用字符串,XML,json...甚至可以直接存储二进制。各种方式都有各自的优劣,有些性能比较好,但是实现方式比较麻烦。有些数据冗余太多。
  • 简介
今天我们来学习一种广泛使用的数据格式:Protobuf。简单来说,它就是一种二进制格式,是google发起的,目前广泛应用在各种开发语言中。具体的介绍可以参见:https://code.google.com/p/protobuf/ 。我们之所以选择protobuf,是基于它的高效,数据冗余少,编程简单等特性。关于C#的protobuf实现,网上有好几个版本,公认比较好的是Protobuf-net。

  • 实例
先来看一个最简单的例子:把一个类用Protobuf格式序列化到一个二进制文件。再读取二进制数据,反序列化出对象数据。

从网上参考了一个例子 http://blog.csdn.net/ddxkjddx/article/details/7239798

//----------------实体类----------------------

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using ProtoBuf;  
  4. using System;  
  5. using System.Collections.Generic;  
  6.   
  7.   
  8. [ProtoContract]  
  9. public class Test {  
  10.   
  11.   
  12.     [ProtoMember(1)]  
  13.     public int Id  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.   
  19.   
  20.     [ProtoMember(2)]  
  21.     public List<String> data  
  22.     {  
  23.         get;  
  24.         set;  
  25.     }  
  26.   
  27.   
  28.     public override string ToString()  
  29.     {  
  30.         String str = Id+":";  
  31.         foreach (String d in data)  
  32.         {  
  33.             str += d + ",";  
  34.         }  
  35.         return str;  
  36.     }  
  37.       
  38. }  
//-----------测试类---------------------------
[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5. using ProtoBuf;  
  6. using System;  
  7.   
  8.   
  9. public class ProtobufNet : MonoBehaviour {  
  10.   
  11.   
  12.     private const String PATH = "c://data.bin";  
  13.   
  14.   
  15.     void Start () {  
  16.         //生成数据  
  17.         List<Test> testData = new List<Test>();  
  18.         for (int i = 0; i < 100; i++)  
  19.         {  
  20.             testData.Add(new Test() { Id = i, data = new List<string>(new string[]{"1","2","3"}) });  
  21.         }  
  22.         //将数据序列化后存入本地文件  
  23.         using(Stream file = File.Create(PATH))  
  24.         {  
  25.             Serializer.Serialize<List<Test>>(file, testData);  
  26.             file.Close();  
  27.         }  
  28.         //将数据从文件中读取出来,反序列化  
  29.         List<Test> fileData;  
  30.         using (Stream file = File.OpenRead(PATH))  
  31.         {  
  32.             fileData = Serializer.Deserialize<List<Test>>(file);  
  33.         }  
  34.         //打印数据  
  35.         foreach (Test data in fileData)  
  36.         {  
  37.            Debug.Log(data);  
  38.         }  
  39.     }  
  40.       
  41. }  

  • 总结

Protobuf-net 利用Attributes来实现序列化字段,对程序员的负担减轻,代码侵入性也降低。接下来,我将会写一个简单的Unity c/s demo,其中的通信编码就是用到Protobuf,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; //需要用到MemoryStream using System.IO; using UnityEngine; //引入ProtoBuf命名空间 using ProtoBuf; /// /// 测试类 /// public class TestProtobuf : MonoBehaviour { /// /// 用于测试的数据类 /// [ProtoContract] //声明这个类能被序列化 public class UserData { //声明每一个需要被序列化的成员,编号从1开始 [ProtoMember(1)] public int id; [ProtoMember(2)] public string name; [ProtoMember(3)] public int level; } //测试代码 void Start() { //将要被序列化的UserData示例 UserData user1 = new UserData (); user1.id = 1; user1.name = "User1"; user1.level = 10; //打印user1 Debug.Log (string.Format ("user1-> id:{0}, name:{1}, level:{2}", user1.id, user1.name, user1.level)); //序列化 byte[] buff = null; using (MemoryStream ms = new MemoryStream ()) { Serializer.Serialize (ms, user1); ms.Position = 0; int length = (int)ms.Length; buff = new byte[length]; ms.Read (buff, 0, length); } //输出字节数组 Debug.Log (string.Format("Serialized data-> {0}", BitConverter.ToString(buff))); //反序列化 UserData user2 = default(UserData); using (MemoryStream ms = new MemoryStream (buff)) { user2 = Serializer.Deserialize (ms); } //打印反序列化生成的user2 Debug.Log (string.Format ("user2-> id:{0}, name:{1}, level:{2}", user2.id, user2.name, user2.level)); } } 作者:qufangliu 链接:https://www.jianshu.com/p/d9be1b3d2446 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值