Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hassanalic · Aug 13, 2016 at 04:33 PM · iospluginil2cpp

Callbacks from C to C# are not working in 5.4.0f3

Here is my simple flow of callback from ios to unity.

 private delegate void CallbackDelegate();
 
 [DllImport("__Internal")]
 private static extern void PluginFunction(CallbackDelegate callback);
 
 void Start()
 {
     PluginFunction(CallbackMethod);
 }
 
 [MonoPInvokeCallback(typeof(CallbackDelegate))]
 void CallbackMethod()
 {
    Debug.Log("Working...");
 }

C side code

 extern "C" typedef void (*CallBackFuncP) ();
 
 extern "C" {
     void PluginFunction(CallBackFuncP callback) {
         callback();
     }
 }

I am getting this exception after upgrading to Unity 5.4.0f3 working on previous version

Blockquote NotSupportedException: IL2CPP does not support marshaling delegates that point to instance methods to native code.

Comment
Add comment · Show 2
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 Bunny83 · Aug 13, 2016 at 06:20 PM 0
Share

What platform? It seems that the IL to C++ compiler (IL2CPP) doesn't support passing managed delegates to native code. This is most likely platform dependent since Unity doesn't use IL2CPP for all platforms.

avatar image hassanalic Bunny83 · Aug 15, 2016 at 06:16 AM 0
Share

I'm on iOS platform and trying to run it on iPod 6 with os version 9.1

1 Reply

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

Answer by JoshPeterson · Aug 24, 2016 at 11:33 AM

You'll need to change CallbackMethod to be defined like this:

  [MonoPInvokeCallback(typeof(CallbackDelegate))]
  static void CallbackMethod()
  {
     Debug.Log("Working...");
  }

For both IL2CPP and Mono on an AOT platform (like iOS) it is not possible to marshal an instance method to a function pointer that can be called from native code. Only static methods in C# code be called from native code like this.

Comment
Add comment · Show 1 · 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 hassanalic · Aug 24, 2016 at 12:21 PM 0
Share

thanks for your solution. it works

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

IL2CPP C# passed ref float[] to C++ can't get right result 1 Answer

What difference IL2CPP make for native plugins? 1 Answer

I18N.dll and I18N.CJK.dll don't work with IL2CPP 1 Answer

Unity sounds not playing after AVAudioSession is disabled 1 Answer

What does MapFileParser do? 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