This README will guide you to set up a new Unity project and to configure a .NET gRPC client in C#.
Open Unity Hub, click on New Project
. In the new window, select 3D in the Core
category. Set the project name and the wanted location in the right panel. Click
on Create Project
.
The gRPC .NET client is avalaible through the Nuget ecosystem. Unity doesn't support it natively, so we had to add a module (NuGetForUnity) to be able to install it easily. Please install NuGetForUnity by following the README on their github page and come back here.
Upon the installation complete, restart Unity to have the new NuGet menu. Select Nuget -> Manage NuGet Packages. Search and install these packages:
- Grpc.Net.Client
- Grpc.Net.Client.Web
- Google.Protobuf
We will copy the generated GnoNative API files and gRPC C# stubs into
Assets/Gno
.
In a terminal, cd to your Unity project folder and write this command (replace ${gnonative} by the location where is gnonative in your filesystem):
cp -r ${gnonative}/api/gen/csharp Assets/GnoNative
In the project panel, click on the Assets
folder to select it. Then click on
the +
sign and click on C# script
. Name the new file Hello
.
Double click on it to open it in Visual Studio. Replace the content with:
using System;
using System.Net.Http;
using UnityEngine;
using TMPro;
using Grpc.Net.Client;
using Grpc.Net.Client.Web;
using Land.Gno.Gnonative.V1;
public class Hello : MonoBehaviour
{
public TextMeshProUGUI Text;
private GrpcChannel channel;
private GnoNativeService.GnoNativeServiceClient client;
// Start is called before the first frame update
void Start()
{
var options = new GrpcChannelOptions();
var handler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler());
options.HttpHandler = handler;
channel = GrpcChannel.ForAddress("http://localhost:26658", options);
client = new GnoNativeService.GnoNativeServiceClient(channel);
}
private void OnDestroy()
{
channel.Dispose();
}
public void OnHello()
{
var reply = client.Hello(new HelloRequest { Name = "Gno" });
Debug.Log(reply.Greeting.ToString());
Text.text = $"[{DateTime.Now}] {reply.Greeting}";
}
}
In the Start()
function, we initialize the gRPC client with the gRPC Web
protocol and use the default GnoNative port. In the OnHello()
function, we do
the Hello
gRPC call and set the result into the Text
object. This function
can be triggered by a button for example.
Save the modifications and go back to Unity.
We have to link the script with an object in the scene. So in the Hierarchy
panel, click on the +
sign and Create Empty
. Name it UI Script
. From the
Project panel, drag and drop the Hello script to the UI Script object in the
Hierarchy panel.
We want to create a button and a text objects on the scene to start the GnoNative call and print the result.
In the Hierarchy panel, click on the +
sign and select UI
and
Text - TextMeshPro
. In the TMP Importer
windows, click on
Import TMP Essentials
and close it. Still in the UI
section, add a
Button - TextMeshPro
. You can move the objects in the scene to make it clean.
Now we have to attach these two objects to the script we wrote:
- In the
Hierarchy
panel, select the button. Scroll down on theInspector
(right panel) to find theOn Click
section and click on+
to add a new action. Drag and drop theUI Script
object to theNone (Object)
in this action, and replaceNo Function
byHello.OnHello()
. - In the
Hierarchy
panel, click onUI Script
. In theInspector
panel, you can see theHello (Script)
section with the emptyText
field. Drag and drop theText
object (in theHierarchy
) to thisText
field (in theInspector
).
In a terminal, go to gnonative/goserver
and run the gRPC server:
go run . tcp
In Unity, click on the play button and click to the button on the scene. The text object should change.