facebook twitter hatena line email

Unity/grpc/Core

提供: 初心者エンジニアの簡易メモ
2023年8月24日 (木) 10:28時点におけるAdmin (トーク | 投稿記録)による版 (photo生成して実行)

移動: 案内検索

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準備

https://packages.grpc.io/archive/2022/04/67538122780f8a081c774b66884289335c290cbe-f15a2c1c-582b-4c51-acf2-ab6d711d2c59/index.xml

から以下を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を使う場合

  1. Grpc.Tools.2.47.0-dev202204190851.unpkgの拡張子を.zipへ変更し展開
  2. 適当な場所に置く(/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"
  1. 以下のようにパスを通す
$ 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サンプルプロジェクトがある

  1. examples/csharp/HelloworldUnity/Assets以下のScenesとScriptsを自分のプロジェクトのAssets以下に移す
  2. 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;
        });
    }
}

参考

https://took.jp/unity-grpc-core/