x


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.

more ▼

asked Jan 09 '10 at 05:08 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 09 '10 at 05:30 PM

jonas echterhoff gravatar image

jonas echterhoff ♦♦
9.7k 7 23 102

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 ...?

Jan 10 '10 at 03:23 AM Ashkan_gc

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++.

Jan 12 '10 at 12:15 PM jonas echterhoff ♦♦

can you tell me how unity uses physx or openAL? is there any general approach for creating wrappers?

Jan 16 '10 at 06:21 PM Ashkan_gc

The use of PhysX or OpenAL (now FMOD) 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.

Jan 17 '10 at 03:07 PM jonas echterhoff ♦♦

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 :)

Jan 18 '10 at 02:17 PM Ashkan_gc
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5070
x231
x7
x6

asked: Jan 09 '10 at 05:08 PM

Seen: 4867 times

Last Updated: Dec 25 '11 at 01:45 AM