• 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 Nathan7 · Sep 13, 2011 at 04:09 PM · iosplugin

Developing Plugins for iOS

Hey everyone,

I've been building a plugin associated with a C++ .dll. Thus far, it's Windows-only, but I'd like to leverage the same plugin on iOS, reusing whatever work possible. Documentation and information on iOS plugins seems to be lacking on the whole so I have a few questions about how things are supposed to be setup for iOS.

Initially, I just tried the same project, plugin, and .dll on Mac OSX within Unity but I receive a "DllNotFoundException," even though the file is there. Is this because Unity can't recognize the .dll file on Mac OSX? What type should the .dll file be to work on iOS (.bundle? .a?)?

Should I use the same DLLImport directive with that file type? The Plugin documentation mentions using "[DllImport ("__Internal")]" but I'm not clear whether all methods need to be defined like this or if this is referencing a particular library/dll named "__Internal".

I've also seen a couple different threads about limitations of iOS plugins. Is there anything in particular to keep in mind?

If anyone knows of a good sample plugin for iOS that includes the code with the library it's calling into, I would love to see it.

Thanks!

Comment
Add comment
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

1 Reply

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

Answer by benjamin_schulz · Sep 14, 2011 at 05:20 PM

Hello,

when developing plugins for iOS, your code will not be in a seperate binary (.dll) all code will be in one binary. This is the reason, why you do [DllImport ("__Internal")]. __Internal stands for the name of the currently running executable.

The easiest way to begin development is to put the source for your plugin in Assets/Plugins/iOS. But subdirectories do not work! The files in that directory get sym-linked into the exported xcode project. I was not able to edit the files within xcode (they are write protected due to the fact that they are sym-links) and I found the workflow rather painful.

But anyways, lets say we would have:

Assets/Plugins/iOS/my_plugin.cpp:

 extern "C" {
   int foo(int x)  //pointless demo function
   {
     return x*10;
   }

   char* bar()  //another rather useless demo function
   {
     ret = malloc(10);
     strcpy(ret, "Muh!");
     return ret; //apparently this is not a leak, because it gets freed by mono !!!
   }

 }

You would do:

 class MyPlugin
 {
     [DllImport ("__Internal")]
     public static extern int foo (int x);
 }

Now if you call MyPlugin.foo(10) the function on the c++ side gets called.

If you are able to wrap the interface of your plugin in only basic datatypes that might be all you need. Even passing and returning of char* does work (as long as returned strings are allocated on the heap!!!!). But if you're in need of passing more complex datastructures I'd suggest you serialize them to json/xml on one side and deserialize them on the other side.

Sincerely -- Benjamin

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is there a way to reset rendering after showing an iOS modal window? 0 Answers

Admob Plugin Unity IOS 4 Answers

why screen freezes when i press the home button? Help! 0 Answers

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

iOS plugin and contact view 0 Answers


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