Unity使用IL2CPP方式打Windows包

笔者Unity版本2021.3.8f1

1,确保安装了对应版本的扩展包:UnitySetup-Windows-IL2CPP-Support-for-Editor

2,打包报错:

error: Could not set up a toolchain for Architecture x64. Make sure you have the right build tools installed for il2cpp builds. Details:
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Internal build system error. BuildProgram exited with code -2146233088.
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 SDK (recommended)
 * Visual Studio 2019 with C++ compilers and Windows 10 SDK
 * Visual Studio 2017 with C++ compilers and Windows 10 SDK
 * Visual Studio 2015 with C++ compilers and Windows 10 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 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

Windows 10 SDK is not installed. You can install from here: https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/


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 SDK (recommended)
 * Visual Studio 2019 with C++ compilers and Windows 10 SDK
 * Visual Studio 2017 with C++ compilers and Windows 10 SDK
 * Visual Studio 2015 with C++ compilers and Windows 10 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 SDK is detected by looking at "SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0\InstallationFolder" in the registry.

这里:unable-to-build-il2cpp 建议使用Visual Studio Installer补充安装一些C++相关的工具包。
在这里插入图片描述

3,若依旧报错类似Building Library\Bee\artifacts\WinPlayerBuildProgram\ei6vj\ejx6_b_vm6.lump.obj failed with output: i3ib_vm6.lump.cpp
可以考虑把如下脚本放到Assets目录下(来自这里)

#if UNITY_EDITOR
using System;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class MsvcStdextWorkaround : IPreprocessBuildWithReport
{
    const string kWorkaroundFlag = "/D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS";
    public int callbackOrder => 0;
    public void OnPreprocessBuild(BuildReport report)
    {
        var clEnv = Environment.GetEnvironmentVariable("_CL_");
        if (string.IsNullOrEmpty(clEnv))
        {
            Environment.SetEnvironmentVariable("_CL_", kWorkaroundFlag);
        }
        else if (!clEnv.Contains(kWorkaroundFlag))
        {
            clEnv += " " + kWorkaroundFlag;
            Environment.SetEnvironmentVariable("_CL_", clEnv);
        }
    }
}
#endif // UNITY_EDITOR
Unity 中,可以使用 BuildPipeline.BuildPlayer 方法来实现一键打。而编译 il2cpp 则需要使用 UnityIL2CPP 构建选项。 以下是一个简单的示例代码,演示了如何实现一键打并编译 il2cpp: ```csharp using UnityEditor; using UnityEditor.Build.Reporting; using UnityEngine; public class BuildScript : MonoBehaviour { [MenuItem("Build/Build and Compile il2cpp")] public static void BuildAndCompile() { // 设置打参数 string[] scenes = { "Assets/Scenes/MyScene.unity" }; string outputPath = "Builds/MyGame.exe"; BuildPlayerOptions buildOptions = new BuildPlayerOptions(); buildOptions.scenes = scenes; buildOptions.locationPathName = outputPath; buildOptions.target = BuildTarget.StandaloneWindows64; buildOptions.options = BuildOptions.None; // 打 BuildReport buildReport = BuildPipeline.BuildPlayer(buildOptions); // 检查打结果 if (buildReport.summary.result == BuildResult.Succeeded) { // 编译 il2cpp PlayerSettings.SetScriptingBackend(BuildTargetGroup.Standalone, ScriptingImplementation.IL2CPP); PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.Standalone, Il2CppCompilerConfiguration.Release); PlayerSettings.SetArchitecture(BuildTargetGroup.Standalone, 2); BuildPipeline.BuildPlayer(buildOptions); Debug.Log("Build and il2cpp compile succeeded!"); } else { Debug.LogError("Build failed!"); } } } ``` 在上面的代码中,我们在菜单栏中添加了一个 "Build/Build and Compile il2cpp" 的选项,当用户点击此选项时,会执行 BuildAndCompile 方法。该方法首先使用 BuildPipeline.BuildPlayer 方法进行打,然后检查打结果。如果打成功,则设置编译 il2cpp 所需的参数,并再次调用 BuildPipeline.BuildPlayer 方法进行编译。 需要注意的是,IL2CPP 编译可能需要一定的时间,具体取决于项目的规模和复杂度。因此,建议用户在执行一键打并编译 il2cpp 之前,先备份项目以防止意外情况发生。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

iningwei

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

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

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

打赏作者

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

抵扣说明:

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

余额充值