I am trying to use some C code in Unity3D pro.
My basic approach is to (1) build C source code into a static libary (2) create an empty bundle and link against the library (3) import the bundle into Unity 3D pro (as a plugin).
I was basically following this tutorial.
It has been working for me for a while until recently when I was updating my C code and recompiling and rebuilding the library and the bundle, everything goes wrong...
When I first imported the bundle, I got a DllNotFoundException.
At that stage my bundle project was merely a pile of libraries - the library built from my source code (which is a .a file) and a few other libraries that I think the .a library depends on (the other libraries have .dylib extension).
I added one test.c file to the bundle. The test .c file contains only two lines:
#include <stdio.h>
#include "ccn.h"
char* say()
{
return "hi";
}
in which ccn.h is a header file from my C source code.
After I re-built the bundle, the DllNotFoundExeption was gone, but I got an EntryPointNotFoundException.
Lots and Lots of thanks!
Also I saw something on Apple's documentation that I don't quite understand:
If your loadable bundle is a plug-in, the host application developer usually provides an interface for the plug-in architecture in a framework. This framework typically contains a class that all plug-in principal classes inherit from, or a protocol (formal or informal) for the plug-in’s principal class to adopt.
I am pretty sure the loadable bundle I create is a plugin. I am importing the bundle into Unity3D. Does that mean I should define a principal class from Unity? How?
The problem has a simple solution:
try out whatever function that is giving you the EntryPointNotFoundException in the test.c file first.
By "try out" I mean, have the function called in your main() and test it out in C first.
In my case, the function that throws out the EntryPointNotFoundException is called ccn_run(). So I did the following things:
I tried it out and the function is working alright in C, now the magical thing is that when I rebuild the bundle (without deleting the main() function and the ccn_run() call inside it), Unity can call the ccn_run() function without problem!
I couldn't explain why this solves my problem, but this has been working for me for almost a month now. Not long ago Xcode upgraded to 4.3.2 and I tried the same approach again, still works. I guess perhaps adding a main() function helps defining the entry point?
Finally, I documented the whole process of importing C code to Unity and I am glad to share here: https://docs.google.com/document/d/1vAeupNQlBTY3y3Bma5Jo_Hs4JRYm6FoEc4vQN0WBGNg/edit
My code sample:
Hope this answer especially my tutorial helps!