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

相关内容

热门资讯

关于透视!微信中至有辅助吗,四... 关于透视!微信中至有辅助吗,四川途游小程序辅助软件(附辅助开挂安装);四川途游小程序辅助软件是一种具...
发现透视!随意玩最新跳转链接有... 发现透视!随意玩最新跳转链接有辅助吗,桂林字牌辅助科技(附开挂辅助工具);1、让任何用户在无需AI插...
实测透视!欢乐情怀辅助挂,斗棋... 实测透视!欢乐情怀辅助挂,斗棋辅助脚本视频(附辅助开挂插件);大家肯定在之前斗棋辅助脚本视频或者斗棋...
原来有透视!三哥玩透视辅助,新... 原来有透视!三哥玩透视辅助,新畅游互娱辅助(附辅助开挂脚本);小薇(透视辅助)致您一封信;亲爱新畅游...
了解透视!钱塘十水三挂件,哈糖... 了解透视!钱塘十水三挂件,哈糖大菠萝提高胜率(附开挂辅助插件);1、点击下载安装,哈糖大菠萝提高胜率...
分享透视!新九哥破解版,衢州都... 【福星临门,好运相随】;分享透视!新九哥破解版,衢州都莱罗松辅助器(附辅助开挂安装);暗藏猫腻,小编...
发现透视!新九五有没有脚本,w... 发现透视!新九五有没有脚本,wepoker破解版内购(附开挂辅助工具);AI辅助机器人普及解说快速成...
推荐透视!微乐小程序游戏破解器... 推荐透视!微乐小程序游戏破解器,萍乡滚筒四幅攻略(附开挂辅助神器);微乐小程序游戏破解器是一种具有地...
传授透视!吉祥填大坑有什么诀窍... 【福星临门,好运相随】;传授透视!吉祥填大坑有什么诀窍,反杀大厅辅助(附辅助开挂软件);暗藏猫腻,小...
发现透视!易趣麻将辅助器,蜀渝... 发现透视!易趣麻将辅助器,蜀渝牌乐汇脚本(附辅助开挂神器);一、蜀渝牌乐汇脚本AI软件牌型概率发牌机...