Unity3D 使用 Proto

一. 下载与安装

这里下载
Google Protobuff下载

1. 源码用来编译CSharp 相关配置

2. win64 用于编译 proto 文件

二. 编译

1. 使用VS 打开

2. 点击最上面菜单栏 工具>NuGet 包管理器>管理解决方案的NuGet 管理包

版本一定要选择咱们一开始下载的对应版本否则不兼容!这里选择3.25.3。然后点击安装.

3.编译项目

拷贝文件

unity 目录

三.proto 文件

新建txt文本取名Person.proto,切记后缀.txt改为.proto
输入一下代码

syntax="proto3";//指定了正在使用proto3语法,如果没指定编译器默认使用proto2语法
package TestGoogleProtoBuff;//等于C#中命名空间

message personInfo
{
	string name=1;
	int32 age=2;
	int64 money=3;
	message PhoneNumber{
		string number=1;
		PhoneType type=2;
	}
	repeated PhoneNumber phone=5;
}

enum PhoneType{
	HOME=0;
	WORK=1;
	MOBILE=2;
}

编译 

然后在这个文件夹中创建ExProtProto.bat,输入一下命令并保存。

双击ExProtProto.bat,我们发现生成了Person.cs文件

四.Unity中的使用

然后我们打开unity,新建文件夹Proto,将刚刚生成好的Person.cs代码放在Proto文件夹中。

我们在unity中新建ProtoBufffer.cs脚本

using Google.Protobuf;
public class ProtoBufffer
{
    public static byte[] Serialize(IMessage message)
    {
        return message.ToByteArray();
    }

    public static T DeSerialize<T>(byte[] packet)where T:IMessage, new()
    {
        IMessage message = new T();
        try
        {
            return (T)message.Descriptor.Parser.ParseFrom(packet);
        }
        catch (System.Exception e)
        {
            throw;
        }
    }
}

在创建一个Test.cs脚本来测试该功能,将该脚本拖到主摄像机上

using TestGoogleProtoBuff;
using UnityEngine;

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        personInfo info = new personInfo();
        info.Name = "ys";
        info.Age = 50;
        info.Money = 9999;
        info.Phone.Add(new personInfo.Types.PhoneNumber { Number = "12123",Type=PhoneType.Home});
        byte[] msg = ProtoBufffer.Serialize(info);
        Debug.Log(msg.Length);

        personInfo deinfo  = ProtoBufffer.DeSerialize<personInfo>(msg);
        Debug.Log("姓名:"+deinfo.Name);
        Debug.Log("年龄:"+deinfo.Age);
        Debug.Log("资产:" + deinfo.Money);
        Debug.Log($"{deinfo.Phone[0].Type}的电话号:{deinfo.Phone[0].Number}");
    }
}

未完待续........

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将.proto文件生成Java文件,可以使用gRPC提供的protobuf编译器插件。下面是在Spring Boot中使用protobuf编译器插件将.proto文件生成Java文件的步骤: 1. 在pom.xml文件中添加protobuf编译器插件: ```xml <build> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.17.2:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.38.1:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 在src/main/proto目录下创建.proto文件,如下所示: ```protobuf syntax = "proto3"; option java_multiple_files = true; option java_package = "com.example.grpc"; option java_outer_classname = "HelloWorldProto"; package helloworld; message HelloRequest { string name = 1; } message HelloReply { string message = 1; } service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } ``` 3. 在项目根目录下运行以下命令: ``` mvn clean compile ``` 这将使用protobuf-maven-plugin插件自动从src/main/proto目录中的.proto文件生成Java类。生成的Java类将保存在target/generated-sources/protobuf/java目录中。 以上是在Spring Boot中使用protobuf编译器插件将.proto文件生成Java文件的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值