Unity 使用 IL2CPP 发布项目

2024-07-19 1583阅读

一、为什么用 IL2CPP

Unity的IL2CPP(Intermediate Language to C++)是一个编译技术,它将C#代码转换为C++代码,然后再编译成平台相关的二进制代码。IL2CPP提供了几个优点,特别是在性能和跨平台部署方面。以下是IL2CPP的一些主要优点:

1.性能提升:IL2CPP通常能提供比传统的Mono运行时更好的性能,因为它可以利用本地编译器的优化。

2.跨平台支持:由于IL2CPP生成的是C++代码,这使得它更容易被跨平台的编译器支持,因此你可以将游戏编译到多个平台,包括iOS、Android、Windows、Mac、Linux和游戏主机。

3.安全性增强:IL2CPP提供了比Mono更高级别的安全性,因为它不需要运行一个易于逆向工程的中间语言虚拟机。C++代码更难被反编译,这使得逆向工程变得更加困难。

4.完全AOT编译(Ahead-of-Time):IL2CPP完全支持AOT编译,这意味着所有的代码都在部署之前编译好了,有利于实现更快的启动时间和更稳定的运行时性能,这在iOS等不允许JIT编译的平台上尤其重要。

5.内存管理改进:IL2CPP使用了更现代的垃圾回收技术,可以更高效地管理内存,减少内存泄露和碎片化问题。

6.64位支持:IL2CPP支持64位计算,这是许多现代设备和操作系统的要求。

7.与现代C++编译器的兼容性:由于生成的是C++代码,IL2CPP可以利用现代C++编译器(如Clang和GCC)进行进一步的优化。

8.可预测的性能:由于所有的代码都在编译时就已经确定了,运行时的性能比JIT(Just-In-Time)编译更加可预测,因为它不会在用户使用应用程序时进行编译。

9.通用基础设施:IL2CPP为Unity带来了一个通用的代码基础设施,用于支持不断增长的平台数量,简化了Unity的维护和新平台支持的工作。

10.现代语言特性支持:IL2CPP更好地支持C#的新特性,因为它能够使用新的C#编译器,而旧的Mono运行时可能不支持这些新特性。

尽管IL2CPP提供了许多优点,但它也有其限制,比如编译时间往往比Mono更长,且生成的二进制文件大小可能会更大。然而,对于寻求性能和兼容性的开发者来说,IL2CPP提供了一个强有力的解决方案。

二、环境搭建

在安装 Unity 的时候,记得将 Windows Build Support(IL2CPP) 勾上,如果是发布 Linux 系统,则用 Linux Build Support(IL2CPP)

Unity 使用 IL2CPP 发布项目

如果没有安装 IL2CPP,在发布界面这里会提示没有安装 IL2CPP,如下:

Currently selected scripting backend (IL2CPP) is not installed

另外还需要安装 C++ 开发环境,否则不能发布项目,错误如下:

Unity 使用 IL2CPP 发布项目

详细错误:

Internal build system error. BuildProgram exited with code 1.

error: Could not set up a toolchain for Architecture x64. Make sure you have the right build tools installed for il2cpp builds. Details:

IL2CPP C++ code builder is unable to build C++ code. In order to build C++ code for Windows Desktop, you must have one of these installed:

 * Visual Studio 2022 or newer with C++ compilers and Windows 10 (or newer) SDK (recommended)

 * Visual Studio 2019 with C++ compilers and Windows 10 (or newer) SDK

 * Visual Studio 2017 with C++ compilers and Windows 10 (or newer) SDK

 * Visual Studio 2015 with C++ compilers and Windows 10 (or newer) SDK

Visual Studio 2017 (or newer) is detected using `vswhere.exe` as well as VSCOMNTOOLS environment variables.

Visual Studio 2015 is detected by looking at "SOFTWARE\Microsoft\VisualStudio\14.0_Config\InstallDir" in the registry as well as VSCOMNTOOLS environment variables.

Windows 10 (or newer) SDK is detected by looking at "SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0\InstallationFolder" in the registry.

Unable to detect any compatible Visual Studio installation!

 * Found Visual Studio 2022 installation without C++ tool components

Unity.IL2CPP.Bee.BuildLogic.ToolchainNotFoundException: IL2CPP C++ code builder is unable to build C++ code. In order to build C++ code for Windows Desktop, you must have one of these installed:

 * Visual Studio 2022 or newer with C++ compilers and Windows 10 (or newer) SDK (recommended)

 * Visual Studio 2019 with C++ compilers and Windows 10 (or newer) SDK

 * Visual Studio 2017 with C++ compilers and Windows 10 (or newer) SDK

 * Visual Studio 2015 with C++ compilers and Windows 10 (or newer) SDK

Visual Studio 2017 (or newer) is detected using `vswhere.exe` as well as VSCOMNTOOLS environment variables.

Visual Studio 2015 is detected by looking at "SOFTWARE\Microsoft\VisualStudio\14.0_Config\InstallDir" in the registry as well as VSCOMNTOOLS environment variables.

Windows 10 (or newer) SDK is detected by looking at "SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0\InstallationFolder" in the registry.

Unable to detect any compatible Visual Studio installation!

 * Found Visual Studio 2022 installation without C++ tool components

   at Unity.IL2CPP.Bee.BuildLogic.WindowsDesktop.WindowsDesktopBuildLogic.UserAvailableToolchainFor(Architecture architecture, NPath toolChainPath, NPath sysRootPath, Boolean targetIsSimulator)

   at PlayerBuildProgramLibrary.PlayerBuildProgramBase.GetIl2CppToolChain(PlatformBuildLogic platform, Architecture architecture, NPath toolChainPath, NPath sysrootPath)

   at PlayerBuildProgramLibrary.PlayerBuildProgramBase.SetupIl2CppBuild()

   at PlayerBuildProgramLibrary.PlayerBuildProgramBase.b__94_0()

   at Bee.Core.TinyProfiler2Base.Section[T](String label, Func`1 func, Dictionary`2 metadata)

   at PlayerBuildProgramLibrary.PlayerBuildProgramBase.SetupPlayerBuild()

   at WinPlayerBuildProgram.WinPlayerBuildProgram.SetupPlayerBuild()

   at PlayerBuildProgramLibrary.PlayerBuildProgramBase.RunBuildProgram()

   at PlayerBuildProgramTypeWrapper.Run(String[] args)

   at Program.Main(String[] args)

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

 

解决方法:

在 Visual Studio 2022 中,添加 C++ 环境,并勾选 Windows 10 SDK 安装

Unity 使用 IL2CPP 发布项目

上面两个条件安装完成后,还需要在 Unity --> Player Settings 中进行设置

在 Configuration --> Scripting Backend 这里选择 IL2CPP 

Unity 使用 IL2CPP 发布项目

接下来就可以直接发布 PC 版的项目了,发布成功后,生成的文件和 Mono 不太一样

Unity 使用 IL2CPP 发布项目

发布 Android 也不需要改多少配置,这里我只是将 ARMv7 换成了 ARM64,其他的设置就是默认的。

Unity 使用 IL2CPP 发布项目

Android 打包是真的慢,一个空包差不多用了 20 分钟,不过包小了很多,只有 10 M。

Unity 使用 IL2CPP 发布项目

end

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]