PROTOBUF is a data interchange format that is much faster than using XML for data exchange because it is a binary format.
It can be used in data communication between distributed applications or in heterogeneous environments.
As a good efficiency and compatibility of the binary data transmission format, can be used for such as network transport, configuration files, data storage and many other areas
Development tools: VS, Unity
PROTOBUF Official website: Click to open the link
Resources available: Click to open link
"VS Project"
Create a new C # empty project using VS
Right click on the reference
Select NuGet Package, search protobuf (here I choose Protobuf-net. Enyim)
Click Download to install and create a
Person.cs object to combat the application of a serialized and deserialized session
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using Protobuf;
Namespace Protobufobj
{
[protocontract]
class person
{
[Protomember (1)] public
int ID
{
get;
Set;
}
[Protomember (2)]
public string name
{
get;
Set;
}
[Protomember (3)]
public string address
{
get;
Set;}}
}
Next, implement the serialization and deserialization functions
Encapsulates two function interfaces, which are converted to strings and files, respectively.
New ProtobufHelper.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using Protobuf;
Using System.IO; Namespace Protobufobj {class Protobufhelper {//Serializes the object to a string public static string Serializertostrin G<t> (T-t) {using (MemoryStream ms = new MemoryStream ()) {Serialize
R.serialize (ms,t); Return ASCIIEncoding.UTF8.GetString (Ms.
ToArray ());
}}//Serializing an object to a file public static void Serializertofile<t> (T t,string filePath) {
using (var file = file.create (FilePath)) {serializer.serialize (file,t); }}//Convert string to object public static T dederializerfromstring<t> (String str) {// The using as statement, which defines a range, releases the object at the end of the range//converts the string into a memory stream using (MemoryStream ms=new MemoryStream (asciiencoding.u TF8.
GetBytes (str))) {T obj = serializer.deserialize<t> (ms);
return obj;
}}//Convert file data to object public static T dederializerfromfile<t> (string filePath) { using (var file = File.openread (FilePath)) {T obj = serializer.deserialize<t> (file
);
return obj;
}
}
}
}
The final Test
New Program.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using Protobuf;
Namespace Protobufobj
{
class program
{public
static void Main (string[] args)
{
person person = new Person
{
id=1,
name= "step",
address= "Jiangxi Ganzhou"
};
Serializes a
string str=protobufhelper.serializertostring<person> (person);
Deserialization of person
obj=protobufhelper.dederializerfromstring<person> (str);}
}
debugger, found together normal, person object is serialized as binary string
Of course it can be converted to a file
"Unity"
After the VS project, we can find Protobuf-net.dll in the debug file directory of the project
Create a new named plugins file in unity and copy the Protobuf-net.dll to it
Copy Person.cs, Program.cs, ProtobufHelper.cs to Unity
Modify the Program.cs file directory to:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Protobuf;
Using Unityengine;
Namespace Protobufobj
{
class program:monobehaviour
{
void Start ()
{person person
= new Person
{
id=1,
name= "step",
address= "Jiangxi Ganzhou"
};
Serializes a
string str=protobufhelper.serializertostring<person> (person);
Debug.Log (str);
Deserialization of person
obj=protobufhelper.dederializerfromstring<person> (str);}
}
Run the program, some normal