Search code examples
unity-game-enginemonophysx

How is Unity3D Able to Compile PhysX?


Unity3D uses Mono to compile code to pure intermediate language for cross-compatibility (please correct me if I'm wrong). PhysX contains native code, some of which can't be represented in pure managed code (/clr:pure).

I am aware that you can't link ijw/native modules with pure modules; you can't link native object files in pure mode. However, Unity3D seemingly gets around this with the inclusion of PhysX. How?

I could not find any resource that could explained this other than presumably special builds through Unity's and PhysX partnership: How does Unity3D use PhysX without requiring the PhysX runtime?


Solution

  • Mono and .NET allow the user to call native C/C++ code by calling to external libraries.

    Unity has a system called IL2CPP which takes the intermediate mono language and converts it to C++ (or asm.js for webgl builds via emscripten). This is how Unity compiles to iOS. All the C#/Unityscript/Boo code is converted to C/C++/Obj-C which is then included into an XCode project to let you build and debug iOS apps.

    Unity pro allows for the inclusion of native libraries into your games. This allows you to run raw C/C++ code by including a static or shared library (depending on the platform) and calling the library from C#. Not only is there probably a special build of PhysX but it's probably accessed via Mono's native interface.