- 下载 protobuf-csharp-3.21.1.zip
- 执行脚本 重新生成.dll
- 下载 protoc-21.1-win64.zip
- 写 proto文件
- 写 test.bat
protoc.exe messageTest.proto --csharp_out=./
pause
- 执行 text.bat 生成 *.cs文件
- unity导入.dll , protobuf生成 *.cs 文件
using Google.Protobuf;
using UnityEngine;
public class Test : MonoBehaviour
{
void Start()
{
Person p = new Person()
{
Name = "QQ",
Age = 2,
Marriage = true
};
byte[] bytes = p.ToByteArray();
Person p2 = Person.Parser.ParseFrom(bytes);
Debug.Log($"{p2.Name} {p2.Age} {p2.Marriage}");
Persons ps = new Persons();
ps.Name.Add("as");
ps.Name.Add("ds");
bytes = ps.ToByteArray();
Persons ps2 = Persons.Parser.ParseFrom(bytes);
}
}