Unity 使用 IL2CPP 发布项目

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

  • 12
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity使用 Gradle 来编译 il2cpp,需要进行以下步骤: 1. 打开 Unity,选择 File -> Build Settings,在弹出的窗口中选择 Android 平台。 2. 在右侧的面板中,选择 Player Settings,进入 Android Player Settings 界面。 3. 在 Android Player Settings 界面中,找到 Publishing Settings,勾选 Custom Main Gradle Template。 4. 点击 Edit Main Gradle Template 按钮,打开 Gradle 模板文件。 5. 在模板文件中,找到 buildTypes 部分,添加以下代码: ``` release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' ndk { abiFilters "armeabi-v7a", "arm64-v8a" } externalNativeBuild { ndkBuild { path "src/main/cpp/Android.mk" } } } ``` 说明: - `minifyEnabled` 表示是否启用代码混淆,默认为 false。 - `proguardFiles` 表示混淆规则文件,默认为 Android SDK 中的 proguard-android.txt 文件。 - `ndk` 表示指定编译的 ABI 类型,这里指定了 armeabi-v7a 和 arm64-v8a 两种类型。 - `externalNativeBuild` 表示使用外部编译工具进行编译。 - `ndkBuild` 表示使用 ndk-build 工具进行编译。 - `path` 表示 ndk-build 工具的编译脚本文件路径。 6. 保存模板文件,返回 Android Player Settings 界面。 7. 在 Android Player Settings 界面中,找到 Publishing Settings,勾选 Custom Gradle Properties。 8. 在 Custom Gradle Properties 中添加以下代码: ``` android.useAndroidX=true android.enableJetifier=true ``` 说明: - `android.useAndroidX` 表示是否使用 AndroidX 库,默认为 true。 - `android.enableJetifier` 表示是否启用 Jetifier 工具,将旧版的 Support 库转换为 AndroidX 库,默认为 true。 9. 点击 Build 按钮,进行 il2cpp 编译。编译完成后,会生成生成 APK 文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊思宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值