il2cpp vs Mono Scripting backend in Unity3d - What you need to know!

If you are familiar with scripting backends in unity like IL2CPP and Mono, then you have definitely thought about which scripting backend to use. But if you are not familiar with them, this can be a problem as scripting backend is the DNA of your Unity project. What are these scripting backend? What is IL2CPP? And the other one, Mono? Let do a comparison in this comprehensive blog.

Scripting backend Selection in Unity


What is the Unity scripting backend?

Mono and IL2CPP are Unity’s scripting backends. But what is a scripting backend, actually?

A scripting backend is the runtime technology that compiles and executes your C# code. As a Unity developer, you should understand this because it affects your entire project lifecycle. It impacts iteration speed, performance, memory usage, platform support, and debugging experience. So yes, it is an important thing to know.

Most of the time, it works without any extra settings, so we hardly notice its importance. But once you understand it, you get a better idea of what is happening under the hood. This helps you make better decisions and find ways to improve performance and overall execution.

What is unity scripting backend and why it is important
Before going further, there is one important thing you should be aware of. On certain platforms, you do not actually have a choice when picking a scripting backend. This is why the scripting backend option is disabled in some projects. Many developers ask why this option is disabled.

One possible reason is simple. You may not have that backend installed, so you need to install it first. But in many cases, the reason is platform limitation.

On iOS, WebGL, and consoles, there is only one scripting backend available, which is IL2CPP. These platforms do not allow JIT compilation, and Mono depends on JIT. Because of this restriction, Mono is not an option on these platforms. IL2CPP works differently. It converts managed C# code into C++, then compiles it ahead of time, which is allowed on these platforms.

On most other platforms, like desktop and some mobile targets, you still have the option to use Mono. Unity provides both scripting backends, and each comes with its own pros and cons. There is no clear winner here. It depends on the platform and what you are trying to achieve.

In general, IL2CPP is more secure, but it takes more time to build and is the only option on consoles, iOS, and WebGL. Mono, on the other hand, has much faster build times and is easier to work with during development.

Before we talk about Mono or IL2CPP, we need to understand JIT and AOT. Without this, everything else sounds confusing for no reason.

Your computer does not understand C#, Java, or any high-level language. It only understands machine code, which is basically zeros and ones. So your C# code must be converted into machine code before it can run. This conversion does not happen magically. There are different ways to do it, and the two most common ones are JIT and AOT.

JIT (Just-In-Time)

In JIT, your code is not fully converted into machine code in advance. Instead, the compilation happens while the application is running. When a piece of code is needed, it is compiled at that moment and then executed.

This approach has some advantages. Build times are faster because the compiler does not do all the work upfront. It also allows the runtime to optimize code based on how the application is actually running. But there is a downside. Because compilation happens at runtime, it is not allowed on platforms that restrict dynamic code execution.

AOT (Ahead-Of-Time)
In AOT, the compilation happens before the application runs. Your C# code is converted into machine code ahead of time, during the build process. When the application starts, it already has a machine-ready version of the code.

This makes AOT suitable for platforms with strict security rules, where runtime code generation is not allowed. The downside is that build times are longer, and iteration during development is slower because everything must be compiled again.

In simple terms, JIT compiles code while the app is running, and AOT compiles code before the app runs. Both exist for a reason, and which one you use depends more on platform limitations than personal preference.

Now that you have a basic idea of JIT and AOT, we can look at Mono and IL2CPP.

Mono

Mono is a traditional .NET runtime that uses just-in-time (JIT) compilation. It compiles your C# code into native code while the application is running. This is why build times are faster. Mono is Unity’s default scripting backend on platforms that allow JIT compilation.
Mono Scripting backend execution


IL2CPP

IL2CPP is Unity’s own scripting backend. It takes your C# code, converts it into .NET assemblies, then into C++, and finally compiles it into native machine code ahead of time (AOT). The result is a native binary tailored for the target platform.


IL2CPP execution process.

IL2CPP vs Mono Comparison

You can find a very detailed comparison of Mono and IL2CPP in the official Unity documentation. Here, I want to focus on five important differences that actually matter in practice.

Two of them we already discussed. First, Mono uses the JIT execution model, while IL2CPP uses AOT. Second is platform support. IL2CPP is the only available option on iOS, WebGL, and consoles.

Now, the remaining three are also very important.

Build Time
Mono is faster. You might have heard people complain that Unity WebGL builds are very slow. Yes, the main reason behind this is IL2CPP. Compared to Mono, IL2CPP takes more time because your C# code is first converted into intermediate language, then into C++, and finally compiled. This code generation and compilation process takes time. Mono builds are much faster.

Startup Time
If you understand the difference between JIT and AOT, you can probably guess this. IL2CPP usually has faster startup time because the code is already compiled and ready to run. Mono may spend some time doing JIT compilation when the application starts.

Reflection / Dynamic Code Support
IL2CPP has limited support for dynamic code generation. Things like Reflection.Emit do not work with IL2CPP. If your project heavily depends on runtime code generation or emit-based reflection, then IL2CPP is not an option. Mono handles these cases much better.

il2cpp vs mono - unity scripting backend comparsion.

Practical tips

During development and testing, using Mono makes sense because it saves a lot of build time. Faster iteration means faster debugging and development. You can reduce IL2CPP build pain with cloud builds or proper build pipelines, but Mono is still quicker for day-to-day work.

If you need more security, IL2CPP is the better choice. Due to security reasons, different platforms wont allow you to deploy a mono build. 

il2cpp or Mono what to choose for your unity project?


And remember no matter if it Mono or IL2CPP, you can get peak performance with burst compiler via converting C# code to highly efficent machine code.



Post a Comment

0 Comments