Unity 使用 IL2CPP 发布项目
创始人
2025-01-07 12:08:13
0

一、为什么用 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)

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

Currently selected scripting backend (IL2CPP) is not installed

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

详细错误:

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 --> Player Settings 中进行设置

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

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

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

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

end

相关内容

热门资讯

解迷了解!威信茶馆免费辅助器(... 解迷了解!威信茶馆免费辅助器(辅助)上游炸金花果然真的是有辅助app(哔哩哔哩)1、威信茶馆免费辅助...
截至目前!wepoker私人局... 截至目前!wepoker私人局俱乐部(透视)摆八张辅助(一直真的有辅助平台)-哔哩哔哩截至目前!we...
教学辅助挂!哈糖大菠萝破解器(... 教学辅助挂!哈糖大菠萝破解器(透视)江湖悠悠内购破解版(辅助)果然存在有教程(哔哩哔哩)1、任何江湖...
黑科技攻略!wepoker怎么... 黑科技攻略!wepoker怎么获得好牌(透视)微乐锄大地脚本(果然是真的辅助工具)-哔哩哔哩wepo...
了解了解!福建兄弟13水辅助(... 了解了解!福建兄弟13水辅助(辅助)渝都麻将原来是有辅助安装(哔哩哔哩)1、下载好福建兄弟13水辅助...
刚刚!wepoker底牌透视(... 刚刚!wepoker底牌透视(透视)悟空大厅辅助器下载(果然是有辅助神器)-哔哩哔哩1、下载好悟空大...
黑科技辅助!werplan脚本... 黑科技辅助!werplan脚本(透视)赖子三加一辅助(辅助)竟然存在有器(哔哩哔哩)赖子三加一辅助能...
此事迅速冲上热搜!pokemm... 此事迅速冲上热搜!pokemmo脚本最新版(透视)微乐小程序微乐家乡辅助器(确实是有辅助技巧)-哔哩...
详情了解!518互游破解(辅助... 详情了解!518互游破解(辅助)雀星确实是有辅助插件(哔哩哔哩)1、实时518互游破解透视辅助更新:...
透视好友房!we-poker软... 透视好友房!we-poker软件(透视)大唐辅助安装(都是有辅助平台)-哔哩哔哩1、让任何用户在无需...