Objective
The first thing to know is that Unity3d's C # Base script module is implemented through mono.
What is mono?
Refer to Baidu Encyclopedia: Mono is a project hosted by Novell (launched by Xamarin) and led by Miguel de Lcaza, a dedicated pioneer. NET open source project used on Linux. It contains a C # language compiler, a CLR runtime, and a set of class libraries, and implements both ADO Net and ASP NET.
It provides another implementation of the Microsoft. Net Framework, based on the CLI (Common language architecture) and the ECMA standard for C #.
Mono is mainly composed of the following parts:
C # compiler--mcs. (The latest mono version can already support c#5.0, but Unity3d's mono version still stays in the 2.0+ phase, so some of the new features in C # are not available in unity)
Runtime: Instant compiler JIT (described later). As well as the GC, Class library loader, and so on.
Base Class Library (BCL).
Mono Class Library. Provided beyond Microsoft. NET offers a number of additional features, primarily for building applications on other operating systems.
Below to get to the chase:
Mono and Unity
The Unity engine itself is written by C + +, except that mono is embedded in unity, providing unity with a complete virtual machine runtime environment. This allows mono's embedded interface to expose the mono runtime to the C + + code at the bottom of unity. Through these interfaces, the developer can control the Mono runtime, as well as the managed code that relies on the mono runtime (i.e., the C#,js script, etc.).
Why can unity cross the platform?
In a nutshell, the main reason is that unity uses a stack-based code instruction set called CIL (Common intermediate Language, also known as MSIL) generic intermediate language, which is a subset of the CLR through mono.
During the development of Unity3d, code compilation is divided into two main processes:
1) First compile the corresponding script code into CIL (then CIL will be compiled into a bit code to generate a CLI collection).
2) Then mono compiles the bit code in the CLI collection at runtime to the native instruction that is running locally. (This will use the JIT, AOT-compilation mode above)
Note: This means that the CIL is actually running in mono, and the native commands that are generated after the compilation are run locally.
Here's a little bit more about what is JIT and AOT
JIT: Instant compilation, or dynamic compilation , which compiles code when the program executes, translates an intermediate managed statement (CLI) into a machine statement, and executes the statement. But it also caches the compiled code instead of compiling it every time. Therefore, it is a combination of static compilation and interpreter.
AOT: Static compilation , the same JIT is used to compile, but it is compiled before the program runs. However, some of the code will be compiled immediately.
The process of the AOT is brief:
1) Collect the code that needs to be compiled.
2) Use JIT to compile the code.
3) emit compiled code and some meta-data
4) generates an executable file after calling the local assembler or connector processing.
Note: The iOS platform disables the use of the JIT compiler, so Mono raises a full AOT mode, which is not mentioned here.
Summarize
Unity's cross-platform is to compile C # script code into the CLIvia Mono , and then the mono Runtime leverages the JIT or AOT The CLI is compiled into the native code of the target platform .
Resources
1. Baidu Encyclopedia
Https://baike.baidu.com/item/CIL/3583850?fr=aladdin
https://baike.baidu.com/item/JIT%E7%BC%96%E8%AF%91%E5%99%A8/3793585
2. Electronic industry publishing House: "Unity3d script Programming" Chen Jiadong
Unity3d Learning (a): The principle of unity cross-platform based on simple combing