Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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:
    110
    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:
    78
    @Marquo That was actually the crucial answer. Thank you!

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

    arunjose007

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

    ShantiB95

    Joined:
    Feb 8, 2017
    Posts:
    3
    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:
    2,806
    The easiest way to call C# from Java is via AndroidJavaProxy.
     
unityunity