The underlying principle of c# in unity

There are two mechanisms for running C# at the bottom of unity: Mono and IL2CPP

1.Mono

A high-level language such as C# or VB that follows the CLI specification will first be compiled into an intermediate language: IL (CIL) by their respective compilers. When it needs to be actually executed, these IL will be loaded into the runtime library (CLR), that is, In the VM, the VM is dynamically compiled into assembly code (JIT) and then executed.

When using Mono, the compilation and operation of the script is shown in the following figure:

The three major scripts are compiled into IL. When the game is running, IL and other third-party compatible DLLs in the project are put into the Mono VM virtual machine, which is parsed into machine code by the virtual machine and executed.

2.IL2CPP

IL2CPP is mainly composed of two parts:

1. AOT static compilation compiler (il2cpp.exe), converts IL into C++ source code, and then submits it to the C++ compiler of each platform for compilation to achieve platform compatibility

2. The runtime library (libil2cpp), the runtime library will provide services and abstractions such as garbage collection, thread/file acquisition, and internal calls that directly modify the native generation of managed data structures

When using IL2CPP, the compilation and operation of the script is shown in the figure below:

When Unity is compiled and packaged, the IL2CPP mode also first compiles the files in the project into IL, but IL2CPP will not directly run these ILs in the virtual machine, but will use the project IL together with other ILs to convert these ILs back to CPP ( C Plus Plus or C++) code, and then put the C++ code into the local compiler (the compiler of the specific running machine platform) to compile it into executable native assembly code, and then use the self-developed IL2CPP VM to run.

3. Comparison of features

advantage

shortcoming

Mono

1. Building apps is very fast

2. Due to Mono's JIT (Just In Time compilation) mechanism, it supports more managed libraries

3. Support runtime code execution, dll dynamic loading assembly hot update on Android

4. The code must be published as a managed assembly (.dll file, generated by mono or .net)

1. Difficult maintenance, copyright restrictions, and the powerful features of C# cannot be used in lower versions of mono;
2. Mono needs to run in a virtual machine, which is very inefficient compared to compiling native CPP code

IL2CPP

1. Compared with Mono, code generation has been greatly improved

2. Can debug generated C++ code

3. The running efficiency of the program is higher than that of Mono, and the running speed is fast

4. Engine code stripping can be enabled to reduce code size

5. Multi-platform porting is very convenient

1. Only AOT (Ahead of Time) compilation is supported

2. It is slower to build applications than Mono

IL2CPP is more suitable for developing and publishing projects, but in order to improve the speed of version iteration, you can switch to Mono mode during development (faster to build applications).

Guess you like

Origin blog.csdn.net/m0_57771536/article/details/127827491