- Home /
How to use native C++ classes in Unity
Hi everyone,
I've searched for days but couldn't find a answer for this question.
Limitations apply to my case:
1. Target platform: PC
2. Unity Indie
3. Having the C++ source code.
I'm currently using a C# wrapper dll which depends on native dlls and it works well in Unity Indie (it's OpenCvSharp, if you like to know). Thus please do not answer Pro is required and see the Plugin page.
So I believe I can do it too. Compile the C++ dll and create a C# wrapper dll for it. I'm trying to do this with SWIG. I putted all SWIG generated .cs files in a VS project targeting .Net_2.0, compiled a C# dll, put it in Unity and Mono editor can auto-complete methods in the class. Is it working?
No, the compiler complains:
EntryPointNotFoundException: SWIGRegisterExceptionCallbacks_MaxFlowGraph
MaxFlowGraph is the namespace of C++ code.
And I'm stuck here. Any help will be appreaciated. MERRY CHRISTMAS!
--------------Update---------------
Thank you Dreamora.
I finally got the compiler errors away, but now facing an other problem.
When I do this:
Graph g = new Graph();
int k = g.AddNode();
It's fine, and I tried Debug.Log(k) and it seems working right. So I think I can call functions and get returning values.
But when I try to pass a value to the dll, Unity will crash:
g.AddNode(num_of_nodes);
No idea why I can receive parameters but cannot pass them.
Answer by Dreamora · Dec 26, 2011 at 04:54 PM
if you use a c# -> native code wrapper you have to put the native code library in the right place (which isn't Plugins due to your inbetween layer to trick out the broken interop services check of unity). the easiest place is a folder thats in the global path environment variable, if thats not fine you have to tinker with it to find the right place as it depends on the referencing in your wrapper.
Thanks Dreamora, I did placed the native C++ dll in the right place, it turns out that I should deal with those SWIGTYPE_p_{TYPE}. Fixed it and now facing new problem. I guess my new problem is more general... not sure.