Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    111
    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:
    25
    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);
     
    HofiOne likes this.
  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
     
    JoeStrout and StridingDragon like this.
  7. StridingDragon

    StridingDragon

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

    Writing
    [AOT.MonoPInvokeCallback (typeof(AceCallback))]
    instead of
    [MonoPInvokeCallback (typeof(AceCallback))]
    fixed the compilation error.
     
    HofiOne likes this.
  8. arunjose007

    arunjose007

    Joined:
    Apr 28, 2020
    Posts:
    5
  9. ShantiB95

    ShantiB95

    Joined:
    Feb 8, 2017
    Posts:
    17
    Did you find, how to do this for Android?
    How do we invoke the C# static method from native (Java) side? (Other than UnitySendMessage)
     
    Last edited: Aug 18, 2020
  10. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    The easiest way to call C# from Java is via AndroidJavaProxy.