Slow Unity IL2CPP Builds Usually Come in Three Kinds

Reading through Unity forum threads about slow IL2CPP builds, I noticed people describe a few different problems with the same words. “IL2CPP is slow” can mean very different things, and the fix depends on which one you have. Before anything else, check that IL2CPP is actually the slow step. A player build also compiles shaders, and one developer on the Unity forums found that 3 to 4 hours of a build lasting over 5 hours was shader variant compilation, not IL2CPP. In the Editor log, the IL2CPP stages show up as “Compiling cpp for libil2cpp” and, on Windows, “Linking GameAssembly.dll”.

在阅读关于 Unity IL2CPP 构建缓慢的论坛帖子时,我注意到人们用同样的词汇描述了几个不同的问题。“IL2CPP 很慢”可能意味着完全不同的情况,解决方法取决于你具体遇到了哪一种。在做任何事之前,请先确认 IL2CPP 是否真的是导致缓慢的环节。玩家构建过程还会编译着色器,Unity 论坛上的一位开发者发现,在一个持续超过 5 小时的构建中,有 3 到 4 小时是着色器变体编译,而不是 IL2CPP。在编辑器日志中,IL2CPP 阶段显示为“Compiling cpp for libil2cpp”,在 Windows 上则显示为“Linking GameAssembly.dll”。

  1. Only the first build is slow This is normal. Unity’s incremental build pipeline reuses work from the previous build, and most of that cache lives in Library/Bee. The first build has nothing to reuse. So the fix is to keep the cache, not to change settings. On CI, a runner that starts from a fresh checkout pays this cost every time, so persist Library/Bee between runs.

  2. 只有第一次构建很慢:这是正常的。Unity 的增量构建流水线会重用上一次构建的工作成果,而大部分缓存位于 Library/Bee 中。第一次构建时没有可重用的内容。因此,解决方法是保留缓存,而不是更改设置。在持续集成(CI)中,从全新检出开始的运行器每次都会付出这个代价,因此请在多次运行之间持久化 Library/Bee。

  3. Every build is slow This is the one settings can help with. For builds you iterate on: set C++ Compiler Configuration to Debug set IL2CPP Code Generation to Optimize for code size and build time build only the architecture of your test device Both settings can make the game run slower, so keep them out of the build you ship. Unity also recommends excluding your project and build folders from antivirus scans and using your fastest disk, since IL2CPP does a lot of reading and writing.

  4. 每次构建都很慢:这是可以通过设置来改善的情况。对于你进行迭代的构建:将 C++ 编译器配置设置为 Debug;将 IL2CPP 代码生成设置为“优化代码大小和构建时间”;仅构建测试设备的架构。这两个设置都可能导致游戏运行变慢,因此在发布版本中请勿使用。Unity 还建议将项目和构建文件夹排除在杀毒软件扫描之外,并使用最快的磁盘,因为 IL2CPP 会进行大量的读写操作。

  5. Builds were fast and suddenly got slow This often means the cache stopped being reused and every build is a full rebuild. Things to check: a build script or CI job passing BuildOptions.CleanBuildCache missing ScriptsOnlyCache.yaml or DataBuildDirtyInfo.json in Library/PlayerDataCache something outside Unity (a cleanup script, a sync tool) touching Library Some cases never get explained. In one forum thread where builds went from about 30 seconds to 20 minutes, the reply was that the cache was being cleared and nobody could tell why.

  6. 构建曾经很快,突然变慢了:这通常意味着缓存不再被重用,每次构建都变成了全量重新构建。需要检查的事项包括:构建脚本或 CI 任务是否传递了 BuildOptions.CleanBuildCache;Library/PlayerDataCache 中是否缺少 ScriptsOnlyCache.yaml 或 DataBuildDirtyInfo.json;Unity 之外的某些程序(如清理脚本、同步工具)是否触动了 Library 文件夹。有些情况永远无法解释。在一个论坛帖子中,构建时间从约 30 秒增加到 20 分钟,回复称缓存被清除了,但没人知道原因。

A rough decision flow If you have hit one of these, which one was it for you? (I used AI to help me write this up in English.)

一个粗略的决策流程:如果你遇到了这些问题中的一个,你属于哪种情况?(我使用了人工智能来帮助我用英语撰写此文。)