# protobuf_for_unity **Repository Path**: googleapp/protobuf_for_unity ## Basic Information - **Project Name**: protobuf_for_unity - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-08-12 - **Last Updated**: 2021-08-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # protobuf_for_unity google's protobuf 3.x for unity3D 5.x 来源: [google's protobuf](https://github.com/google/protobuf/tree/master/csharp)
版本号:3.x
适用:unity3d 5.x
备注:unity 2017 及以后版本请使用 google 官方版本

使用方法:
将目录 Google.Protobuf 下的所有文件拷到 Unity 工程中。
![](https://github.com/GongFaxin/protobuf_for_unity/raw/master/doc/Project.png)

接下来可以参考 test_msg.proto
```protobuf syntax = "proto3"; message TheMsg { string name = 1; int32 num = 2; } ``` 以及 Test.cs
```C# void Start () { TheMsg msg = new TheMsg(); msg.Name = "am the name"; msg.Num = 32; Debug.Log(string.Format("The Msg is ( Name:{0},Num:{1} )",msg.Name,msg.Num)); byte[] bmsg; using (MemoryStream ms = new MemoryStream()) { msg.WriteTo(ms); bmsg = ms.ToArray(); } TheMsg msg2 = TheMsg.Parser.ParseFrom(bmsg); Debug.Log(string.Format("The Msg2 is ( Name:{0},Num:{1} )",msg2.Name,msg2.Num)); } ``` 测试输出:
![](https://github.com/GongFaxin/protobuf_for_unity/raw/master/doc/Log.png)