Hello, I need to do native-to-managed calls and I read about MonoPInvokeCallback attribute which would let me use my delegates. http://docs.go-mono.com/?link=T:MonoTouch.MonoPInvokeCallbackAttribute When I try to use it like the example it says: "The type or namespace name MonoPInvokeCallback could not be found. Are you missing a using directive or an assembly reference?" Is this not supported by Unity? EDIT: I tried to define it myself (according to an answer on http://answers.unity3d.com/questions/191234/unity-ios-function-pointers.html) but then Unity fails to crosscompile: "Cross compilation job Assembly-CSharp-firstpass.dll failed". Am using .net 2.0 subset, no stripping
Old thread, but for those who have this issue, the MonoPInvokeCallbackAttribute method does work on iOS. We had the same issue with the cross compilation fail, and that turned out to be related to the use of the [UnmanagedFunctionPointer(CallingConvention.Cdecl)] attribute on our delegate. By removing this (i believe unnecessary) attribute, our code ran without issue. An example of a production SDK that is using this is the Sphero plugin. https://github.com/orbotix/UNITY-PLUGIN/blob/master/Plugins/Sphero/SpheroDeviceMessenger.cs As luck would have it, I own a sphero and their use of MonoPInvokeCallback worked in their project, too.
I had problem making reverse callback from ios plugins, getting exception: attempting jit compile with aot only option. This solution is working, thanks. Does this work on Android also?
Great reference. Also I found that the string passed to DllImport can be defined as a const string. This eliminates the replicated ifdefs around the import declarations. #if UNITY_IPHONE const string DynLib = "__Internal"; #else const string DynLib "VoconWrap"; #endif [DllImport(DynLib, EntryPoint = "sayCommand")] private static extern int sayCommand(IntPtr ptrEngine);
@Marquo That was actually the crucial answer. Thank you! Writing [AOT.MonoPInvokeCallback (typeof(AceCallback))] instead of [MonoPInvokeCallback (typeof(AceCallback))] fixed the compilation error.
yea [AOT.MonoPInvokeCallback (typeof(AceCallback))] did the trickt thanks @Marquo and @StridingDragon
Did you find, how to do this for Android? How do we invoke the C# static method from native (Java) side? (Other than UnitySendMessage)