Version: 5.6
Measuring Performance with the Built-in Profiler
优化物理性能

Optimizing the size of the built iOS player

The two main ways of reducing the size of the player are by making proper Release build within Xcode and by changing the Stripping Level within Unity.

为分发进行构建

It is expected that final release builds are made using Xcode 4.x/5.x command Product -> Archive. Using this command ensures that build is made with release configuration and all the debug symbols are stripped. After issuing this command latest Xcode switches to Organizer window Archives tab (you can navigate there manually via Window -> Organizer menu). You will find there two very useful functions there: App Store size estimation and Distribution. Build size estimation function works pretty well, but it is always recommended to have some small extra margin for error when aiming for 3G download limit (which currently is 100MB).

iOS 剥离级别

The size optimizations activated by stripping work in the following way:

1.Strip assemblies 级别:分析脚本的字节码,以便可从 DLL 中删除未在脚本中引用的类和方法,从而在 AOT 编译阶段中将它们排除。此优化可以减小主二进制文件和附带 DLL 的大小,只要不使用反射就是安全的。

1.Strip ByteCode 级别:所有 .NET DLL(存储在 Data 文件夹中)都会被剥离为仅剩下元数据。之所以可以这样做,是因为所有代码都已在 AOT 阶段预编译并链接到主二进制文件中。

1.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 进行剥离

Refer to documentaiton on managed bytecode stripping with IL2CPP for more information

Note: it can sometimes be difficult to determine which classes are getting stripped in error even though the application requires them. You can often get useful information about this by running the stripped application on the simulator and checking the Xcode console for error messages.

关于使分发大小尽可能小的简单核对表

  1. Minimize your assets: enable PVRTC compression for textures and reduce their resolution as far as possible. Also, minimize the number of uncompressed sounds. There are some additional tips for file size reduction here.
  2. Set the iOS Stripping Level to Use micro mscorlib. 1.将脚本调用优化 (Script Call Optimization) 级别设置为 Fast but no exceptions。 1.不要在代码中使用 System.dll 或 System.Xml.dll 中的任何内容。这些库与 micro mscorlib 兼容。 1.删除不必要的代码依赖项。 1.将 API 兼容性级别 (API Compatibility Level) 设置为 .Net 2.0 subset。请注意,.Net 2.0 子集与其他库的兼容性有限。
  3. Don’t use JS Arrays. 1.避免将通用容器与值类型(包括结构)结合使用。

使用 Unity 制作的应用程序最小为多大?

如果关闭所有大小优化,则空项目在 App Store 中将小于 22 MB。进行代码剥离后,只包含主摄像机的空场景可以在 App Store 中减少到小于 12 MB(经过压缩并附加 DRM)。

为什么我的应用程序在发布到 App Store 后会增大?

发布应用程序时,Apple App Store 服务首先加密二进制文件,然后通过 zip 对其进行压缩。加密会增加代码段的“随机性”,从而使压缩效果变差。请查看上面的“为分发进行构建”一章,了解如何在提交前估计 App Store 大小。


Measuring Performance with the Built-in Profiler
优化物理性能