一、為什么用 IL2CPP
Unity的IL2CPP(Intermediate Language to C++)是一個(gè)編譯技術(shù),它將C#代碼轉(zhuǎn)換為C++代碼,然后再編譯成平臺(tái)相關(guān)的二進(jìn)制代碼。IL2CPP提供了幾個(gè)優(yōu)點(diǎn),特別是在性能和跨平臺(tái)部署方面。以下是IL2CPP的一些主要優(yōu)點(diǎn):
1.性能提升:IL2CPP通常能提供比傳統(tǒng)的Mono運(yùn)行時(shí)更好的性能,因?yàn)樗梢岳帽镜鼐幾g器的優(yōu)化。
2.跨平臺(tái)支持:由于IL2CPP生成的是C++代碼,這使得它更容易被跨平臺(tái)的編譯器支持,因此你可以將游戲編譯到多個(gè)平臺(tái),包括iOS、Android、Windows、Mac、Linux和游戲主機(jī)。
3.安全性增強(qiáng):IL2CPP提供了比Mono更高級(jí)別的安全性,因?yàn)樗恍枰\(yùn)行一個(gè)易于逆向工程的中間語(yǔ)言虛擬機(jī)。C++代碼更難被反編譯,這使得逆向工程變得更加困難。
4.完全AOT編譯(Ahead-of-Time):IL2CPP完全支持AOT編譯,這意味著所有的代碼都在部署之前編譯好了,有利于實(shí)現(xiàn)更快的啟動(dòng)時(shí)間和更穩(wěn)定的運(yùn)行時(shí)性能,這在iOS等不允許JIT編譯的平臺(tái)上尤其重要。
5.內(nèi)存管理改進(jìn):IL2CPP使用了更現(xiàn)代的垃圾回收技術(shù),可以更高效地管理內(nèi)存,減少內(nèi)存泄露和碎片化問題。
6.64位支持:IL2CPP支持64位計(jì)算,這是許多現(xiàn)代設(shè)備和操作系統(tǒng)的要求。
7.與現(xiàn)代C++編譯器的兼容性:由于生成的是C++代碼,IL2CPP可以利用現(xiàn)代C++編譯器(如Clang和GCC)進(jìn)行進(jìn)一步的優(yōu)化。
8.可預(yù)測(cè)的性能:由于所有的代碼都在編譯時(shí)就已經(jīng)確定了,運(yùn)行時(shí)的性能比JIT(Just-In-Time)編譯更加可預(yù)測(cè),因?yàn)樗粫?huì)在用戶使用應(yīng)用程序時(shí)進(jìn)行編譯。
9.通用基礎(chǔ)設(shè)施:IL2CPP為Unity帶來(lái)了一個(gè)通用的代碼基礎(chǔ)設(shè)施,用于支持不斷增長(zhǎng)的平臺(tái)數(shù)量,簡(jiǎn)化了Unity的維護(hù)和新平臺(tái)支持的工作。
10.現(xiàn)代語(yǔ)言特性支持:IL2CPP更好地支持C#的新特性,因?yàn)樗軌蚴褂眯碌腃#編譯器,而舊的Mono運(yùn)行時(shí)可能不支持這些新特性。
盡管IL2CPP提供了許多優(yōu)點(diǎn),但它也有其限制,比如編譯時(shí)間往往比Mono更長(zhǎng),且生成的二進(jìn)制文件大小可能會(huì)更大。然而,對(duì)于尋求性能和兼容性的開發(fā)者來(lái)說(shuō),IL2CPP提供了一個(gè)強(qiáng)有力的解決方案。
二、環(huán)境搭建
在安裝 Unity 的時(shí)候,記得將 Windows Build Support(IL2CPP) 勾上,如果是發(fā)布 Linux 系統(tǒng),則用 Linux Build Support(IL2CPP)
如果沒有安裝 IL2CPP,在發(fā)布界面這里會(huì)提示沒有安裝 IL2CPP,如下:
Currently selected scripting backend (IL2CPP) is not installed
另外還需要安裝 C++ 開發(fā)環(huán)境,否則不能發(fā)布項(xiàng)目,錯(cuò)誤如下:
詳細(xì)錯(cuò)誤:
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++ 環(huán)境,并勾選?Windows 10 SDK 安裝
上面兩個(gè)條件安裝完成后,還需要在 Unity --> Player Settings 中進(jìn)行設(shè)置
在 Configuration --> Scripting Backend 這里選擇 IL2CPP?
接下來(lái)就可以直接發(fā)布 PC 版的項(xiàng)目了,發(fā)布成功后,生成的文件和 Mono 不太一樣
發(fā)布 Android 也不需要改多少配置,這里我只是將 ARMv7 換成了 ARM64,其他的設(shè)置就是默認(rèn)的。
Android 打包是真的慢,一個(gè)空包差不多用了 20 分鐘,不過(guò)包小了很多,只有 10 M。
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-860410.html
end文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-860410.html
到了這里,關(guān)于Unity 使用 IL2CPP 發(fā)布項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!