-
Notifications
You must be signed in to change notification settings - Fork 1
Handle Android Plugin's Callback in Unity3d without SendUnityMessage
Stanislav Osipov edited this page May 3, 2020
·
2 revisions
When using SendUnityMessage you have to make sure that GameObject
with a specific name exists and make sure you have the right script attached to it.
The dev kit enables you to pass the C# Action as a function parameter. See the sample below.
Let's say we have this CallbackSample
class on Java side:
package com.stansassets.android.samples;
import com.stansassets.android.IUnityCallback;
import com.stansassets.android.UnityBridge;
public class CallbackSample {
public static class CallbackDataSample {
String m_StringData;
int m_IntData;
}
public static void ModelCallbackSample(final IUnityCallback callback) {
CallbackDataSample dataSample = new CallbackDataSample();
dataSample.m_StringData = "My String data";
dataSample.m_IntData = 100;
UnityBridge.sendCallback(callback, dataSample);
}
}
Now let's create the same CallbackDataSample
model on the C# Unity side. The plugin uses Unity JSONUtility under the hood, so make sure the model is serializable.
[Serializable]
public class CallbackDataSample
{
[SerializeField]
string m_StringData = default;
[SerializeField]
int m_IntData = default;
public int IntData => m_IntData;
public string StringData => m_StringData;
}
The last step is to call ModelCallbackSample
method from the CallbackSample
java class.
public static class CallbackSample
{
const string k_FullJavaClassName = "com.stansassets.android.samples.CallbackSample";
public static void ModelCallbackSample(int x, int y, string name, Action<CallbackDataSample> callback)
{
var requestBuilder = new JavaRequestBuilder(k_FullJavaClassName, nameof(ModelCallbackSample));
requestBuilder.AddArgument(x);
requestBuilder.AddArgument(y);
requestBuilder.AddArgument(name);
requestBuilder.AddAction(callback);
requestBuilder.Invoke();
}
}
Reliable and high-quality Unity Development service. Let's Talk!
Website | AssetStore | LinkedIn | Youtube | Scripting Reference