优化unity发布 iOS应用大小 Optimizing the Size of the Built iOS Player<转>

原文链接:http://www.ceeger.com/Manual/iphone-playerSizeOptimization.html

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

降低的播放机大小主要有两个途径:一是更改Xcode 内的Active Build Configuration,二是更改Unity的剥离级别(Stripping Level)。

Building in Release Mode 在发布模式下生成

You can choose between the Debug and Release options on the Active Build Configuration drop-down menu in Xcode. Building as Releaseinstead of Debug can reduce the size of the built player by as much as 2-3MB, depending on the game.

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


The Active Build Configuration drop-down 活动生成配置下拉列表

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.

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

iOS Stripping Level (Advanced License feature) 
iOS 剥离级别 (高级版许可功能)

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. 
    剥离程序集级别: 通过分析脚本的字节码,来将这些脚本没有引用的类和方法从 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. 
    剥离字节码级: 任何.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
如何使用反射处理剥离

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 a link.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:-

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

<linker>
       <assembly fullname="System.Web.Services">
               <type fullname="System.Web.Services.Protocols.SoapTypeStubInfo" preserve="all"/>
               <type fullname="System.Web.Services.Configuration.WebServicesConfigurationSectionHandler" preserve="all"/>
       </assembly>

       <assembly fullname="System">
               <type fullname="System.Net.Configuration.WebRequestModuleHandler" preserve="all"/>
               <type fullname="System.Net.HttpRequestCreator" preserve="all"/>
               <type fullname="System.Net.FileWebRequestCreator" preserve="all"/>
       </assembly>
</linker>

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.

注意:有时侯,我们很难确定哪些类会被进行错误的剥离。通常你可以通过在模拟器上运行已被剥离的应用程序和检查 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
    最小化你的资源: 启用 PVRTC 压缩纹理,并尽可能低降低其分辨率。此外,尽量缩减无损压缩的音频的个数。这里还有一些额外的缩减文件大小的提示。
  2. Set the iOS Stripping Level to Use micro mscorlib
    设置iOS剥离级别为Use micro mscorlib。
  3. Set the script call optimization level to Fast 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 are not 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 to armv6 (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 的压缩资源预算。如果你拥有先进的许可证 (并因此获得剥离选项),那么只含有主相机的空场景在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 压缩大小,再加上未压缩的可执行文件的大小。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值