Search Unity

MonoPInvokeCallback in unity?

Discussion in 'iOS and tvOS' started by Kaiser, Apr 17, 2012.

  1. Kaiser

    Kaiser

    Joined:
    Nov 30, 2011
    Posts:
    12
    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
     
    Last edited: Apr 17, 2012
  2. pushxtonotdie

    pushxtonotdie

    Joined:
    Oct 21, 2010
    Posts:
    100
    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.
     
    Last edited: Mar 11, 2013
  3. wihu

    wihu

    Joined:
    Nov 22, 2012
    Posts:
    23
    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?
     
  4. richoncode

    richoncode

    Joined:
    Sep 16, 2016
    Posts:
    1
    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);
     
  5. heru_

    heru_

    Joined:
    Jun 7, 2017
    Posts:
    2
    using AOT;

     
    LXThundr and MinsukCha like this.
  6. Marquo

    Marquo

    Joined:
    Aug 24, 2013
    Posts:
    3
    Note that MonoPInvokeCallback is in the AOT namespace
     
    StridingDragon likes this.
  7. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    76
    @Marquo That was actually the crucial answer. Thank you!

    Writing
    [AOT.MonoPInvokeCallback (typeof(AceCallback))]
    instead of
    [MonoPInvokeCallback (typeof(AceCallback))]
    fixed the compilation error.
     
unityunity