Skip to main content

gRPC in Unity3D

✍ Last Updated : November 25, 2022

πŸšͺ Prequisite Knowledge (Optional)

UploadDescribe InInitial ProgressKnowledge 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

Sign in

Net Core GRPC

https://learn.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.1&tabs=visual-studio

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.

  1. Install Nuget Package on Unity by GlitchEnzo : Github Link

    NuGetForUnity version that using on this installation is NugetForUnity.3.0.5.unitypackage

  2. After NuGetForUnity is installed, Click the NuGet menu on the toolbar and select Manage Nuget Packages like the picture below

    1.png

    and then install these packages

    • GRPC.Client

      2.png

    • GRPC.Client.Web

      4.png

    • Google Protobuf

      4-3.png

    make sure all packages are already installed on Packages Folder

    4-2.png

  3. 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

    UnityGrpcTools.unitypackage

    after the package is installed make sure these files are included in the unity project

    5.png

  4. 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 tutorial

    6.png

    The created file will look like this on default

    7.png

    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

    8.png

    9.png

    after compilation finishes, it will generate 2 scripts like this, in case these files are not show up you can refresh the folder

    10.png

  5. 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}");
        }
    }
    

    11.png

  6. Run the scene, if there is no error the unity project successfully connects to grpc server like the picture below

    12.png