Lesson 102 +10 XP

Scripting Backends and IL2CPP

Scripting Backends and IL2CPP

Unity compiles your C# code using one of two scripting backends. The backend determines how your code runs and what optimizations are available.

Mono Backend

Mono is the original cross-platform .NET runtime Unity uses.

  • C# compiles to Intermediate Language (IL), which is interpreted by the Mono runtime at runtime.
  • Faster iteration: Changes in code compile quickly during development.
  • Lower performance: The JIT (Just-In-Time) compiler is less aggressive than AOT.
  • Available on: All platforms as a development option. Not available on iOS App Store builds.

IL2CPP Backend

IL2CPP (Intermediate Language To C++) converts IL bytecode into C++ source code, which is then compiled to native machine code by the target platform's C++ compiler.

  • Higher performance: Fully native code with AOT (Ahead-Of-Time) optimizations.
  • Platform requirement: Required for iOS (Apple does not allow JIT), WebGL, and most console platforms.
  • Slower build times: Requires compiling C++ output.
  • Smaller binaries on some platforms.

Selecting the Backend

Edit > Project Settings > Player > Other Settings > Scripting Backend:

  • Choose Mono for fast development iteration.
  • Choose IL2CPP for release builds and required platforms.

Managed Code Stripping

IL2CPP enables Managed Code Stripping, which removes unused C# code from the build to reduce binary size:

Strip LevelDescription
DisabledNo stripping. Safest but largest binary.
LowRemoves unreachable types. Recommended default.
MediumRemoves more aggressively. May require link.xml.
HighStrips most unused code. Requires thorough testing.

Use a link.xml file to preserve types that stripping incorrectly removes:

<linker>
  <assembly fullname="MyAssembly">
    <type fullname="MyNamespace.ImportantClass" preserve="all"/>
  </assembly>
</linker>

Scripting Restrictions with IL2CPP

IL2CPP does not support:

  • Runtime code generation (e.g., System.Reflection.Emit).
  • dynamic keyword in C# (limited support).
  • Some advanced reflection patterns.

TL;DR

  • Mono uses JIT compilation. Fast iteration, lower performance. Good for development.
  • IL2CPP converts to C++ and compiles to native code. Required for iOS, WebGL, consoles.
  • IL2CPP builds are slower but produce faster, smaller runtime executables.
  • Use Managed Code Stripping with IL2CPP; protect needed types with link.xml.