.Net in Unity != .Net in MS

Mati-asยท2023๋…„ 3์›” 16์ผ
0

If you have experience using C# or Unity, you may be familiar with the .Net Framework. Unity uses C# to create game content, which means that C# runs on the .Net architecture. If you are familiar with middleware concepts, you may know that .NET in Unity is different from MS .NET. This is why programmers need to be well-versed in this, especially engine developers, as it is crucial for memory management. In this post, we will take a look at the structure of Unity .NET and how it differs from MS .NET.

๐Ÿ“Œ .NET used by Unity (scripts, backends)
There are two types of .NET used by Unity: MONO and ILCPP. To summarize the differences, MONO only supports 32-bit and its performance is lower than IL2CPP, so it is not commonly used these days. However, ILCPP also has its drawbacks. It uses AOT (Ahead-Of-Time) compilation, which increases the size of the final build of the app. Therefore, it has become a trend to solve such memory issues with high-performance computers that can handle both time and space resources.

.NET used by Unity: MONO, ILCPP
Mono:
โ€ข Cross-platform support like Java
โ€ข Uses JIT (Just-In-Time) compilation
โ€ข Currently managed by MS
โ€ข Currently Xamarin (which has also been acquired by MS)

IL2CPP:
โ€ข Developed to support 64-bit because MONO only supports 32-bit
โ€ข Supports AOT compilation

๐Ÿ“Œ Build process using IL2CPP
The process can be expressed as:
C# by Roslyn --> IL, IL Stripping --> C++, C++ in a target platform--> exe or dll file

Here are the details:

โ€ข 1. The Roslyn compiler compiles the C# code.
(Note that Roslyn is a platform used to compile C#, and is generally used instead of other platforms.)
โ€ข 2. IL2CPP performs IL Stripping (Intermediate Language Stripping) to reduce the size of the application.
โ€ข 3. IL2CPP converts IL code to standard C++ code.
โ€ข 4. The C++ compiler compiles the converted C++ code for the target platform.
โ€ข 5. Unity creates an executable file or DLL depending on the target platform.

Note that code stripping is the process of deleting unused code. However, you need to be careful as code stripping may unintentionally cause bugs. For example, if you use dynamic member variables to reference a class, unused members or classes may be recognized, causing them to be stripped unintentionally.

๐Ÿ“Œ Library
Although Unity uses C#, it does not support the latest .NET libraries, nor does it support all libraries. The available versions are .NET Standard 2.0 and .NET 4.X. However, Unity recommends using .NET Standard 2.0. This is because it reduces the size of the library, makes cross-platform support smoother, and supports .NET runtime. It also catches errors during compile-time. However, this may vary depending on the platform and third-party library.

Note: A third-party library is a reusable software component developed and maintained by an entity other than the original vendor of the development platform.

๐Ÿ“Œ Memory management hierarchy of Unity .NET
This is the most important part of today's post, as it relates to the memory management aspect, which is one of the core competencies of programmers. There are three memory management hierarchies in Unity's .NET: Native Memory, Managed Memory, and Unmanaged Memory.

Native Memory: Refers to the memory management system that the Unity engine uses internally. Most users cannot access it directly. Although users cannot access or manipulate it directly, it is possible to indirectly modify it using Unity's C# API if fine-tuned memory adjustments are desired. The native memory stores scenes, assets, graphics APIs, graphics drivers, subsystems, and plugin buffers.

Unmanaged Memory: Refers to the layer that accesses the native memory to fine-tune memory allocation. This is used for the Job System and Burst compiler. Given that the Job System and Burst are complex topics, I will provide links for further explanation.

Managed Memory: Refers to the memory managed by the garbage collector. As previously mentioned, this prevents various mistakes that can occur during memory allocation and deallocation, but if the garbage collection is not properly understood, it can lead to performance degradation. Unity's garbage collection is different from MS's method. Unity uses the Boehm GC method, which is also non-generational and does not compress memory. This means that memory fragmentation frequently occurs in Unity, and the heap can easily expand, leading to optimization issues. Additionally, to prevent excessive consumption of performance resources during garbage collection, Unity also adopts an incremental approach that divides garbage collection into small steps.

profile
plonker yet

3๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2024๋…„ 10์›” 10์ผ

Your generosity in sharing such valuable information is a testament to your genuine desire to Slope help others succeed, and for that, I am forever grateful.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ
comment-user-thumbnail
2024๋…„ 11์›” 29์ผ

Number of the Beast: This will be finished if solar smash you fail to kill 666 million people on Earth.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ
comment-user-thumbnail
2025๋…„ 1์›” 2์ผ

nice

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ