GeneralCmdScene.PNG

We provide another demo scene (GeneralCmdScene.unity) which allow you to send different class and command to the server.

The General Command Proto:

syntax = "proto3";

package GeneralCommand;

service CommandClass{
 rpc SendCommand(CmdRequest) returns (CmdReply){}
}

message CmdRequest{
 string CmdName =1;
 string CmdData =2;
}

message CmdReply{
 string CmdName =1;
 string CmdReplyData =2;
}

The core idea is passing the rpc through cmd name (class name) and cmd data (serialize with json). Then convert to any c# class and no need to define too many .proto function. For example, we define 3 different type of class structure and it allow to transfer by using Grpc.

Untitled


Then we using the JsonUtility Tool to Send Different Rpc Data:

private void SendCharacterRpc()
{
    GrpcClient.SendGrpc(new CmdRequest
    {
        CmdName = nameof(HeroInfo),
        CmdData = JsonUtility.ToJson(new HeroInfo
        {
            Name = CharacterDropdown.options[CharacterDropdown.value].ToString(),
            Hp = 100,
            Type = (HeroType)CharacterDropdown.value
        })
    });
}
private void SendWeaponRpc()
{
    GrpcClient.SendGrpc(new CmdRequest
    {
        CmdName = nameof(WeaponInfo),
        CmdData = JsonUtility.ToJson(new WeaponInfo
        {
            Name = WeaponDropdown.options[WeaponDropdown.value].ToString(),
            Damage = 1000,
            Type = (WeaponType)WeaponDropdown.value
        })
    });
}
private void SendEquipRpc()
{
    GrpcClient.SendGrpc(new CmdRequest
    {
        CmdName = nameof(EquipmentInfo),
        CmdData = JsonUtility.ToJson(new EquipmentInfo
        {
            Name = EquipDropdown.options[EquipDropdown.value].ToString(),
            Value = 500,
            Type = (EquipmentType)EquipDropdown.value
        })
    });
}

Then we can convert back the json data to custom class when receive Rpc call.

var heroInfo = JsonUtility.FromJson<HeroInfo>(data);

var weaponInfo = JsonUtility.FromJson<WeaponInfo>(data);

var equipmentInfo = JsonUtility.FromJson<EquipmentInfo>(data);


File Transfer Demo Scene