- Home /
how to create a wrapper for C++ class libraries
i want to use some C++ libraries in unity. unity just can use C style function libraries. how should i wrap the classes? think i want to use physx cloth or any other library. unity already do that with physx and some other libraries so it's possible. i know that C++/CLI is not an option here because they are mixed assemblies and mono doesn't support them. my target is windows. some of game libraries are static .lib files and some of them are DLLs. most of the third party game related libraries are C++ class libraries so we all need this. i know about some tools to create wrappers but i want to do that myself programmaticly because tools don't work well always.
Answer by jonas-echterhoff · Jan 09, 2010 at 05:30 PM
When you are targeting Mac/PC standalones, then you can write plugins for Unity in C - and use those to access your C++ libraries.
Read the docs to learn how.
i am targeting windows and i know what you said but i want to know what C program should i write to wrap a class library correctly? shoul i keep static references to instances of classes or ...?
I guess there a multiple approaches here, depending on what you need. If you just need to access a single manager class, you can just keep a static reference to that in your C program. If you need to expose the use of instances of classes to C#, you could just cast the instance pointer values to ints and pass those to C# and then back to C++.
can you tell me how unity uses physx or openAL? is there any general approach for creating wrappers?
The use of PhysX or OpenAL (now F$$anonymous$$OD) in Unity is somewhat different, as these libraries are not wrapped and accessed from the C# code, but are part of Unity's C++ core - so the situation is not really comparable.
But, really, wrapping such libraries to C# should be well possible - for OpenAL, which is in C, you could directly map every function to C# as they are - for C++ libraries like PhysX, you can just pass the object instance as another parameter.
you are right. thank you so much. i'll take a look at wrapper generators like swig and also i should take a look at some open source wrappers. the real library that i love to wrap is a weather library called simulweather. i encourage you to take a look at simulweather you might love to have such an advanced weather system in unity 3 :)