- Home /
Callbacks from C to C# are not working in 5.4.0f3
Here is my simple flow of callback from ios to unity.
private delegate void CallbackDelegate();
[DllImport("__Internal")]
private static extern void PluginFunction(CallbackDelegate callback);
void Start()
{
PluginFunction(CallbackMethod);
}
[MonoPInvokeCallback(typeof(CallbackDelegate))]
void CallbackMethod()
{
Debug.Log("Working...");
}
C side code
extern "C" typedef void (*CallBackFuncP) ();
extern "C" {
void PluginFunction(CallBackFuncP callback) {
callback();
}
}
I am getting this exception after upgrading to Unity 5.4.0f3 working on previous version
Blockquote NotSupportedException: IL2CPP does not support marshaling delegates that point to instance methods to native code.
What platform? It seems that the IL to C++ compiler (IL2CPP) doesn't support passing managed delegates to native code. This is most likely platform dependent since Unity doesn't use IL2CPP for all platforms.
I'm on iOS platform and trying to run it on iPod 6 with os version 9.1
Answer by JoshPeterson · Aug 24, 2016 at 11:33 AM
You'll need to change CallbackMethod
to be defined like this:
[MonoPInvokeCallback(typeof(CallbackDelegate))]
static void CallbackMethod()
{
Debug.Log("Working...");
}
For both IL2CPP and Mono on an AOT platform (like iOS) it is not possible to marshal an instance method to a function pointer that can be called from native code. Only static methods in C# code be called from native code like this.