简体   繁体   English

我可以将 IL2CPP(C++ 中间语言)用于非 Unity 应用程序吗?

[英]Can I use IL2CPP (Intermediate Language To C++) for non-Unity applications?

IL2CPP.exe is a Unity utility for converting C# IL code to C++. IL2CPP.exe是一个 Unity 实用程序,用于将 C# IL 代码转换为 C++。 My question is: can this executable be used outside of the Unity game-development environment as a general-purpose tool for converting any .NET application (not just games) to a high-performance native executable?我的问题是:这个可执行文件可以在 Unity 游戏开发环境之外用作通用工具,用于将任何.NET 应用程序(不仅仅是游戏)转换为高性能本机可执行文件吗?

Although I do know some C++, It would certainly be nice to be able write all kinds of programs in a language I am comfortable and fluent with (C#)......whether they be audio/video/music-production DAWs or OS-level security/forensics tools or machine-learning platforms or anything else that's resource-intensive.......and know that they will run as efficiently as an app written in straight C++.虽然我确实知道一些C++,但能够用我熟悉和流利的语言(C#)编写各种程序肯定会很好......无论它们是音频/视频/音乐制作 DAW 还是操作系统级安全/取证工具或机器学习平台或任何其他资源密集型......并且知道它们将像直接用 C++ 编写的应用程序一样高效地运行。

Short answer: NO简短的回答:否

IL2CPP is tightly connected to the Unity environment and it's not possible to use it outside of Unity. IL2CPP 与 Unity 环境紧密相连,无法在 Unity 之外使用它。 You would need to write your own converter(?) to do such a thing.您需要编写自己的转换器(?)来做这样的事情。

Longer answer更长的答案

IL2CPP doesn't do any magic in terms of performance improvement. IL2CPP 在性能改进方面没有任何魔力。 Comparing C++ with C# with IL2CPP code should give (almost - more below) no performance benefit.将 C++ 与 C# 与 IL2CPP 代码进行比较应该没有性能优势(几乎 - 更多如下)。

IL2CPP is performant compared to C# code written for Unity specifically for few reasons that have nothing to do with C++.与为 Unity 编写的 C# 代码相比,IL2CPP 的性能更高,特别是出于与 C++ 无关的几个原因。

Why Unity is unique and needs IL2CPP:为什么 Unity 是独一无二的并且需要 IL2CPP:

  • Unity API is very heavily reliant on main thread performance, as the whole Unity API was written almost 10 years ago, where 2 Core CPUs were top-notch and everyone believed that we will have 20-50GHz single-core CPUs by now. Unity API 非常依赖主线程性能,因为整个 Unity API 几乎是 10 年前编写的,其中 2 核 CPU 是一流的,每个人都认为我们现在将拥有 20-50GHz 的单核 CPU。
  • Unity makes a lot of assumptions that you will use their API for everything, begging from IO to Threading and GPU access, which is heavily bound to C++ core. Unity makes a lot of assumptions that you will use their API for everything, begging from IO to Threading and GPU access, which is heavily bound to C++ core.
  • Unity needs to be wrapped with Unity objects (MonoBehaviours and GameObjects) to be used for almost anything, you cannot write your own native anything. Unity 需要用 Unity 对象(MonoBehaviours 和 GameObjects)包装以用于几乎任何东西,你不能编写自己的原生任何东西。 (This is a simplification) (这是一个简化)
  • Unity is written in C++, so it needs to do something very similar to Marshalling, and it's not very efficient. Unity是用C++写的,所以需要做一些和Marshalling很相似的事情,效率不是很高。

So why IL2CPP?那么为什么是 IL2CPP 呢?

  • Unity cannot convert its already very legacy backend (Mono) and its legacy API to be multithreaded since Mono have a lot of assumptions about your code that are not easily convertible to "simple" unity API. Unity 无法将其已经非常遗留的后端(Mono)及其遗留 API 转换为多线程,因为 Mono 对您的代码有很多假设,这些假设不容易转换为“简单”统一 ZDB974238714CA8DE634A7CE1D0
  • Unity core is written in C++, so they are eliminating any form of Marshalling all together by skipping Mono "translator". Unity 内核是用 C++ 编写的,因此它们通过跳过 Mono“翻译器”来消除任何形式的编组。
  • IL2CPP converts highly inefficient C#, single-threaded code to multithreaded C++, where possible, and it does this by analyzing IL code. IL2CPP 尽可能将效率极低的 C# 单线程代码转换为多线程 C++,它通过分析 IL 代码来实现这一点。

Is it worth converting other C# to C++?是否值得将其他 C# 转换为 C++?

No!不! Compare any arbitrary, optimized C# code that was precompiled by AOT to (modern) C++.将 AOT 预编译的任意优化 C# 代码与(现代)C++ 进行比较。 You should get the same performance.您应该获得相同的性能。 Identical I would say.我会说是一样的。

C# is compiled to IL (Intermediate Language) which as the name suggests is Intermediate. C# 编译为 IL(中间语言),顾名思义就是中间语言。 It's converted in runtime to Native Binary code (only when needed), that is what C++ is compiled into.它在运行时转换为本机二进制代码(仅在需要时),这就是 C++ 编译成的。 You can force this conversion by skipping IL generation by running Ahead of Time compilation (AOT).您可以通过运行 Ahead of Time 编译 (AOT) 跳过 IL 生成来强制进行此转换。

The ONLY thing that your C# code will be less performant is when you are abusing GC's ability to clean up after you.您的 C# 代码的唯一性能会降低是当您滥用 GC 的清理能力时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM