当前位置:网站首页>How Unity handles C# under the hood
How Unity handles C# under the hood
2022-08-11 07:28:00 【Clank's Game Stack】
Foreword
In interviews, we are often asked how the bottom layer of Unity handles C#. This section will analyze this question in detail through the following three points:
History of C#
Before C# came out, at that time, Java relied on Java virtual machine + Java bytecode interpretation and execution, so that Java code can be ported and written to run across platforms.At the same time, Java has a garbage collection mechanism, etc., which greatly reduces the difficulty of development.In response to Java, Microsoft launched the .net platform.The .net platform includes several points:
a: Design and create some programming development languages, such as: C#, J#, etc.
b: .net development tool, which compiles .net programming languages into .net bytecode, which we call CLR or IL, so that the code written in C# can be called by J#, because the bottom layer is based on .net wordsSection code.
c: Develop a .net virtual machine that can interpret and execute CLR bytecode, and transplant the .net virtual machine to multiple platforms (windows, win mobile and other Windows operating systems).In this way, programs developed with C#/J# can cross-platform to the operating system supported by .net, and at the same time, C# and J# can call each other at the same bottom layer.
d: In order to allow .net to support more platforms, Microsoft has opened up the .net CLR standard.
e: An open-source project Mono was developed, which uses .net as the standard to develop a .net virtual machine that supports mainstream PC and mobile operating systems.This gives .net code the opportunity to run on non-win family operating systems.
Why Unity uses C#
After talking about several stages of .net's development history, let's take a look at why Unity uses C# as the development language.When Unity came out, it also needed to solve the game for mac, the game for linux, the game for windows, the game for xbox and other platforms. Later, when the mobile game developed, it also needed to solve the game for Android, the game for IOS and so on.Therefore, the Unity engine must be built on the basis that it can be distributed across platforms.So the early Unity chose Mono as a cross-platform basis, so Unity is a project developed based on mono.Mono is built on .net technology, so it is developed on mono and can support multiple development languages, such as c#, J#, etc. So early Unity can support programming languages such as C#, Boo, Js, etc., all based on mono .net technology.So Mono helps Unity solve the problem of cross-platform, development language and development tools.
Everything looks good, but the technical solution based on mono also has fatal problems. Later, Unity found more and more problems. The main problems are as follows:
a: Program execution efficiency, interpreting and executing CLR code on the mobile terminal is not as efficient as direct native code;
b: Mono virtual machine porting. When a new game development platform comes out, the mono virtual machine needs to be re-ported. Let's not talk about the bugs here, it is not certain whether it can be supported or not;
c: mono copyright issue, when Unity uses mono, mono authorization is required;
d: It is almost impossible to port the mono virtual machine to the web platform, and now the web platform is also very popular, such as WeChat Douyin mini-games, etc.;
e: IOS does not allow running .net virtual machines;
Unity il2cppSavior
In order to solve the problem of mono .net, the Unity development team launched a new technology, which is il2cpp, as the name implies, it is to convert CLR/IL code into c/c++ code through il2cpp, and then based on the platform's native developmentThe tool is then compiled, and finally the native binary code is compiled.
At the same time, high-level programming languages such as C# will have some advanced features, such as garbage collection, etc., which requires some support at the bottom, such as garbage collection based on C/C++, thread library of C#, etc.These functions are implemented on the il2cpp runtime library (il2cpp vm), which provides support for these basic services at runtime.
There is no problem in publishing IOS in this way, and the native code performance is also good. At the same time, if a new platform appears, just port C/C++ to the corresponding platform, which is very convenient when porting.
To summarize the basic principles and steps of Unity's construction and processing of C# based on il2cpp:
(1) The developer is still developing based on .net C#;
(2) Or use .net tools to compile C# code into CLR or IL
(3) Use il2cpp tool to convert CLR/IL bytecode into static C++ code;
(4) Use native development tools (xcode, Android NDK, etc.) to compile the C++ code into the target's native machine code;
In this way, the installation package developed by Unity to the target OS platform is packaged and released.Assuming that there is a new platform, it is enough to transplant the native code of the game engine + platform engineering tools, and obtain better cross-platform portability while obtaining better performance.Finally, the last picture shows the entire architecture, as follows:
This section is shared here, follow me to learn more about Unity development.
边栏推荐
- Redis源码-String:Redis String命令、Redis String存储原理、Redis字符串三种编码类型、Redis String SDS源码解析、Redis String应用场景
- Daily sql-seek the sum of successful investments in 2016
- 常见激活函数及其导数
- 1688 product interface
- Amazon Get AMAZON Product Details API Return Value Description
- 微信小程序功能上新(2022.06.01~2022.08.04)
- 【深度学习】什么是互信息最大化?
- LabelEncoder和LabelBinarizer的区别
- exness:黄金1800关口遇阻,静待美国CPI出炉
- 抖音获取douyin分享口令url API 返回值说明
猜你喜欢
Amazon API interface Daquan
Implement general-purpose, high-performance sorting and quicksort optimizations
Douyin API interface
Douyin API interface
使用Keras构建GAN,以Mnist为例
《Show and Tell: A Neural Image Caption Generator》论文解读
Taobao sku API interface (PHP example)
unable to extend table xxx by 1024 in tablespace xxxx
radix-4 FFT principle and C language code implementation
【@网络工程师:用好这6款工具,让你的工作效率大翻倍!】
随机推荐
图文带你理解什么是Few-shot Learning
radix-4 FFT 原理和C语言代码实现
Spatial Pyramid Pooling -Spatial Pyramid Pooling (including source code)
Do not add the is prefix to the variables of the boolean type in the POJO class of the Alibaba specification
Taobao sku API interface (PHP example)
daily sql - user retention rate for two days
每日sql-求2016年成功的投资总和
抖音API接口
Redis源码:Redis源码怎么查看、Redis源码查看顺序、Redis外部数据结构到Redis内部数据结构查看源码顺序
sql--Users who have purchased more than 3 times (inclusive) within 7 days (including the current day), and the purchase amount in the past 7 days exceeds 1,000
NFT 的价值从何而来
抖音获取douyin分享口令url API 返回值说明
京东商品详情API调用实例讲解
HCIA experiment
Open Set Domain Adaptation 开集领域适应
什么是Inductive learning和Transductive learning
Unity底层是如何处理C#的
淘宝API接口参考
一张图了解JVM八大原子操作
Get Pinduoduo product information operation details