Unity3D优化之Optimizing the Size of the Built iOS Player

来源:互联网 发布:阅读mobi的软件 编辑:程序博客网 时间:2024/06/08 11:42

Optimizing the Size of the Built iOS Player  优化内置 iOS Player的尺寸

 

The two main ways of reducing the size of the player are by changing theActive Build Configuration within Xcode and by changing theStripping Level within Unity.

降低的播放机大小主要有两个途径:一是更改Xcode内的Active Build Configuration,二是更改UnityStripping级别

 

Building in Release Mode

在发布模式下生成

You can choose between the Debug andRelease options on theActive Build Configuration drop-down menu in Xcode. Building asRelease instead ofDebug can reduce the size of the built player by as much as 2-3MB, depending on the game.

你可以在 Xcode的Active Build Configuration下拉菜单中的调试和发布选项之间进行选择。根据自身游戏的大小,选择发布模式生成的player会比选择调试模式减少2-3MB的大小。

 

In Release mode, the player will be built without any debug information, so if your game crashes or has other problems there will be no stack trace information available for output. This is fine for deploying a finished game but you will probably want to use Debug mode during development.

在发布模式下,player会去掉任何的调试信息进行生成,所以如果你的游戏崩溃或有其他问题,将是没有可用的、可输出的堆栈跟踪信息。这适合发布已完成的游戏,但你更应该在开发过程中使用调试模式。

 

iOS Stripping Level (Advanced License feature)

iOS Stripping级别(Advanced License feature

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

尺寸优化工作可通过以下几种方式被剥离机制激活:

 

1.      Strip assemblies level: the scripts' bytecode is analyzed so that classes and methods that are not referenced from the scripts can be removed from the DLLs and

          thereby excluded from the AOT compilation phase. This optimization reduces the size of the main binary and accompanying DLLs and is safe as long as no

          reflection is used.

          Strip assemblies级别:通过分析脚本的字节码,来将这些脚本没有引用的类和方法从 Dll 中删除,从而使其不进入 AOT编译阶段。这种优化减少了主要的二进制以及

           相应 Dll的大小。在不使用反射的条件下,这种优化是安全的。

 

2.      Strip ByteCode level: any .NET DLLs (stored in the Data folder) are stripped down to metadata only. This is possible because all the code is already precompiled

          during the AOT phase and linked into the main binary.

          Strip ByteCode级别:任何.Net Dll(存储数据文件夹中)将从元数据中被剥离出来。这是可能的,因为所有的代码都已经 AOT阶段被预编译并链接到主要的二进制文件

           中。

 

3.      Use micro mscorlib level: a special, smaller version of mscorlib is used. Some components are removed from this library, for example, Security, Reflection.Emit,

          Remoting, non Gregorian calendars, etc. Also, interdependencies between internal components are minimized. This optimization reduces the main binary and

          mscorlib.dll size but it is not compatible with some System and System.Xml assembly classes, so use it with care.

          使用micro mscorlib级别:使用一种特殊的、较小的mscorlib版本。某些部件将从此库中被移除,例如,安全,Reflection.Emit,远程处理,非公历日历等组件都将被删

          除。此外,内部组件之间的相互依赖性也会被降至最低。该种优化减少了主要的二进制以及mscorlib.dll大小,但不能与一些系统和 System.Xml程序集类兼容。因此,

         小心地使用它。

 

These levels are cumulative, so level 3 optimization implicitly includes levels 2 and 1, while level 2 optimization includes level 1.

这些级别是累积的,第3级优化隐式地包含级别 2和级别1,而第 2级优化则包含级别1。

 

Note:Micro mscorlib is a heavily stripped-down version of the core library. Only those items that are required by the Mono runtime in Unity remain. Best practice for using micro mscorlib is not to use any classes or other features of .NET that are not required by your application. GUIDs are a good example of something you could omit; they can easily be replaced with custom made pseudo GUIDs and doing this would result in better performance and app size.

Micro mscorlib是核心库的最低级版本。只有这些项目是目前Unity中的Mono运行库所需要的。使用micro mscorlib的最佳做法是不使用任何你的应用程序所不需要的.Net的类或其他功能。GUID就是一个你可以忽略的例子,她可以轻松地被自定义的伪 Guid所替代,这样做将产生更好的性能和更小的应用程序。

Tips

How to Deal with Stripping when Using Reflection

如何处理使用反射机制的Strip

 

Stripping depends highly on static code analysis and sometimes this can't be done effectively, especially when dynamic features like reflection are used. In such cases, it is necessary to give some hints as to which classes shouldn't be touched. Unity supports a per-project custom strippingblacklist. Using the blacklist is a simple matter of creating alink.xml file and placing it into the Assets folder. An example of the contents of thelink.xml file follows. Classes marked for preservation will not be affected by stripping:

Strip程度取决于静态代码的分析,但有时侯,这种分析并不是很有效,特别是针对使用类似反射一样的动态功能的代码。在这种情况下,有必要给出一些提示来指出哪些类是不应该被触动的。Unity支持对于每个项目都给出一个自定义Strip黑名单。使用黑名单是创建 link.xml 文件并将它放入Assets文件夹的一个简单方法。以下是一个的 link.xml文件的示例。其中有保留标记的类将不会被Strip:

 

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.

有时侯,我们很难确定哪些类会被进行错误的Strip。通常你可以通过在模拟器上运行已被Strip的应用程序和检查 Xcode 控制台的错误消息来获取有用的信息。

 

Simple Checklist for Making Your Distribution as Small as Possible

简单的清单,使你的发布版本尽可能地小

 

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.

         最小化你的资源(assets):启用 PVRTC压缩纹理,并尽可能低降低其分辨率。此外,尽量缩减无损压缩的音频的个数。这里还有一些额外的缩减文件大小的提示。

 

2.      Set the iOS Stripping Level toUse micro mscorlib.

         设置iOSStrip级别为Use micro mscorlib

 

3.      Set the script call optimization level toFast but no exceptions.

         设置脚本调用优化级别为Fast but no exceptions。

 

4.      Don't use anything that lives in System.dll or System.Xml.dll in your code. These libraries arenot compatible with micro mscorlib.

         不要让你的任何代码与System.dll或 System.Xml.dll有关联,因为这些库并不兼容micro mscorlib。

 

5.      Remove unnecessary code dependencies.

         去掉不必要的代码关联。

 

6.      Set the API Compatibility Level to.Net 2.0 subset. Note that .Net 2.0 subset has limited compatibility with other libraries.

         将API兼容性级别设置为.Net 2.0子集。请注意.Net 2.0子集与其他库与兼容性比较有限。

 

7.      Set the Target Platform toarmv6 (OpenGL ES1.1).

         设置目标平台为armv6 (OpenGL ES1.1)

 

8.      Don't use JS Arrays.

         不要使用 JS阵列。

 

9.      Avoid generic containers in combination with value types, including structs.

         避免泛型容器与结构等类型相结合。

 

 

Can I produce apps of less than 20 megabytes with Unity?

我能用Unity创造出低于 20 mb的应用程序吗?

 

Yes. An empty project would take about 13 MB in the AppStore if all the size optimizations were turned off. This gives you a budget of about 7MB for compressed assets in your game. If you own an Advanced License (and therefore have access to the stripping option), the empty scene with just the main camera can be reduced to about 6 MB in the AppStore (zipped and DRM attached) and you will have about 14 MB available for compressed assets.

当然可以。如果所有优化被都关掉,一个空项目在AppStore中大小大致为 13 MB左右。这在游戏中为你提供了大约7 MB的压缩资源预算。如果你拥有先进的许可证(并因此获得Strip选项),那么只含有主相机的空场景在AppStore中可压缩到大约 6 MB (zipped和 DRM连接),这样,你就可以有大约 14 MB可用空间用于压缩资源。

 

 

Why did my app increase in size after being released to the AppStore?

为什么我的应用程序发布到AppStore后大小增加了?

When they publish your app, Apple first encrypt the binary file and then compresses it via zip. Most often Apple's DRM increases the binary size by about 4 MB or so. As a general rule, you should expect the final size to be approximately equal to the size of the zip-compressed archive of all files (except the executable) plus the size of the uncompressed executable file.

当他们发布你的应用程序时,苹果首先对二进制文件进行加密,然后将它通过 zip压缩。最常见的,苹果 DRM会使二进制文件大小增加大约 4 MB左右。作为一般规则,你应预见到的文件最终大小大致为所有文件(除了可执行文件)的 zip压缩大小,再加上未压缩的可执行文件的大小。

 

备注:最近一直在研究Unity3D的性能优化问题,这段时间可能会多翻译这方面的文章,如有翻译不当之处还请多多指出。

原文地址:http://unity3d.com/support/documentation/Manual/iphone-playerSizeOptimization.html

0 0