1

I'm pondering an idea of accessing Unity3d's native API and possibly creating a wrapper library for easy human usage. To do that I'm currently trying to figure out what exactly happens when Unity project is converted to XCode project during build with IL2CPP flag on.

I'd like to know what is the exact process that goes on when IL2CPP translates IL to c++, why does the project still has DLL's included and is there anything special about the final compilation process e.g. where does the magic happen?

Any insights and opinions regarding the feasibility of described goal (Unity3d c++ API) are welcome.

PS: I understand that such an API would be hackish to say the least and yes, I do understand, that there are other technologies apart from Unity3d, that allow c++ usage as first-class citizen, namely Unreal Engine. This question is first and foremost theoretical in nature. That, and I also like c++ :)

user229044
  • 232,980
  • 40
  • 330
  • 338
pdeschain
  • 1,411
  • 12
  • 21

1 Answers1

2

No, nothing about the IL2CPP scripting backend exposes a new native API from Unity.

There really isn't too much magic going on here, actually. The toolchain is something like this.

  1. Use the Mono compiler to generate IL assemblies from the C# code in the user's Unity project.
  2. Strip unused IL from the assemblies to convert to C++ (this help us decrease the fine code and binary size).
  3. Use IL2CPP to generate C++ code from the IL assemblies.
  4. Compile the C++ code with a C++ Compiler for the native platform (Xcode on iOS, Emscripten for WebGL)

The only difference between using the Mono scripting backend and the IL2CPP scripting backend ouptut to Xcode for iOS is which files are included in the Xcode project. For IL2CPP, all of the generated C++ files are included.

No matter which scripting backend you use those, the API for Unity is unchanged.

Josh Peterson
  • 2,299
  • 19
  • 21
  • So, it basically converts entire unity engine AND user scripts to c++? I thought that unity was c++-based engine internally and c# was just a scripting language. – pdeschain Mar 27 '15 at 13:43
  • Is it possible to utilise generated types in some custom way? In example - instantiating an instance of converted class of SpriteRenderer or other unity class? – pdeschain Mar 27 '15 at 13:45
  • You are correct, much of the Unity engine is C++. IL2CPP does not have any impact on that code. It will convert any IL code (usually from C#) to C++. It is more correct to say that IL2CPP converts only the user scripts to C++. It is not easy to use the types in the generated C++ code, as the generated code is not meant to be extensible. A change to the script code may change a type name in the generated code in an unexpected way, for example. – Josh Peterson Mar 27 '15 at 15:24
  • After some further thinking - how do converted user scripts interface with underlying unity engine? If we can get to the interfaces of unity types that are used by generated user scripts, then there it is - a c++ api that can theoretically be used. – pdeschain May 21 '15 at 09:18
  • The managed code in the Unity engine is converted by IL2CPP along with the user scripts. So there is not a stable API to the Unity engine. I guess it is possible to use the generated C++ code from the Unity engine managed assemblies, but this is not something Unity will support. A slight change to the code generation done via il2cpp.exe (which happens every release), could easily break the Unity engine API you would be using. – Josh Peterson May 21 '15 at 11:43