「Unity/grpc/Core」の版間の差分
(→準備) |
(→protobufインストール) |
||
| (同じ利用者による、間の52版が非表示) | |||
| 行10: | 行10: | ||
macの場合 | macの場合 | ||
brew install protobuf | brew install protobuf | ||
| + | インストール確認 | ||
| + | $ protoc --version | ||
| + | libprotoc 24.4 | ||
| + | |||
| + | ===protobufアップグレード=== | ||
| + | macの場合 | ||
| + | brew upgrade protobuf | ||
===grpcインストール=== | ===grpcインストール=== | ||
| − | go install google.golang.org/protobuf/cmd/protoc-gen-go@v1. | + | |
| − | go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1. | + | |
| + | go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.31 | ||
| + | go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3 | ||
| + | |||
$ vim ~/.bash_profile | $ vim ~/.bash_profile | ||
export PATH="$PATH:$(go env GOPATH)/bin" | export PATH="$PATH:$(go env GOPATH)/bin" | ||
| + | $ source ~/.bash_profile | ||
| + | |||
| + | インストール確認 | ||
| + | $ protoc-gen-go --version | ||
| + | protoc-gen-go v1.31.0 | ||
| + | $ protoc-gen-go-grpc --version | ||
| + | protoc-gen-go-grpc 1.3.0 | ||
| + | |||
| + | protoc-gen-goバージョン確認 | ||
| + | https://github.com/protocolbuffers/protobuf-go/releases | ||
| + | |||
| + | protoc-gen-go-grpcバージョン確認 | ||
| + | https://pkg.go.dev/google.golang.org/grpc/cmd/protoc-gen-go-grpc?tab=versions | ||
| + | |||
| + | ==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を展開 | ||
| + | <pre> | ||
| + | Google.Protobuf | ||
| + | Grpc.Core | ||
| + | Grpc.Core.Api | ||
| + | System.Buffers | ||
| + | System.Memory | ||
| + | System.Runtime.CompilerServices.Unsafe | ||
| + | </pre> | ||
| + | |||
| + | ==grpc-toolsのインストール== | ||
| + | ===Grpc.Tools.zipを使う場合=== | ||
| + | #Grpc.Tools.2.47.0-dev202204190851.unpkgの拡張子を.zipへ変更し展開 | ||
| + | #適当な場所に置く(/d/src/Grpc.Tools.2.47.0-dev202204190851 など) | ||
| + | vim ~/.bash_profile | ||
| + | <pre> | ||
| + | export PATH="$PATH:/d/src/Grpc.Tools.2.47.0-dev202204190851/tools/macosx_x64" | ||
| + | </pre> | ||
| + | #以下のようにパスを通す | ||
| + | $ 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を開いてボタンを押すと、実行できる。 | ||
| + | |||
| + | ===grpc_csharp_ext.bundleの警告が出た場合=== | ||
| + | 警告内容 | ||
| + | "grpc_csharp_ext.bundle"は、開発元を検証できないため開けません。 | ||
| + | mac/システム設計/プライバシーとセキュリティ/このまま許可を選択して開くを選択すると実行できる | ||
| + | |||
| + | Assets/Plugins/Grpc.Core/runtimes/osx/x64/grpc_csharp_ext.bundle にあるファイルっぽい。 | ||
| + | |||
| + | ==photo生成して実行== | ||
| + | ===photo生成=== | ||
| + | Photos/Hello2.proto | ||
| + | <pre> | ||
| + | syntax = "proto3"; | ||
| + | message Request{ | ||
| + | string name = 1; | ||
| + | } | ||
| + | message Response{ | ||
| + | string hello = 1; | ||
| + | } | ||
| + | // Requestを引数に渡してSayHelloを呼び出すと、Responseが返ってくる | ||
| + | service HelloService { | ||
| + | rpc SayHello(Request) returns (Response) {} | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | $ 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 | ||
| + | </pre> | ||
| + | |||
| + | 以下ファイルができてることを確認 | ||
| + | *Assets/Scripts/Hello2.cs | ||
| + | *Assets/Scripts/Hello2Grpc.cs | ||
| + | |||
| + | 参考: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> | ||
| + | #Hello2ServerとHello2Clientを適当なGameObjectにAddする。 | ||
| + | #Buttonを押すと、"Hello taro"とメッセージが返ってくる。 | ||
| + | |||
| + | ==その他== | ||
| + | Timeoutの設定は、deadline 辺り | ||
| + | |||
| + | 参考:https://zenn.dev/mochineko/scraps/5d43cc67c2c7a9 | ||
| + | |||
| + | ==Grpcのログ== | ||
| + | <pre> | ||
| + | GrpcEnvironment.SetLogger(new ConsoleLogger()); | ||
| + | GrpcEnvironment.Logger.Debug("debug1"); | ||
| + | GrpcEnvironment.Logger.Info("info1"); | ||
| + | GrpcEnvironment.Logger.Warning("warning1"); | ||
| + | GrpcEnvironment.Logger.Error("error1"); | ||
| + | GrpcEnvironment.Logger.Debug("Debug {0} / {1}", 1, 2); | ||
| + | GrpcEnvironment.Logger.Warning("Warning {0} / {1}", 1, 2); | ||
| + | GrpcEnvironment.Logger.Error("Error {0} / {1}", 1, 2); | ||
| + | GrpcEnvironment.Logger.Warning(new Exception(), "Warning"); | ||
| + | GrpcEnvironment.Logger.Error(new Exception(), "Error"); | ||
| + | </pre> | ||
| + | 以下カスタムLoggerを実装すれば、アプリConsoleにログを表示できる。 | ||
| + | |||
| + | ===カスタムLogger=== | ||
| + | 呼び出し | ||
| + | <pre> | ||
| + | GrpcEnvironment.SetLogger(new CustomLogger()); | ||
| + | </pre> | ||
| + | カスタムログ(CustomLogger.cs) | ||
| + | <pre> | ||
| + | using Grpc.Core.Logging; | ||
| + | using System; | ||
| + | |||
| + | public class CustomLogger : ILogger | ||
| + | { | ||
| + | public ILogger ForType<T>() | ||
| + | { | ||
| + | return this; | ||
| + | } | ||
| + | |||
| + | public void Debug(string message) | ||
| + | { | ||
| + | UnityEngine.Debug.Log(message); | ||
| + | } | ||
| + | |||
| + | public void Debug(string format, params object[] formatArgs) | ||
| + | { | ||
| + | UnityEngine.Debug.Log(string.Format(format, formatArgs)); | ||
| + | } | ||
| + | |||
| + | public void Error(string message) | ||
| + | { | ||
| + | UnityEngine.Debug.LogError(message); | ||
| + | } | ||
| + | |||
| + | public void Error(string format, params object[] formatArgs) | ||
| + | { | ||
| + | UnityEngine.Debug.LogError(string.Format(format, formatArgs)); | ||
| + | } | ||
| + | |||
| + | public void Error(Exception exception, string message) | ||
| + | { | ||
| + | UnityEngine.Debug.LogError(message + " " + exception); | ||
| + | } | ||
| + | |||
| + | public void Info(string message) | ||
| + | { | ||
| + | UnityEngine.Debug.Log( message); | ||
| + | } | ||
| + | |||
| + | public void Info(string format, params object[] formatArgs) | ||
| + | { | ||
| + | UnityEngine.Debug.Log(string.Format(format, formatArgs)); | ||
| + | } | ||
| + | |||
| + | public void Warning(string message) | ||
| + | { | ||
| + | UnityEngine.Debug.LogWarning(message); | ||
| + | } | ||
| + | |||
| + | public void Warning(string format, params object[] formatArgs) | ||
| + | { | ||
| + | UnityEngine.Debug.LogWarning(string.Format(format, formatArgs)); | ||
| + | } | ||
| + | |||
| + | public void Warning(Exception exception, string message) | ||
| + | { | ||
| + | UnityEngine.Debug.LogWarning(message + " " + exception); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | 参考:https://baba-s.hatenablog.com/entry/2020/03/04/150000 | ||
| + | |||
| + | 参考:https://blog.xin9le.net/entry/2017/09/11/150421 | ||
| + | |||
| + | 参考:http://mxproj.blogspot.com/2018/01/grpc-logger.html | ||
| + | |||
| + | ===SetLogger=== | ||
| + | <pre> | ||
| + | // コンソールの場合 | ||
| + | GrpcEnvironment.SetLogger(new ConsoleLogger()); | ||
| + | // ログレベル制御出力の場合 | ||
| + | GrpcEnvironment.SetLogger(new LogLevelFilterLogger(new ConsoleLogger(), LogLevel.Warning, true)); | ||
| + | // ファイル出力の場合 | ||
| + | GrpcEnvironment.SetLogger(new TextWriterLogger(TextWriter.Synchronized(File.CreateText("out.log")))); | ||
| + | // ログなしの場合 | ||
| + | GrpcEnvironment.SetLogger(new NullLogger()); | ||
| + | // 使用先のクラスを引数に | ||
| + | GrpcEnvironment.SetLogger(GrpcEnvironment.Logger.ForType<Hello2Client>()); | ||
| + | </pre> | ||
| + | |||
| + | ===環境変数=== | ||
| + | 全部のログを出力したい場合 | ||
| + | <pre> | ||
| + | Environment.SetEnvironmentVariable("GRPC_TRACE", "all"); | ||
| + | Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "DEBUG"); | ||
| + | </pre> | ||
| + | |||
| + | 参考:https://kiririmode.hatenablog.jp/entry/20190512/1557611712 | ||
| + | |||
| + | ==grpcのdocker== | ||
| + | https://github.com/namely/docker-protoc | ||
| + | |||
| + | ===protocのバージョン確認=== | ||
| + | <pre> | ||
| + | $ docker run -v $PWD:/defs namely/prototool version | ||
| + | Default protoc version: 3.11.0 | ||
| + | </pre> | ||
| + | |||
| + | ==参考== | ||
| + | https://took.jp/unity-grpc-core/ | ||
2023年10月11日 (水) 19:35時点における最新版
目次
grpc.coreとは
- gRPCの古いプラグイン
- 2023/5までサポート
- Mac M1は動かない。Mac Intelは動く。
準備
protobufインストール
protobufは、データシリアライズ用
macの場合
brew install protobuf
インストール確認
$ protoc --version libprotoc 24.4
protobufアップグレード
macの場合
brew upgrade protobuf
grpcインストール
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.31 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3
$ vim ~/.bash_profile export PATH="$PATH:$(go env GOPATH)/bin" $ source ~/.bash_profile
インストール確認
$ protoc-gen-go --version protoc-gen-go v1.31.0 $ protoc-gen-go-grpc --version protoc-gen-go-grpc 1.3.0
protoc-gen-goバージョン確認 https://github.com/protocolbuffers/protobuf-go/releases
protoc-gen-go-grpcバージョン確認 https://pkg.go.dev/google.golang.org/grpc/cmd/protoc-gen-go-grpc?tab=versions
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を開いてボタンを押すと、実行できる。
grpc_csharp_ext.bundleの警告が出た場合
警告内容
"grpc_csharp_ext.bundle"は、開発元を検証できないため開けません。
mac/システム設計/プライバシーとセキュリティ/このまま許可を選択して開くを選択すると実行できる
Assets/Plugins/Grpc.Core/runtimes/osx/x64/grpc_csharp_ext.bundle にあるファイルっぽい。
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;
});
}
}
- Hello2ServerとHello2Clientを適当なGameObjectにAddする。
- Buttonを押すと、"Hello taro"とメッセージが返ってくる。
その他
Timeoutの設定は、deadline 辺り
参考:https://zenn.dev/mochineko/scraps/5d43cc67c2c7a9
Grpcのログ
GrpcEnvironment.SetLogger(new ConsoleLogger());
GrpcEnvironment.Logger.Debug("debug1");
GrpcEnvironment.Logger.Info("info1");
GrpcEnvironment.Logger.Warning("warning1");
GrpcEnvironment.Logger.Error("error1");
GrpcEnvironment.Logger.Debug("Debug {0} / {1}", 1, 2);
GrpcEnvironment.Logger.Warning("Warning {0} / {1}", 1, 2);
GrpcEnvironment.Logger.Error("Error {0} / {1}", 1, 2);
GrpcEnvironment.Logger.Warning(new Exception(), "Warning");
GrpcEnvironment.Logger.Error(new Exception(), "Error");
以下カスタムLoggerを実装すれば、アプリConsoleにログを表示できる。
カスタムLogger
呼び出し
GrpcEnvironment.SetLogger(new CustomLogger());
カスタムログ(CustomLogger.cs)
using Grpc.Core.Logging;
using System;
public class CustomLogger : ILogger
{
public ILogger ForType<T>()
{
return this;
}
public void Debug(string message)
{
UnityEngine.Debug.Log(message);
}
public void Debug(string format, params object[] formatArgs)
{
UnityEngine.Debug.Log(string.Format(format, formatArgs));
}
public void Error(string message)
{
UnityEngine.Debug.LogError(message);
}
public void Error(string format, params object[] formatArgs)
{
UnityEngine.Debug.LogError(string.Format(format, formatArgs));
}
public void Error(Exception exception, string message)
{
UnityEngine.Debug.LogError(message + " " + exception);
}
public void Info(string message)
{
UnityEngine.Debug.Log( message);
}
public void Info(string format, params object[] formatArgs)
{
UnityEngine.Debug.Log(string.Format(format, formatArgs));
}
public void Warning(string message)
{
UnityEngine.Debug.LogWarning(message);
}
public void Warning(string format, params object[] formatArgs)
{
UnityEngine.Debug.LogWarning(string.Format(format, formatArgs));
}
public void Warning(Exception exception, string message)
{
UnityEngine.Debug.LogWarning(message + " " + exception);
}
}
参考:https://baba-s.hatenablog.com/entry/2020/03/04/150000
参考:https://blog.xin9le.net/entry/2017/09/11/150421
参考:http://mxproj.blogspot.com/2018/01/grpc-logger.html
SetLogger
// コンソールの場合
GrpcEnvironment.SetLogger(new ConsoleLogger());
// ログレベル制御出力の場合
GrpcEnvironment.SetLogger(new LogLevelFilterLogger(new ConsoleLogger(), LogLevel.Warning, true));
// ファイル出力の場合
GrpcEnvironment.SetLogger(new TextWriterLogger(TextWriter.Synchronized(File.CreateText("out.log"))));
// ログなしの場合
GrpcEnvironment.SetLogger(new NullLogger());
// 使用先のクラスを引数に
GrpcEnvironment.SetLogger(GrpcEnvironment.Logger.ForType<Hello2Client>());
環境変数
全部のログを出力したい場合
Environment.SetEnvironmentVariable("GRPC_TRACE", "all");
Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "DEBUG");
参考:https://kiririmode.hatenablog.jp/entry/20190512/1557611712
grpcのdocker
https://github.com/namely/docker-protoc
protocのバージョン確認
$ docker run -v $PWD:/defs namely/prototool version Default protoc version: 3.11.0
