gRPC in Unity3D
β Last Updated : November 25, 2022
πͺ Prequisite Knowledge (Optional)
Describe Initial Knowledge that reader need to know before reading this article
[gRPC](gRPC 9752fbf9cb004f05b234ee169ccd8b2c.md)
[Unity 3D](Unity 3D 5532e59028e74dd5938b01064478ba74.md)
β Key Question / Problem / Issue
Describe what question / Problem / Issue this knowledge should answer
How to implement gRPC client in unity 3D
β Expected Output/Definition of Done
Define what information you really need and the definition of done
Implemented GRPC Client on Unity for simple use case
π Resulting Solution
Write writing results below
Net Core GRPC
Issues
- Unity does not have NuGet Package Management
- Unity using .Net Standard that does not support requests over Http/2 which is one of the main features of gRPC
- Unity does not have gRPC Compiler like Net Core
Trouble Shooting
- Installing Unofficial Package from GlitchEnzo (Github Link)
- Using Grpc.Client.Web as a handler to request to the server
- Copy file compiler and modify it based on need from https://github.com/FearlessHyena/unity-basic-grpc
Pros and Cons
-
Pros
GRPC is ready to be included in the unity project, it will be useful for handling server streams like in-game update resources
-
Cons
There are several features that are not supported on the Grpc Client Web
- Server to Client Stream not supported
- Bi-directional stream between server and client not supported
Installation
This tutorial is intended for installation in unity because there are several steps that are not maintained by official unity. First, we need to download the NuGet package manually on the website and import it to the unity project, but thankfully we found NuGet package management plugins.
-
Install Nuget Package on Unity by GlitchEnzo : Github Link
NuGetForUnity version that using on this installation is NugetForUnity.3.0.5.unitypackage
-
After NuGetForUnity is installed, Click the NuGet menu on the toolbar and select Manage Nuget Packages like the picture below
and then install these packages
-
GRPC.Client
-
GRPC.Client.Web
-
Google Protobuf
make sure all packages are already installed on Packages Folder
-
-
Install UnityGRPCTools that we custom from https://github.com/FearlessHyena/unity-basic-grpc, we take the proto compiler and modified a little bit so we donβt need the whole packages from them
after the package is installed make sure these files are included in the unity project
-
Create Protos folder on Assets and then create Proto File and name it with Greet.Proto, for example we will make simple client to server request like on
Net.Core
tutorialThe created file will look like this on default
Cope code below and replace code on Greet.proto
syntax = "proto3"; option csharp_namespace = "GrpcGreeterClient"; package greet; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply); } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings. message HelloReply { string message = 1; }
and then generate proto code on the toolbar, wait until the log showing that compile already finish
after compilation finishes, it will generate 2 scripts like this, in case these files are not show up you can refresh the folder
-
Create Scripts folder on Assets and then create GreetManager.cs, replace the content with the code below, and then attach it to game object
using Grpc.Net.Client; using Grpc.Net.Client.Web; using GrpcGreeterClient; using System.Net.Http; using UnityEngine; public class GreeterManager : MonoBehaviour { private Greeter.GreeterClient _greeterClient; private GrpcChannel _channel; private readonly string _server = "https://localhost:5001"; void Start() { _channel = GrpcChannel.ForAddress(_server, new GrpcChannelOptions { HttpHandler = new GrpcWebHandler(new HttpClientHandler()) }); _greeterClient = new Greeter.GreeterClient(_channel); var response = _greeterClient.SayHello(new HelloRequest { Name = name }); Debug.Log($"Response Grpc : {response.Message}"); } }
-
Run the scene, if there is no error the unity project successfully connects to grpc server like the picture below