手机运行 Unity Grpc
(金庆的专栏 2018.8)
- 安装 Unit2018, 支持 .NET 4.x
- 创建一个项目,开启 .NET 4.x
Edit->Project Settings->Player->Other Settings
->Configuration->Script Runtime Version->.Net 4.x Equivalent - 从 https://packages.grpc.io/ 的 Daily Builds 下载最新的
grpc-protoc_windows_x86-VERSION.zip
grpc_unity_package.VERSION.zip - grpc_unity_package.VERSION.zip
解压到 Assets 目录下 - 从 github grpc 复制 examples/protos/helloworld.proto 为 Assets/protos/helloworld.proto
- 创建 Assets/Scripts/Greeter/, 并在该目录下运行
protoc.exe -I../../../protos --csharp_out=. ../../../protos/helloworld.proto --grpc_out=. --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe
- 需要先从 grpc-protoc_windows_x86-VERSION.zip 解压 protoc.exe,grpc_csharp_plugin.exe
- 生成 Helloworld.cs HelloworldGrpc.cs
- 客户端代码
- 创建Channel连接服务器
channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
- 创建客户端并发出请求
var client = new Greeter.GreeterClient(channel);
HelloReply reply = client.SayHello(new HelloRequest { Name = "Jin Qing" });
- 创建Channel连接服务器
- 完整代码见:https://gitee.com/jinq0123/unity-grpc-sample
- 最后打包安装到手机测试通过