Unity
MagicOnion also supports integration with Unity. This includes support for IL2CPP as well as features to make it easier to use MagicOnion in Unity applications.
gRPC channel management integration for Unity
Wraps gRPC channels and provides a mechanism to manage them with Unity's lifecycle. This prevents your application and the Unity Editor from freezing by releasing channels and StreamingHub in one place.
The editor extension also provides the ability to display the communication status of channels.
The data rate is calculated only for the message body of methods, and does not include Headers, Trailers, or heartbeats.
APIs
MagicOnion.GrpcChannelx
classGrpcChannelx.ForTarget(GrpcChannelTarget)
methodGrpcChannelx.ForAddress(Uri)
methodGrpcChannelx.ForAddress(string)
method
MagicOnion.Unity.GrpcChannelProviderHost
classGrpcChannelProviderHost.Initialize(IGrpcChannelProvider)
method
MagicOnion.Unity.IGrpcChannelProvider
interfaceDefaultGrpcChannelProvider
classLoggingGrpcChannelProvider
class
Usages
1. Prepare to use GrpcChannelx
in your Unity project.
Before creating a channel in your application, you need to initialize the provider host to be managed.
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void OnRuntimeInitialize()
{
// Initialize gRPC channel provider when the application is loaded.
GrpcChannelProviderHost.Initialize(new DefaultGrpcChannelProvider(() => new GrpcChannelOptions()
{
HttpHandler = new YetAnotherHttpHandler()
{
Http2Only = true,
},
DisposeHttpClient = true,
}));
}
GrpcChannelProviderHost will be created as DontDestroyOnLoad and keeps existing while the application is running. DO NOT destroy it.
2. Use GrpcChannelx.ForTarget
or GrpcChannelx.ForAddress
to create a channel.
Use GrpcChannelx.ForTarget
or GrpcChannelx.ForAddress
to create a channel instead of GrpcChannel
.
var channel = GrpcChannelx.ForTarget(new GrpcChannelTarget("localhost", 12345, isInsecure: true));
// or
var channel = GrpcChannelx.ForAddress("http://localhost:12345");
3. Use the channel instead of GrpcChannel
.
var channel = GrpcChannelx.ForAddress("http://localhost:12345");
var serviceClient = MagicOnionClient.Create<IGreeterService>(channel);
var hubClient = StreamingHubClient.ConnectAsync<IGreeterHub, IGreeterHubReceiver>(channel, this);
Extensions for Unity Editor (Editor Window & Inspector)
To open the channel window, go to Window
-> MagicOnion
-> gRPC Channels
.