🌏 한국어
- Unity provides the feature that enables transmitting data to/from Native.
- Android uses AndroidJavaClass and iOS uses DllImport to connect with Native.
- Communicator provides Unity and Native with a common interface that allows data to be transmitted and received to facilitate data transmission.
- Install Game Package Manger
- Run : Unity Menu > Tools > GPM > Manager
- Service installation : Communicator
- Require many resources to develop plugins.
- In Unity requires a native connection by AndroidJavaClass or DllImport.
- In Native requires registering Unity's GameObject and Callback.
- For Android requires a connection with unity-classes.jar.
- Additional tasks such as registering Callback in Unity class that use native feature are required.
- For plugins developed for different purposes, most of their codes overlap with one another.
- Allows communication with Native through the unified interface.
In Unity requires a native connection by AndroidJavaClass or DllImport.In Native requires registering Unity's GameObject and Callback.For Android requires a connection with unity-classes.jar.Additional tasks such as registering Callback in Unity class that use native feature are required.
- 2018.4.0 or higher
using Gpm.Communicator;Generates a Native Class to receive a message from Unity.
- For Android, enter the full path including the package and class name.
- For iOS, enter only the class name.
API
static void InitializeClass(GpmCommunicatorVO.Configuration configuration)Example
public void Initialize()
{
GpmCommunicatorVO.Configuration configuration = new GpmCommunicatorVO.Configuration()
{
#if UNITY_ANDROID
className = "${ANDROID_CLASS_NAME}"
#elif UNITY_IOS
className = "${IOS_CLASS_NAME}"
#endif
};
GpmCommunicator.InitializeClass(configuration);
}Register a Receiver to receive a message from Native.
API
static void AddReceiver(string domain, GpmCommunicatorCallback.CommunicatorCallback callback)Example
public void AddReceiver()
{
GpmCommunicator.AddReceiver("${DOMAIN}", OnReceiver);
}
private void OnReceiver(GpmCommunicatorVO.Message message)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("OnReceiver");
sb.AppendLine("Domain" + message.domain);
sb.AppendLine("Data" + message.data);
sb.AppendLine("Extra" + message.extra);
Debug.Log(sb.ToString());
}Sends the message to Native. The process result can be immediately received as a return value.
API
public static GpmCommunicatorVO.Message CallSync(GpmCommunicatorVO.Message message)Example
public void CallSync()
{
GpmCommunicatorVO.Message message = new GpmCommunicatorVO.Message()
{
domain = "${DOMAIN}",
data = "USER_SYNC_DATA",
extra = "USER_SYNC_EXTRA"
};
GpmCommunicatorVO.Message responseMessage = GpmCommunicator.CallSync(message);
StringBuilder sb = new StringBuilder();
sb.AppendLine("CallSync Response");
sb.AppendLine("Domain : " + responseMessage.domain);
sb.AppendLine("Data : " + responseMessage.data);
sb.AppendLine("Extra : " + responseMessage.extra);
Debug.Log(sb.ToString());
}Sends the message to Native. The Receiver registered using AddReceiver API can receive the async process result.
API
static void CallAsync(GpmCommunicatorVO.Message message)Example
public void CallAsync()
{
GpmCommunicatorVO.Message message = new GpmCommunicatorVO.Message()
{
domain = "${DOMAIN}",
data = "USER_ASYNC_DATA",
extra = "USER_ASYNC_EXTRA"
};
GpmCommunicator.CallAsync(message);
}Install Communicator with GPM Manager.
-
Create a project with Android Studio. (e.g. com.gpm.communicator.sample)
-
Create a folder within the project. (e.g. Project/externalLibs)
-
Copy the Unity Assets/GPM/Communicator/Plugins/Android/GpmCommunicatorPlugin.aar file to the folder you created.
-
File/New/New Module/Android Library in Android Studio.
-
Create the GpmCommunicatorSample.java file and paste the code below.
// GpmCommunicatorSample.java
// Check the Package path.
package com.gpm.communicator.sample;
import com.gpm.communicator.Interface.GpmCommunicatorReceiver;
import com.gpm.communicator.GpmCommunicatorPlugin;
import com.gpm.communicator.vo.GpmCommunicatorMessage;
public class GpmCommunicatorSample {
private final String DOMAIN = "GPM_COMMUNICATOR_SAMPLE";
public GpmCommunicatorSample() {
// Creates a Receiver.
GpmCommunicatorReceiver listener = new GpmCommunicatorReceiver() {
@Override
public void onRequestMessageAsync(GpmCommunicatorMessage message) {
// Processes a Async Receiver.
GpmCommunicatorPlugin.sendResponseMessage(message);
}
@Override
public GpmCommunicatorMessage onRequestMessageSync(GpmCommunicatorMessage message) {
// Processes a Sync Receiver.
return message;
}
};
// Registers a Receiver.
GpmCommunicatorPlugin.addReceiver(DOMAIN, listener);
}
} dependencies {
// Add
compileOnly files('../externalLibs/GpmCommunicatorPlugin.aar')
...
}Create the file in Asset/Plugins/IOS folder of the Unity project.
// GPMCommunicatorSample.h
#import <Foundation/Foundation.h>
@interface GPMCommunicatorSample: NSObject
@end// GPMCommunicatorSample.mm
#import "GPMCommunicatorSample.h"
#import "GPMCommunicator.h"
#import "GPMCommunicatorReceiver.h"
#import "GPMCommunicatorMessage.h"
#define GPM_COMMUNICATOR_SAMPLE_DOMAIN @"GPM_COMMUNICATOR_SAMPLE"
@implementation GPMCommunicatorSample
- (id)init {
if((self = [super init]) == nil) {
return nil;
}
// Creates a Receiver.
GPMCommunicatorReceiver* receiver = [[GPMCommunicatorReceiver alloc] init];
receiver.onRequestMessageSync = ^GPMCommunicatorMessage *(GPMCommunicatorMessage *message) {
// Processes a Sync Message.
return message;
};
receiver.onRequestMessageAsync = ^(GPMCommunicatorMessage *message) {
// Processes a Async Message.
[[GPMCommunicator sharedGPMCommunicator] sendResponseWithMessage:message];
};
// Registers a Receiver.
[[GPMCommunicator sharedGPMCommunicator] addReceiverWithDomain:GPM_COMMUNICATOR_SAMPLE_DOMAIN receiver:receiver];
return self;
}
@endCreate Sample.cs
namespace Gpm.Communicator.Sample
{
using UnityEngine;
using Gpm.Communicator;
using System.Text;
public class Sample : MonoBehaviour
{
private const string DOMAIN = "GPM_COMMUNICATOR_SAMPLE";
private const string ANDROID_CLASS_NAME = "com.gpm.communicator.sample.GpmCommunicatorSample";
private const string IOS_CLASS_NAME = "GPMCommunicatorSample";
private void Awake()
{
Initialize();
AddReceiver();
}
/// <summary>
/// Initialize Native class
/// </summary>
public void Initialize()
{
GpmCommunicatorVO.Configuration configuration = new GpmCommunicatorVO.Configuration()
{
#if UNITY_ANDROID
className = ANDROID_CLASS_NAME
#elif UNITY_IOS
className = IOS_CLASS_NAME
#endif
};
GpmCommunicator.InitializeClass(configuration);
}
/// <summary>
/// Register Unity Receiver
/// </summary>
public void AddReceiver()
{
GpmCommunicator.AddReceiver(DOMAIN, OnReceiver);
}
private void OnReceiver(GpmCommunicatorVO.Message message)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("OnReceiver");
sb.AppendLine("Domain : " + message.domain);
sb.AppendLine("Data : " + message.data);
sb.AppendLine("Extra : " + message.extra);
Debug.Log(sb.ToString());
}
/// <summary>
/// Call Async
/// </summary>
public void CallAsync()
{
GpmCommunicatorVO.Message message = new GpmCommunicatorVO.Message()
{
domain = DOMAIN,
data = "USER_ASYNC_DATA",
extra = "USER_ASYNC_EXTRA"
};
GpmCommunicator.CallAsync(message);
}
/// <summary>
/// Call Sync
/// </summary>
public void CallSync()
{
GpmCommunicatorVO.Message message = new GpmCommunicatorVO.Message()
{
domain = DOMAIN,
data = "USER_SYNC_DATA",
extra = "USER_SYNC_EXTRA"
};
GpmCommunicatorVO.Message responseMessage = GpmCommunicator.CallSync(message);
StringBuilder sb = new StringBuilder();
sb.AppendLine("CallSync Response");
sb.AppendLine("Domain : " + responseMessage.domain);
sb.AppendLine("Data : " + responseMessage.data);
sb.AppendLine("Extra : " + responseMessage.extra);
Debug.Log(sb.ToString());
}
}
}




