Unity_C#中使用protobuf

Unity_C#中使用protobuf

下载官方protobuf地址: 

https://github.com/protocolbuffers/protobuf/releasesicon-default.png?t=N7T8https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fprotocolbuffers%2Fprotobuf%2Freleases

protobuf-c#源码生成dll,导入unity

1. 下载最新版

 2. VS打开解决方案(protobuf-25.1\csharp\src)

3. 编译生成

Release 模式,然后右键选中 Google.Protobuf -> 生成,可生成需要的 dll 文件。

生成的文件位于 protobuf-22.2/csharp/src/Google.Protobuf/bin/Release/net45 目录下

 4. 将以上文件全部导入Unity的Plugins文件夹:

proto文件生成c#文件,导入unity

1. 下载proto-win64版本程序文件

Releases · protocolbuffers/protobuf (github.com)icon-default.png?t=N7T8https://github.com/protocolbuffers/protobuf/releases

 

2. 编写proto文件,新建文本文件,重命名为*.proto

3. 在同文件夹目录新建.bat文件运行生成c#文件

调用运行:

using Google.Protobuf;
using Msg;
using UnityEngine;

public class Test1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        MessageResult messageResult = new MessageResult();
        messageResult.Code = 99;
        messageResult.Msg = "success";

        // proto消息对象,转换成字节数组
        byte[] dataBytes = messageResult.ToByteArray();

        // proto消息字节数组,转换成对象
        // 第一种方式:实例调用
        // IMessage message = new MessageResult();
        // MessageResult messageResult1 = (MessageResult)message.Descriptor.Parser.ParseFrom(dataBytes);

        // 第二种方式:静态直接调用
        MessageResult messageResult1 = (MessageResult)MessageResult.Descriptor.Parser.ParseFrom(dataBytes);

        Debug.Log(messageResult1.Code);
        Debug.Log(messageResult1.Msg);


    }
}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jennifer33K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值