- Home /
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!
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
Your answer
Follow this Question
Related Questions
How do I remove Google Plus from iOS Build only? 1 Answer
Plugins colliding with each other. 0 Answers
Call Unity class in XCode 1 Answer
iOS Launch Options 0 Answers