在新版Unity中使用Protobuf以及使用Google.Protobuf.WellKnownTypes中的Duration Timestamp

5 篇文章 0 订阅

总的来说,就是手写.proto文件后,用CMD命令行运行protoc.exe编译器,进入.proto文件路径生成C#文件,再把C#文件放到Unity项目中使用。

准备编译器:

编译器是用来将.proto文件编译成相应语音脚本的工具, 编译器可以直接从GitHub上下载也可以选择自己使用工具生成。
GitHub下载 (Git地址 https://github.com/google/protobuf/releases), 下载 对应的protobuf包 (如 protoc-3.9.1-win64.zip), 在bin文件夹下有对应得 protoc.exe 编译器

编写.proto文件:

syntax = "proto3";

package namespace.packagename;

import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";

message classname{
string name=1;
google.protobuf.Duration Delta=2;
google.protobuf.Timestamp lastTime=3;
}

!注意Duration 和Timestamp 的大小写!

编译.CS文件:

E:\OtherProjects\protoc\bin>protoc.exe --proto_path=./ classname.proto --csharp_out=./generate

!注意classname前面要加空格,否则报错“missing input file”

更多proto语法,命令: https://developers.google.com/protocol-buffers/docs/proto3

将CS文件拷入unity

准备DLL文件:

1,从GitHub上下载protobuf源码 (源码链接:https://github.com/google/protobuf)我下的是protobuf-3.9.1
2,打开工程目录下 csharp/src/Google.Protobuf.sln 文件。 PS:在下用的VS2017打开的
3,生成Google.Protobuf得到Google.Protobuf.dll文件 (netstandard1.0版)

4,拷入unity

测试:

类似下列代码进行测试(参考自 https://www.jianshu.com/p/b135676dbe8d):

using UnityEngine;
using Protobuf;            //应用CS文件的命名空间 (.proto文件中的 package 值)
using Google.Protobuf;     //引用DLL
using Google.Protobuf.WellKnownTypes;

public class Test : MonoBehaviour {

   void Start()
   {
       //新建一个Person对象,并赋值
       Person ac = new Person();
        ac.Proj = "ts1";
        ac.Delta = Duration.FromTimeSpan(new TimeSpan(314));
        ac.LastTime = Timestamp.FromDateTime(DateTime.Now.ToUniversalTime());//ArgumentException: Conversion from DateTime to Timestamp requires the DateTime kind to be Utc

        //将对象转换成字节数组
        byte[] databytes = ac.ToByteArray();

        //将字节数据的数据还原到对象中
        IMessage IMperson = new Person();
        Person p1 = new Person();
        p1 = (Person)IMperson.Descriptor.Parser.ParseFrom(databytes);

        //输出测试
        Debug.Log(p1.Proj);
        Debug.Log(p1.FirstTime);//默认值为null
        Debug.Log(p1.LastTime.ToDateTime().ToLocalTime());
        Debug.Log(p1.Delta);
   }
}

 

要在Unity使用Google.Protobuf.dll进行序列化和反序列化,你需要按照以下步骤进行操作: 1. 下载并安装Google.Protobuf.dll。 2. 在Unity创建一个新的C#脚本,并将其命名为“ProtoHelper”(或任何你想要的名称)。 3. 在脚本添加以下代码: ```csharp using Google.Protobuf; using System.IO; public static class ProtoHelper { public static byte[] Serialize<T>(T obj) where T : IMessage<T> { using (MemoryStream stream = new MemoryStream()) { obj.WriteTo(stream); return stream.ToArray(); } } public static T Deserialize<T>(byte[] bytes) where T : IMessage<T>, new() { T message = new T(); message.MergeFrom(bytes); return message; } } ``` 这个代码片段创建了一个名为“ProtoHelper”的静态类,并包含两个静态方法:Serialize和Deserialize。 Serialize方法将一个IMessage<T>对象序列化为一个字节数组,而Deserialize方法将一个字节数组反序列化为一个IMessage<T>对象。 4. 现在你可以在任何其他C#脚本使用这些方法来序列化和反序列化你的ProtoBuf消息。例如: ```csharp using Google.Protobuf; using UnityEngine; public class MyClass : MonoBehaviour { private void Start() { // 创建一个新的ProtoBuf消息 MyMessage message = new MyMessage { Id = 123, Name = "John Doe" }; // 序列化消息 byte[] bytes = ProtoHelper.Serialize(message); // 反序列化消息 MyMessage deserializedMessage = ProtoHelper.Deserialize<MyMessage>(bytes); // 输出消息 Debug.Log(deserializedMessage); } } ``` 这个示例创建了一个名为“MyClass”的MonoBehaviour,并在Start方法创建了一个新的MyMessage对象。然后,它使用ProtoHelper.Serialize方法将该消息序列化为一个字节数组,并使用ProtoHelper.Deserialize方法将该字节数组反序列化为一个新的MyMessage对象。最后,它将反序列化的消息输出到Unity的控制台窗口。 这就是使用Google.Protobuf.dll在Unity进行序列化和反序列化的基本步骤。记住,你需要正确安装Google.Protobuf.dll,以便在Unity使用它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值