「Unity/grpc/Core」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→unityサンプルのDL) |
(→photo生成して実行) |
||
| 行96: | 行96: | ||
参考:https://qiita.com/8March/items/35a0ce799e8c8588a126 | 参考:https://qiita.com/8March/items/35a0ce799e8c8588a126 | ||
| + | |||
| + | ===Server.cs作成=== | ||
| + | Assets/Scripts/Hello2Server.cs | ||
| + | <pre> | ||
| + | using System.Threading.Tasks; | ||
| + | using UnityEngine; | ||
| + | using Grpc.Core; | ||
| + | |||
| + | public class Hello2Server : MonoBehaviour | ||
| + | { | ||
| + | private void Start() | ||
| + | { | ||
| + | var server = new Server() | ||
| + | { | ||
| + | Services = { HelloService.BindService(new Hello2ServerService()) }, | ||
| + | Ports = { new ServerPort("localhost", 50051, ServerCredentials.Insecure) } | ||
| + | }; | ||
| + | server.Start(); | ||
| + | } | ||
| + | } | ||
| + | public class Hello2ServerService : HelloService.HelloServiceBase | ||
| + | { | ||
| + | public override Task<Response> SayHello(Request request, ServerCallContext context) | ||
| + | { | ||
| + | var hello = $"Hello {request.Name}"; | ||
| + | var response = new Response { Hello = hello }; | ||
| + | return Task.FromResult(response); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | ===Client.cs作成=== | ||
| + | Hello2Client.cs | ||
| + | <pre> | ||
| + | using UnityEngine; | ||
| + | using UnityEngine.UI; | ||
| + | using Grpc.Core; | ||
| + | |||
| + | public class Hello2Client : MonoBehaviour | ||
| + | { | ||
| + | [SerializeField] Button button; | ||
| + | [SerializeField] Text text; | ||
| + | void Start() | ||
| + | { | ||
| + | button.onClick.AddListener(() => { | ||
| + | var channel = new Channel("localhost:50051", ChannelCredentials.Insecure); | ||
| + | var client = new HelloService.HelloServiceClient(channel); | ||
| + | var response = client.SayHello(new Request() { Name = "taro" }); | ||
| + | Debug.Log(response.Hello); | ||
| + | text.text = response.Hello; | ||
| + | }); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
==参考== | ==参考== | ||
https://took.jp/unity-grpc-core/ | https://took.jp/unity-grpc-core/ | ||
2023年8月24日 (木) 10:28時点における版
目次
grpc.coreとは
- gRPCの古いプラグイン
- 2023/5までサポート
- Mac M1は動かない。Mac Intelは動く。
準備
protobufインストール
protobufは、データシリアライズ用
macの場合
brew install protobuf
grpcインストール
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
$ vim ~/.bash_profile export PATH="$PATH:$(go env GOPATH)/bin"
インストール確認
$ protoc-gen-go --version $ protoc-gen-go-grpc --version
unity側grpc準備
から以下をDL
- grpc_unity_package.zip
- Grpc.Tools.zip
grpc_unity_package.zipを展開
Google.Protobuf Grpc.Core Grpc.Core.Api System.Buffers System.Memory System.Runtime.CompilerServices.Unsafe
grpc-toolsのインストール
Grpc.Tools.zipを使う場合
- Grpc.Tools.2.47.0-dev202204190851.unpkgの拡張子を.zipへ変更し展開
- 適当な場所に置く(/d/src/Grpc.Tools.2.47.0-dev202204190851 など)
vim ~/.bash_profile
export PATH="$PATH:/d/src/Grpc.Tools.2.47.0-dev202204190851/tools/macosx_x64"
- 以下のようにパスを通す
$ source ~/.bash_profile
権限変更
$ chmod 755 grpc_csharp_plugin
実行確認
$ grpc_csharp_plugin
macでbrewを使う場合
$ brew tap grpc/grpc $ brew install grpc
参考:https://qiita.com/8March/items/35a0ce799e8c8588a126
unityサンプルのDL
https://github.com/grpc/grpc/tree/67538122780f8a081c774b66884289335c290cbe
examples/csharp/HelloworldUnity にUnityサンプルプロジェクトがある
- examples/csharp/HelloworldUnity/Assets以下のScenesとScriptsを自分のプロジェクトのAssets以下に移す
- grpc_unity_package.zipを解凍して、Assets/Plugins以下に移す
Assets/Scene/Sample.unityを開いてボタンを押すと、実行できる。
photo生成して実行
photo生成
Photos/Hello2.proto
syntax = "proto3";
message Request{
string name = 1;
}
message Response{
string hello = 1;
}
// Requestを引数に渡してSayHelloを呼び出すと、Responseが返ってくる
service HelloService {
rpc SayHello(Request) returns (Response) {}
}
$ cd 自分のサンプルプロジェクト $ protoc -I ./Protos --csharp_out=Assets/Scripts --grpc_out=Assets/Scripts --plugin=protoc-gen-grpc=/d/src/Grpc.Tools.2.47.0-dev202204190851/tools/macosx_x64/grpc_csharp_plugin ./Protos/Hello2.proto
以下ファイルができてることを確認
- Assets/Scripts/Hello2.cs
- Assets/Scripts/Hello2Grpc.cs
参考:https://qiita.com/8March/items/35a0ce799e8c8588a126
Server.cs作成
Assets/Scripts/Hello2Server.cs
using System.Threading.Tasks;
using UnityEngine;
using Grpc.Core;
public class Hello2Server : MonoBehaviour
{
private void Start()
{
var server = new Server()
{
Services = { HelloService.BindService(new Hello2ServerService()) },
Ports = { new ServerPort("localhost", 50051, ServerCredentials.Insecure) }
};
server.Start();
}
}
public class Hello2ServerService : HelloService.HelloServiceBase
{
public override Task<Response> SayHello(Request request, ServerCallContext context)
{
var hello = $"Hello {request.Name}";
var response = new Response { Hello = hello };
return Task.FromResult(response);
}
}
Client.cs作成
Hello2Client.cs
using UnityEngine;
using UnityEngine.UI;
using Grpc.Core;
public class Hello2Client : MonoBehaviour
{
[SerializeField] Button button;
[SerializeField] Text text;
void Start()
{
button.onClick.AddListener(() => {
var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);
var client = new HelloService.HelloServiceClient(channel);
var response = client.SayHello(new Request() { Name = "taro" });
Debug.Log(response.Hello);
text.text = response.Hello;
});
}
}
