码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-03-18 12:19:59      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

向原创致敬http://blog.csdn.net/janeky/article/details/17104877

 

一个游戏包含了各种数据,包括本地数据和与服务端通信的数据。今天我们来谈谈如何存储数据,以及客户端和服务端的编码方式。根据以前的经验,我们可以用字符串,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 plaincopy技术分享技术分享
  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 plaincopy技术分享技术分享
    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. }  

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

标签:

原文地址:http://www.cnblogs.com/lindan929/p/4346597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!