Xiao Yu
Links: https://zhuanlan.zhihu.com/p/19972689
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Unity3d Presumably everyone is not unfamiliar, indie game maker many people are using it, even some large companies are also used in very commercial game production. One of the biggest features of Unity3d is the one-time production, multi-platform deployment, and this core functionality is achieved by mono. It can be said that mono is the core of Unity3d core, is the root of Unity3d cross-platform. But in the middle of 2014, the Unity3d official blog published an article "The future of Scripting in Unity", which led to the concept of il2cpp, and felt the potential to replace Mono. What is il2cpp, and what benefits and changes can it bring to Unity3d and the use of Unity3d? This is what this article is trying to illustrate.
c#,.net Framework
Let's first talk about il2cpp trying to replace Mono. Before you say mono, you have to mention the C # language and the. Net Framework behind it. C # is a Microsoft launch based on the. NET Framework, an object-oriented, high-level programming language. C # is pronounced "see sharp", mimicking the musical name "C?" (c-liter), is the C language of the upgrade meaning. The correct wording should be the same as the name "C?". C # is derived from C and C + + and inherits its powerful performance, while at the same time. NET Framework class library as a base, with rapid development capabilities like Visual Basic. C # was developed by Anders Heilsberg and Microsoft released the language in 2000. Speaking of Anders Heilsberg here to say more, the same year and VC (note, is the VC, that time there is no visual Studio) and another company's IDE is also very popular, that is Borland Company's Delphi, is also led by Anders Heilsberg development.
He was later dug up by Microsoft, creating J + +, a Java-like language (well, in my superficial knowledge, that was basically done in Java). Later, due to licensing with sun, Microsoft discontinued the development of J + + in 2001 and launched C # 1.0. Thanks to Sun's lawsuit, or Microsoft will not have c#,j++ also may have been following the footsteps of Java. On the contrary C # has evolved, from 1.0 to 4.0 and the newest 5.0,c# have been far away from Java several streets (or in my personal use of Java and C # sense, about the two languages comparison, whether it is efficiency, Kua platform, or language ease of use, community activity, Online debate is everywhere, everyone has their own views, this is not the focus of this article).
Mono,mono VMs
C # is good, but can only run on Windows, Microsoft did not open it at the time, so there will always be people say that can not cross the platform, light on this, C # and Java can not compare.
Microsoft has applied to the ECMA to apply C # as a standard. In December 2001, ECMA released the ECMA-334 C # language Specification. C # became an ISO standard in 2003 (ISO/IEC 23270). This means that as long as you comply with the CLI (Common Language Infrastructure), third parties can implement any language. NET platform. Mono was born in this environment.
Mono is a free open source project hosted by Xamarin Corporation (formerly Novell, First Ximian). The goal of the project is to create a series of. NET tools that conform to the ECMA standard (Ecma-334 and Ecma-335), including the C # compiler and the common language architecture. Unlike Microsoft's. NET Framework (Common language runtime), the mono project can run on Windows systems as well as Linux,freebsd,unix,os X and Solaris, and even some game platforms, For example: Playstation 3,wii or Xbox 360. Mono makes the language of C # a very good cross-platform capability. Compared to Microsoft's. Net Framework Runtime Library, Mono uses its own mono VM as the runtime library. Coupled with the fast and friendly development capabilities of C # itself, the unity team ultimately decided to use mono,c# as its core at the beginning of its creation. (Well, that's what I guess)
One might say that unity also supports JavaScript and Boo, not just a C # language. The first thing I want to correct is that JavaScript in unity is strictly not a JavaScript in the world of the world, and its right name is Unity Script, which is actually evolved from boo (so that everyone can understand why in 3 languages, Boo uses the least number of people, but that's why it's always there. I think unity started in order for more people to be able to get started quickly, especially considering that many scripting programmers are already familiar with JavaScript, in order to take care of this part of the people, invented the unity Script, its syntax and the language of the unified, the JavaScript almost consistent, So that you can directly use it to develop, reduce the threshold. But unity script is fundamentally different from JavaScript in its running. I'll describe this in more detail in the next section of IL. From the use of three languages in Unity: Boo is almost useless, and in Unity Script and C # both the demo code and the third-party code in the Unity Asset store, C # already has a ratio of 85% to 90% (personal rough estimate, No detailed statistics are done). C # can be seen in unity by me and other game code farmers love.
IL
I'm done. The C#,.net framework and mono lead us to a very important concept, "IL". Il is the full name of intermediate Language, and many times you will see CIL (Common Intermediate Language, especially in. The IL standard under the net platform). In the Unity blog and this article, IL and CIL represent the same thing: translation comes in the middle language. It is a common language architecture and. NET Framework's low-order (lowest-level) human-readable programming language. The goal is. The language of the NET Framework is compiled into CIL and then compiled into byte code. CIL resembles an object-oriented assembly language, and it is completely stack-based, and it runs on virtual machines (. Net Framework, Mono VM) languages.
The process is: C # or VB to follow the CLI specification of the high-level language, was first compiled by the respective compiler into the intermediate language: il (CIL), wait until the need for real execution, the IL will be loaded into the runtime library, that is, the VM is dynamically compiled into the assembly Code (JIT) and then executed.
It is the introduction of VMS that makes many dynamic code features possible. With VMs we can even generate new code and execute it at run time by code. This is what the static compiler language cannot do. Back to the last section I said Boo and Unity Script, with the concept of IL and VM, we can not find that there is no corresponding VM virtual machine, there is only one VM in unity: Mono VM, which means boo and unity Script is compiled by the respective compiler into an IL that follows the CLI specification and is then interpreted by the mono VM. This is also the fundamental difference between unity script and JavaScript. JavaScript is eventually run in the browser's JS parser (for example, the famous Google Chrome V8 engine), while Unity script runs in the Mono VM. In essence, to the IL level, it is by which high-level language is created and not so important, you can use c#,vb,boo,unity script or even C + +, as long as the corresponding compiler can compile it into IL all the line!
il2cpp, Il2cpp VM
The protagonist of this article finally came out: Il2cpp. With the above knowledge, it is easy to understand the meaning of it: the Il Intermediate language into the CPP file. If you understand the above dynamic language CLI, IL and VMS, and then see Il2cpp must be filled with doubts. Now the trend is to add language dynamic features, even if it is a static language such as C + +, there is a C + + compiler suitable for IL, why unity to the contrary, the IL back to the static CPP it? It's not enough to eat. According to the Unity official blog Given earlier in this article, there are several reasons for this:
1.Mono VMS are ported across platforms, maintenance is time-consuming and sometimes impossible to complete
Mono's cross-platform is implemented through mono VMS, with several platforms that implement several VMS, such as Unity's multi-platform-enabled engine, and Mono's official VMS are definitely not meeting the requirements. So for different new platforms, Unity's project team will have to transplant the VMS and solve the bugs found in the VMS. This is a very time consuming and laborious force. These portable platforms are good to say, and there are browser-based platforms like WebGL. It is almost impossible for WebGL to support Mono's VMS.
2.Mono Version License Limited
Does everyone realize that Mono's version has been updated to 3.X, but in unity, the runtime version of C # stays at 2.8, which is one of the most complained by unity community developers: Many of the new features in C # are not available. This is because the mono license is limited, so unity cannot upgrade mono. This problem is solved if the il2cpp,il2cpp VM is a completely self-developed component.
3. Improve operational efficiency
According to the official experimental data, after switching to Il2cpp, the program operates 1.5-twice times more efficiently.
When using mono, the script compiles as shown in the following:
In short, the 3 big scripts are compiled into IL, and in the game runtime, IL and other third-party compatible DLLs in the project are placed into the Mono VM virtual machine, which is parsed into machine code by the virtual machine and executed
The changes made by Il2cpp are marked by the red ones:
After getting the intermediate language IL, use il2cpp to change them back into C + + code, and then compile them directly from the C + + compiler of each platform to execute native assembly code.
Some notes:
1. To change IL back to CPP in addition to the fast execution efficiency of CPP, another important reason is that you can use the ready-made C + + compiler on each platform to perform compile-time optimizations on your code, which can further reduce the size of the final game and increase the speed of the game.
2. Due to the nature of dynamic languages, most of them do not need the programmer to care too much about memory management, and all memory allocations and recoveries are done by a component called GC (Garbage Collector). Although the code becomes static C + + after il2cpp, the memory management is still in the way of C #, which is why there is a final reason to have a il2cpp VM: it is responsible for providing service work such as GC management, thread creation. However, due to the removal of IL loading and dynamic parsing, the Il2cpp VMs can be made very small, and the game loading time is shortened.
3. Because C + + is a static language, it means that we cannot use the cool features of dynamic languages. It is impossible for the runtime to generate code and execute it. This is the so-called AOT (Ahead of Time) compilation, not JIT (Just in time), that is mentioned in unity. In fact, many platforms for security reasons are not allowed to JIT, we are most familiar with the iOS platform, in console consoles, whether it is Microsoft's Xbox360, XboxOne, or Sony PS3,PS4,PSV, no one is allowed to JIT. With Il2cpp, it is completely AOT, and if the original code using dynamic features will fail to compile. The code is inherently unsuccessful when compiling the iOS platform, so don't worry if you're developing game code for iOS. So in this regard, we almost don't feel any problem with development.
Finally, the performance test given on Unite 2014 (the smaller the number indicates the faster it runs):
With the Il2cpp, the program size can be relatively narrow, running speed can be improved! Did you see the excitement? In fact, Il2cpp technology has been introduced in the existing Unity release. In the next article, we will take a practical example to see what Il2cpp has done for us and what we need to pay attention to.
Unity in the future: what is Il2cpp?