Skip to content

Commit d07ccc5

Browse files
authored
feat: add unity grpc client (#105)
A gRPC .NET client with Unity. --------- Signed-off-by: D4ryl00 <[email protected]>
1 parent dfd9a68 commit d07ccc5

13 files changed

+14056
-8
lines changed

api/gen/csharp/Gnonativetypes.cs

+11,333
Large diffs are not rendered by default.

api/gen/csharp/Rpc.cs

+411
Large diffs are not rendered by default.

api/gen/csharp/RpcGrpc.cs

+2,170
Large diffs are not rendered by default.

api/gen/es/gnonativetypes_pb.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v1.5.1
1+
// @generated by protoc-gen-es v1.6.0
22
// @generated from file gnonativetypes.proto (package land.gno.gnonative.v1, syntax proto3)
33
/* eslint-disable */
44
// @ts-nocheck

api/gen/es/gnonativetypes_pb.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v1.5.1
1+
// @generated by protoc-gen-es v1.6.0
22
// @generated from file gnonativetypes.proto (package land.gno.gnonative.v1, syntax proto3)
33
/* eslint-disable */
44
// @ts-nocheck

api/gen/es/rpc_connect.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-connect-es v1.1.4
1+
// @generated by protoc-gen-connect-es v1.3.0
22
// @generated from file rpc.proto (package land.gno.gnonative.v1, syntax proto3)
33
/* eslint-disable */
44
// @ts-nocheck

api/gen/es/rpc_connect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-connect-es v1.1.4
1+
// @generated by protoc-gen-connect-es v1.3.0
22
// @generated from file rpc.proto (package land.gno.gnonative.v1, syntax proto3)
33
/* eslint-disable */
44
// @ts-nocheck

api/gen/es/rpc_pb.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v1.5.1
1+
// @generated by protoc-gen-es v1.6.0
22
// @generated from file rpc.proto (package land.gno.gnonative.v1, syntax proto3)
33
/* eslint-disable */
44
// @ts-nocheck

api/gen/es/rpc_pb.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @generated by protoc-gen-es v1.5.1
1+
// @generated by protoc-gen-es v1.6.0
22
// @generated from file rpc.proto (package land.gno.gnonative.v1, syntax proto3)
33
/* eslint-disable */
44
// @ts-nocheck

api/gen/go/gnonativetypes.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/gen/go/rpc.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

buf.gen.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ plugins:
1010
out: ./api/gen/es
1111
- plugin: buf.build/connectrpc/es
1212
out: ./api/gen/es
13+
- plugin: buf.build/protocolbuffers/csharp
14+
out: ./api/gen/csharp
15+
- plugin: buf.build/grpc/csharp
16+
out: ./api/gen/csharp

examples/UnityClient/README.md

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# gRPC .NET client with Unity
2+
3+
This README will guide you to set up a new Unity project and to configure a
4+
[.NET gRPC](https://github.com/grpc/grpc-dotnet) client in C#.
5+
6+
## Create a new project
7+
8+
Open Unity Hub, click on `New Project`. In the new window, select 3D in the Core
9+
category. Set the project name and the wanted location in the right panel. Click
10+
on `Create Project`.
11+
12+
## Install dependencies
13+
14+
The gRPC .NET client is avalaible through the Nuget ecosystem. Unity doesn't
15+
support it natively, so we had to add a module
16+
([NuGetForUnity](https://github.com/GlitchEnzo/NuGetForUnity)) to be able to
17+
install it easily. Please install NuGetForUnity by following the README on their
18+
[github page](https://github.com/GlitchEnzo/NuGetForUnity) and come back here.
19+
20+
Upon the installation complete, restart Unity to have the new NuGet menu. Select
21+
Nuget -> Manage NuGet Packages. Search and install these packages:
22+
23+
- Grpc.Net.Client
24+
- Grpc.Net.Client.Web
25+
- Google.Protobuf
26+
27+
## Copy API dependencies
28+
29+
We will copy the generated GnoNative API files and gRPC C# stubs into
30+
`Assets/Gno`.
31+
32+
In a terminal, cd to your Unity project folder and write this command (replace
33+
${gnonative} by the location where is gnonative in your filesystem):
34+
35+
```bash
36+
cp -r ${gnonative}/api/gen/csharp Assets/GnoNative
37+
```
38+
39+
## Create a C# script
40+
41+
In the project panel, click on the `Assets` folder to select it. Then click on
42+
the `+` sign and click on `C# script`. Name the new file `Hello`.
43+
44+
Double click on it to open it in Visual Studio. Replace the content with:
45+
46+
```c#
47+
using System;
48+
using System.Net.Http;
49+
using UnityEngine;
50+
using TMPro;
51+
52+
using Grpc.Net.Client;
53+
using Grpc.Net.Client.Web;
54+
using Land.Gno.Gnonative.V1;
55+
56+
public class Hello : MonoBehaviour
57+
{
58+
public TextMeshProUGUI Text;
59+
60+
private GrpcChannel channel;
61+
private GnoNativeService.GnoNativeServiceClient client;
62+
63+
// Start is called before the first frame update
64+
void Start()
65+
{
66+
var options = new GrpcChannelOptions();
67+
var handler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler());
68+
options.HttpHandler = handler;
69+
70+
channel = GrpcChannel.ForAddress("http://localhost:26658", options);
71+
client = new GnoNativeService.GnoNativeServiceClient(channel);
72+
}
73+
74+
private void OnDestroy()
75+
{
76+
channel.Dispose();
77+
}
78+
79+
public void OnHello()
80+
{
81+
var reply = client.Hello(new HelloRequest { Name = "Gno" });
82+
Debug.Log(reply.Greeting.ToString());
83+
Text.text = $"[{DateTime.Now}] {reply.Greeting}";
84+
}
85+
}
86+
```
87+
88+
In the `Start()` function, we initialize the gRPC client with the gRPC Web
89+
protocol and use the default GnoNative port. In the `OnHello()` function, we do
90+
the `Hello` gRPC call and set the result into the `Text` object. This function
91+
can be triggered by a button for example.
92+
93+
Save the modifications and go back to Unity.
94+
95+
We have to link the script with an object in the scene. So in the `Hierarchy`
96+
panel, click on the `+` sign and `Create Empty`. Name it `UI Script`. From the
97+
Project panel, drag and drop the Hello script to the UI Script object in the
98+
Hierarchy panel.
99+
100+
## Create UI objects
101+
102+
We want to create a button and a text objects on the scene to start the
103+
GnoNative call and print the result.
104+
105+
In the Hierarchy panel, click on the `+` sign and select `UI` and
106+
`Text - TextMeshPro`. In the `TMP Importer` windows, click on
107+
`Import TMP Essentials` and close it. Still in the `UI` section, add a
108+
`Button - TextMeshPro`. You can move the objects in the scene to make it clean.
109+
110+
Now we have to attach these two objects to the script we wrote:
111+
112+
- In the `Hierarchy` panel, select the button. Scroll down on the `Inspector`
113+
(right panel) to find the `On Click` section and click on `+` to add a new
114+
action. Drag and drop the `UI Script` object to the `None (Object)` in this
115+
action, and replace `No Function` by `Hello.OnHello()`.
116+
- In the `Hierarchy` panel, click on `UI Script`. In the `Inspector` panel, you
117+
can see the `Hello (Script)` section with the empty `Text` field. Drag and
118+
drop the `Text` object (in the `Hierarchy`) to this `Text` field (in the
119+
`Inspector`).
120+
121+
## Play
122+
123+
In a terminal, go to `gnonative/goserver` and run the gRPC server:
124+
125+
```bash
126+
go run . tcp
127+
```
128+
129+
In Unity, click on the play button and click to the button on the scene. The
130+
text object should change.

0 commit comments

Comments
 (0)