Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ronewolf · Dec 02, 2011 at 08:45 PM · iosiphonenative plugindelegate

Unity iOS - function pointers

It seems that my C SDKs may be unusable for Unity iOS (without a bit of an overhaul), but I wanted to verify to be sure. The long story short: I can compile and run my Unity sample that uses native code with function pointers for callbacks, but when it comes time to trigger my C# callback I get the following error: ExecutionEngineException: Attempting to JIT compile method '(wrapper native-to-managed) gamespySample.CreateUserAccount:createuserCallback (Gamespy.GHTTPResult,intptr,intptr)' while running with --aot-only.

The same code works on PC (with c code compiled into a DLL) and Android (with c code compiled into a .so), but it seems the iOS is limited to aot compiling which does not allow for this function pointer-to-delegate marshaling. Some web searching led me to this issue being a known limitation of MonoDevelop/iOS, which can be solved by using the 'MonoPInvokeCallback' attribute (within the MonoTouch namespace) for the C# delegate, but it seems this namespace is unavailable within the Unity version of MonoDevelop.

Is it in fact not possible to use function pointers in Unity iOS? If this is the case, I presume I'll need to write a layer of c wrappers that handle the function pointer callbacks and just return the needed data (after getting polled).

For reference, I'll include an example function below: C declaration: WSCreateUserAccountValue wsCreateUserAccount(..., WSCreateUserAccountCallback userCallback);

where WSCreateUserAccountCallback is the function pointer: *typedef void (WSCreateUserAccountCallback)(...);

Then in our C# wrapper we have the following declarations: [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)] public delegate void WSCreateUserAccountCallback(GHTTPResult httpResult, IntPtr theResponse, IntPtr userData); [DllImport ("__Internal")] public static extern void wsCreateUserAccount(..., WSCreateUserAccountCallback callback);

And lastly here's our C# invokation: WSCreateUserAccountCallback myCreateUserAccountCallback = new WSCreateUserAccountCallback(createuserCallback); wsCreateUserAccount(..., myCreateUserAccountCallback);

where createuserCallback is defined like: public static void createuserCallback(...) { ... }

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nathan7 · Dec 02, 2011 at 09:15 PM 0
Share

I'm running into this issue as well. Would be great to have a work around without using UnitySend$$anonymous$$essage or something similar.

avatar image ankur6ue · Dec 05, 2011 at 04:00 PM 0
Share

I ran into the exact same issue as well. Currently using a queue to solve the issue. It will be great to have delegates working on iOS

avatar image Muzz5 · Dec 05, 2011 at 05:50 PM 0
Share
  • Answers! As ever, QATO is broken.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dreamora · Dec 23, 2011 at 10:19 AM

Callbacks from stuff running in own threads actually are never an option. Unity is not callback safe so if the GameSpy library calls into a UnityEngine.Object derived class (monobehaviour / scriptable object) unity will simply crash on a multicore machine as the ipad2 / iphone 4s.

You should write a C layer inbetween that does the callback and then uses the iOS Unity exposed UnitySendMessage to inform the inside of unity to stay safe and clean

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image konistehrad · Dec 23, 2011 at 08:50 PM 0
Share

Your point is well-met, but personally I'd rather use normal thread lock procedures and retain the flexibility of real, well-typed callback arguments than have to condense all the callback arguments into a string as mandated by UnitySend$$anonymous$$essage. If you're only ever returning single values easily encapsulated in a string, however, UnitySend$$anonymous$$essage is most certainly the safer option.

avatar image Dreamora · Dec 25, 2011 at 12:00 PM 1
Share

Unity does not respect locks, as such you can decide to either not use a distinct thread in a native code library (means the library itself is in the same thread and you don't use os level asyncs with callbacks) or you have to call into unity through UnitySend$$anonymous$$essage or a 'polling approach' and store the 'callback event' inside the native code layer.

there is no other option or alternative. Either you are fully in sync in the plugin (its driven through calls from unity basically and only operates when told to) when you call back in or you will kill unity flat out.

avatar image konistehrad · Dec 25, 2011 at 05:00 PM 0
Share

This is very good to know; I had no idea that Unity didn't respect locks. I figured if I put a locked flag in the Update loop I would be able to pick up the required data for consumption after it was prepped, but if that's not the case I'm definitely going to have to re-evaluate my solution. Thanks again!

avatar image ronewolf · Jan 17, 2012 at 10:12 PM 0
Share

Thanks for the feedback Dreamora. We've since upgraded our SD$$anonymous$$s to be full C#, so we no longer need the C plugins. Is there still an issue with using a callback to return data (rather than polling or using UnitySend$$anonymous$$essage), or is the issue you described specific to the native code plugin scenario?

avatar image
4

Answer by konistehrad · Dec 23, 2011 at 05:11 AM

According to the patch to Mono that implements the MonoPInvokeCallback attribute, it's by name and not by any specific marker on the attribute! This means you can reimplement the Attribute with the correct method signature and it'll still work. The implementation looks like this:

 public class MonoPInvokeCallbackAttribute : System.Attribute
 {
     private Type type;
     public MonoPInvokeCallbackAttribute( Type t ) { type = t; }
 }

Simply throw this attribute on a static method and your native code can now access your managed delegates. :)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BaoyuMou · Jul 03, 2012 at 09:10 AM 0
Share

I use this for Unity 3.5.1, work good, thanks :)

avatar image Numid · Aug 15, 2012 at 12:42 PM 0
Share

For this use case (Objective-C/$$anonymous$$ono callbacks), limitations are listed here: http://docs.xamarin.com/ios/about/limitations#section_8.

avatar image paleozogt · Jan 25, 2013 at 12:12 AM 0
Share

corroborating evidence: http://stackoverflow.com/questions/7058254/calling-c-sharp-function-from-c

avatar image jarl-ostensen · Apr 16, 2014 at 02:40 PM 1
Share

I can confirm that this also works for 4.3.4

avatar image larku · Jul 14, 2014 at 03:13 AM 0
Share

Thanks konistehrad, you saved me here. Works for me on Unity 4.2.

avatar image
0

Answer by miraclestar · Nov 04, 2016 at 08:35 AM

good , works fine in unity 5.4.1p4

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity iOS sdk question 1 Answer

iOS Shader - Emulator vs Device Inconsistency 0 Answers

Unity iPhone: AddForce to Object after Touch 0 Answers

Three ingame buttons conflict with each other on iOS 0 Answers

Specifying a high quality background image for iPad 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges