http://blog.csdn.net/janeky/article/details/17104877
Games contain a variety of data, including local data and data that communicates with the server. Today we'll talk about how to store data, and how to encode the client and server. Based on previous experience, we can use the string, Xml,json ... Binary can even be stored directly. Various ways have their own merits and demerits, some performance is better, but the implementation of the way is more troublesome. Some data redundancy is too many.
Today we are going to learn a widely used data format: PROTOBUF. Simply put, it is a binary format that is initiated by Google and is now widely used in a variety of development languages. The specific introduction can be found in: https://code.google.com/p/protobuf/. The reason why we choose Protobuf, is based on its high efficiency, less data redundancy, simple programming and other characteristics. On the PROTOBUF implementation of C #, there are several versions on the Internet, it is generally accepted that protobuf-net is better.
Let's take a look at one of the simplest examples: serializing a class to a binary file in protobuf format. The binary data is read again, and the object data is deserialized.
Reference an example from the Internet http://blog.csdn.net/ddxkjddx/article/details/7239798
----------------entity class----------------------
[CSharp]View Plaincopy
- Using Unityengine;
- Using System.Collections;
- Using Protobuf;
- Using System;
- Using System.Collections.Generic;
- [Protocontract]
- Public class Test {
- [Protomember (1)]
- public int Id
- {
- get;
- set;
- }
- [Protomember (2)]
- Public list<string> Data
- {
- get;
- set;
- }
- public Override string ToString ()
- {
- String str = id+":";
- foreach (String d in data)
- {
- str + = d + ",";
- }
- return str;
- }
- }
-----------Test Class---------------------------
[CSharp]View Plaincopy
- Using Unityengine;
- Using System.Collections;
- Using System.Collections.Generic;
- Using System.IO;
- Using Protobuf;
- Using System;
- Public class Protobufnet:monobehaviour {
- private const String PATH = "C://data.bin";
- void Start () {
- //Generate Data
- list<test> testData = new list<test> ();
- For (int i = 0; i <; i++)
- {
- Testdata.add (new Test () {Id = i, data = new list<string> (new string[]{"1","2","3" }) });
- }
- //serialization of data into local files
- using (Stream file = File.create (PATH))
- {
- serializer.serialize<list<test>> (file, testData);
- File. Close ();
- }
- //data is read from the file, deserialized
- List<test> FileData;
- using (Stream file = File.openread (PATH))
- {
- FileData = serializer.deserialize<list<test>> (file);
- }
- //Print data
- foreach (Test data in fileData)
- {
- Debug.Log (data);
- }
- }
- }
Protobuf-net uses attributes to implement serialization fields, reduces the burden on programmers, and decreases code intrusion. Next, I will write a simple unity C/S demo, where the communication code is used to Protobuf,
We'll share it with you. Have any questions welcome to explore [email protected]
Unity Hand Tour Road < >c# version Protobuf