减小播放器大小的两种主要方法是在 Xcode 中创建合理的 Release build 以及在 Unity 中更改 Stripping Level。
通常应使用 Xcode 命令 Product > Archive 进行最终发行版构建。使用此命令可确保使用发布配置进行构建并剥离所有调试符号。 发出此命令后,Xcode 将切换到 Organizer 窗口 Archives 选项卡。如需了解关于如何计算应用程序大小的准则以及其他关于减小大小的技巧,请参阅 Apple 的技术问答:减小我的应用程序的大小 (Reducing the size of my App)。
注意:我们建议您在设置无线下载限制(目前为 150MB)时留出少量的误差余地。
使用以下剥离方式激活 Mono 脚本后端构建的大小优化:
Strip assemblies 级别:分析脚本的字节码,以便可从 DLL 中删除未在脚本中引用的类和方法,从而在 AOT 编译阶段中将它们排除。此优化可以减小主二进制文件和附带 DLL 的大小,只要不使用反射就是安全的。
Strip ByteCode 级别:所有 .NET DLL(存储在 Data 文件夹中)都会被剥离为仅剩下元数据。之所以可以这样做,是因为所有代码都已在 AOT 阶段预编译并链接到主二进制文件中。
Use micro mscorlib 级别:使用特殊的较小版本的 mscorlib。某些组件将从此库中删除,例如 Security、Reflection.Emit、Remoting、非 Gregorian 日历等。此外,内部组件之间的相互依赖关系将最小化。此优化可以减小主二进制文件和 mscorlib.dll 大小,但与某些 System 和 System.Xml 程序集类不兼容,因此请谨慎使用。
这些级别是累积的,因此级别 3 优化隐含地包括级别 2 和 1,而级别 2 优化包括级别 1。
请注意,Micro mscorlib 是核心库的大幅度精简版本。只保留 Unity 中 Mono 运行时所需的项。使用 micro mscorlib 的最佳做法是不要使用应用程序不需要的任何类或其他 .NET 功能。GUID 就是一个可以省略的典型示例;很容易将它们替换为定制的伪 GUID,这样做可以带来更好的性能和更小的应用程序大小。
请参阅有关使用 IL2CPP 进行托管字节码剥离的文档以了解更多信息
注意:有时候很难确定哪些类被错误剥离(虽然应用程序需要这些类)。通常,通过在模拟器上运行剥离后的应用程序并检查 Xcode 控制台是否有错误消息,可以获得有用的信息。
如果关闭所有大小优化,则空项目在 App Store 中将小于 22 MB。进行代码剥离后,只包含主摄像机的空场景可以在 App Store 中减少到小于 12 MB(经过压缩并附加 DRM)。
发布应用程序时,Apple App Store 服务首先加密二进制文件,然后通过 zip 对其进行压缩。加密会增加代码段的“随机性”,从而使压缩效果变差。请查看上面的“为分发进行构建”一章,了解如何在提交前估计 App Store 大小。
2018–06–14 页面已修订
2017–14–06 - 更新了“使用 IL2CPP 进行剥离”部分
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.