C++ Libraries in Unity: How To

Ok so I have a C++ library I want to use inside of Unity. I have it inside of Visual Studio, I’ve built it into a DLL, and imported into Unity. How the heck do I call the functions?

Do I need to make it a managed/native plugin? Note that the functions I want have custom classes as parameters. Do I need to add extra code to tell the Visual Studio how to export the classes and functions? There is an implementation of the library in Python, would that ever be the best route available? I’m really confused by documentation saying it’s possible or simple, because OpenCV is a dependency of this library and it is open source, free to use, but is $100 on the asset store as a plugin… why?

What is the best route of attack on this problem and what documentation should I be looking at?

Any insight is appreciated, thank you!

It’s not a managed plugin you want (that’d be in C#) but a “native plugin” (anything that compiles to assembler): Unity - Manual: Native plug-ins
You do have to mark the methods you expose, in a certain way, albeit that’s not purely Unity specific.

The OpenCV plugin is not the cheapest because it includes a full fledged bridge into Unity I believe. E.g. supporting to use Unity’s Texture2D in OpenCV which normally would expose only some C++ texture struct. Also it’s a massive library, so writing meaningful C# wrappers for everything surely took ages.

P.s. ChatGPT knows how to write Unity-Compatible DLLs and also how to access DLLs from C# (used it for that just the other week).

Thanks for the reply! I think the most confusing part for me is:

writing meaningful C# wrappers for everything

What parts do I need to do that for? If I’m implementing a C++ function that has custom classes as parameters, do I need to write C# wrappers for those custom classes? And what if those classes implement more classes? It feels like I’d have to rewrite everything in C#, so what’s even the point?

And if there’s a particular page that discusses how this shakes out clearly I’d love to know!

OMG I just remembered that my sleep-brain told me I should ask ChatGPT but I totally forgot to do that :smiley:

Using Native libraries is not convenient. Period.

There simply is a firm boundry. You cannot pass C# classes to Cpp and vica versa. You can only pass native types like int, double, char. Strings already are trickier albeit it is possible. The process is called “marshalling”. Main reason for this hassle is that a managed language like C# does not use fixed memory adress for data while C++ does.
It is possible to circumvent this problem, just takes effort.

As to what’s the point:
Either what you want simply only exists in C++ and rewriting that in C# would take too much effort.
And/or you need the performance of native code.

In this case both applies. OpenCV is a huge knowledge base and implementing image processing in C# would be far far slower. In case of image processing, easily factor 50 or even 100.

Performance is also the reason why a lot of Unity’s core is C++ and wrapped in C# for convenience (because large scale game dev in C++ sucks or at least is far less beginnerfriendly).

That said, while I’d certainly not try video processing, image processing is acceptably possible in Unity via Jobs and the Boost compiler. You will quickly notice that this has a similar kind of limitations regarding what data you can move around (albeit not as severe and you can easily use the C# debugger at least by disabling boost compilation).
Using GPU processing e.g. via compute shaders would also be an option (which can even easily surpass C++).

Edit: Don’t know a particular page off the top of my head but keep in mind this is hardly an Unity topic - it’s generally about using DLLs from a C# application. You should find stuff for that.

I see, thank you very much! Looking into compute shaders, this might be PERFECT for what I’m trying to do (identify saddle points from live phone camera video). I might end up learning more than what I bargained for :smiley: