This page will help you understand how to use Grpc for unity step by step and all the script in inside the HelloWorld demoscene.

Untitled

But Before Start:

  1. Define your .proto file. For example:

    syntax = "proto3";
    
    package helloworld;
    
    service Greeter{
     rpc SayHello(HelloRequest) returns (HelloReply){}
    }
    
    message HelloRequest{
     string name =1;
    }
    
    message HelloReply{
     string message=1;
    }
    

    For more information about protobuf, please check official website https://developers.google.com/protocol-buffers/docs/csharptutorial.

  2. Place your .proto file inside Unity folder, and use our built-in tool to compile protobuf class. For more info about protobuf compilation tool, please check here.

    It should contains the .proto file, and 2 generated c# class. Note We already pre-compile the example helloworld.proto class for you.

    Untitled

Grpc Server Base:

  1. Create a new c# class and inherient GrpcServerBase.

    Untitled

  2. You need to bind the proto service with your custom class, for example:

    Untitled

Grpc Client Base:

  1. Create a new c# class and inherient GrpcClientBase.

    Untitled

    SendGrpc() is the function that need to implement, which send the Rpc info to the server.

    SayHello() - The function name you define in .proto file.

    TRequest - The generic request you define in .protoc file which is the Hello Request.


You are now ready to GO:

  1. Create a new c# monobehavior script and declare the grpc variable: