Unity Hand Tour Road < >c# version Protobuf

Source: Internet
Author: User

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.

    • Brief introduction

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.

    • Instance

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
  1. Using Unityengine;
  2. Using System.Collections;
  3. Using Protobuf;
  4. Using System;
  5. Using System.Collections.Generic;
  6. [Protocontract]
  7. Public class Test {
  8. [Protomember (1)]
  9. public int Id
  10. {
  11. get;
  12. set;
  13. }
  14. [Protomember (2)]
  15. Public list<string> Data
  16. {
  17. get;
  18. set;
  19. }
  20. public Override string ToString ()
  21. {
  22. String str = id+":";
  23. foreach (String d in data)
  24. {
  25. str + = d + ",";
  26. }
  27. return str;
  28. }
  29. }

-----------Test Class---------------------------

[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. Public class Protobufnet:monobehaviour {
  8. private const String PATH = "C://data.bin";
  9. void Start () {
  10. //Generate Data
  11. list<test> testData = new list<test> ();
  12. For (int i = 0; i <; i++)
  13. {
  14. Testdata.add (new Test () {Id = i, data = new list<string> (new string[]{"1","2","3"  }) });
  15. }
  16. //serialization of data into local files
  17. using (Stream file = File.create (PATH))
  18. {
  19. serializer.serialize<list<test>> (file, testData);
  20. File. Close ();
  21. }
  22. //data is read from the file, deserialized
  23. List<test> FileData;
  24. using (Stream file = File.openread (PATH))
  25. {
  26. FileData = serializer.deserialize<list<test>> (file);
  27. }
  28. //Print data
  29. foreach (Test data in fileData)
  30. {
  31. Debug.Log (data);
  32. }
  33. }
  34. }

    • Summarize

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.