Using IL2CPP to generate code

There are two main ways to do this:
So, why does any of this matter?

1. Create an empty Unity project, set the scripting backend to IL2CPP, add one or more desired source files (or MonoBehaviours), and build the project.
2. Skip Unity and use il2cpp.exe directly from the command-line.

  Using Unity to generate code with IL2CPP

The first option is more straightforward but slower, and will always include many unneeded Unity assemblies, but has the advantage that you don’t need to worry about configuring all the dependencies for your environment and target architecture. When you build a Unity project with IL2CPP, the location of the generated C++ can vary (depending on which version of Unity you use) but is usually in a folder called il2cppOutput located somewhere in the project’s build output.

To do this:

1. Create a new project in Unity Hub

alt

2. In File -> Build Settings, select either PC, Mac & Linux Standalone, Universal Windows Platform, iOS or Android.
3. Click Player Settings… and in Player -> Other Settings -> Configuration, change the Scripting Backend to IL2CPP. Ensure that the C++ Compiler Configuration is set to Release.

alt

4. Drag the source files you want to use into the Assets folder under the Project tab of the Unity main editor window.
5. In File -> Build Settings, click Build and choose a target folder for the build. This will take a while.

Notable output files:
The Mono build (regular .NET assembly): Test_BackUpThisFolder_ButDontShipItWithYourGame\Managed\Assembly-CSharp.dll.
The C++ source code generated by IL2CPP: Test_BackUpThisFolder_ButDontShipItWithYourGame\il2cppOutput.
The native binary compiled from C++: GameAssembly.dll
The metadata generated by IL2CPP (we’ll return to this later): Test_Data\il2cpp_data\Metadata\global-metadata.dat.

(this is correct for Unity 2019.3.1f1 – file locations may vary with different versions of Unity)