Искать на сайте Unity

Оптимизация размера приложений для iOS с помощью сокращения ресурсов

28 декабря 2015 г. через Технологии | 4 мин. читать

App slicing is a new iOS feature available since iOS and tvOS version 9.0. It's purpose is to reduce the size of the main application bundle by allowing developers to upload several device-dependent versions of resources to the App Store. Only one out of these is included in the App bundle for the device of the particular user.

App slicing is very useful on iOS platform, because it helps the developers to pack more assets into the initial app bundle and still stay within the 100MB over-the-air download limit. To achieve this objective, the developer may also use the counterpart of app slicing, on-demand resources, which we have covered previously. However, in order to use on-demand resources, the app needs to be adapted to download needed resources dynamically, which is not always convenient or optimal. It’s also more complex in all cases. App slicing does not have these drawbacks.

There are two flavors of app slicing: app executable slicing and app resource slicing. The former refers to removal of unneeded executable code from the application bundle and is employed automatically on the App Store for all apps that target iOS/tvOS version 9.0 and higher. The latter refers to removal of unneeded assets and needs more developer effort in order to work. This is what we will cover in this blog post.

The primary objective of app resource slicing is to reclaim wasted bandwidth and space in cases when there are several variants of the same asset. Usage of different asset variants is frequently needed because there is large difference of hardware capabilities between the most recent and older, but still widely used iOS devices. Most of the time developers need to use assets of different quality to fully utilize the most performant devices without penalizing the rest. Prior to iOS 9.0, all asset variants had to be included into the main app bundle. That wasted bandwidth and storage space on devices, as all but one asset variants were unused. By employing app resource slicing, it’s possible to exclude any unneeded assets from the application bundle.

The most convenient way of taking advantage of app resource slicing is via asset bundles. They are the easiest option for adapting the layout of the application assets for slicing. Asset bundles are integrated within Unity and the performance is effectively equivalent to loading from regular data files. Asset bundles are also used as the backend for on-demand resources, thus it's convenient to add app thinning support to an app that already uses on-demand resources or vice-versa. Once asset bundles are configured, you need very few additional changes to use app resource slicing, as shown in the code examples below. In this blog post, we won’t cover asset bundles. If this is the first time you hear about them, this resource will help you.

In order to use app resource slicing, the developer needs to specify which devices are eligible for all variants of assets that need to participate in resource slicing and then load the assets during application startup. In vanilla iOS app development, this is done by creating data set or image set items within an asset catalog, setting up device variations and then assigning resources to the placeholders in the UI. In Unity, the developer needs to set up asset bundle variants, that is, several asset bundles containing the same set of assets assets themselves potentially being different. All asset bundle variants that participate in app resource slicing need to be registered in code using UnityEditor.iOS.BuildPipeline.collectResources callback API. Later, the exact device requirements are specified in the player settings UI. Finally, the developer needs to manually load asset bundle via AssetBundle.CreateFromFile API on application startup.


The following two code examples demonstrate the essence of using app resource slicing:

Editor script to register resources that should participate in app resource slicing:

using UnityEditor.iOS;
public class BuildResources
 static void SetupResourcesBuild()
   UnityEditor.iOS.BuildPipeline.collectResources += CollectResources;
 static UnityEditor.iOS.Resource[] CollectResources()
  return new Resource[] {
    new Resource("asset-bundle-name").BindVariant("path/to/asset-bundle.hd"), "hd")
                                     .BindVariant("path/to/asset-bundle.md"), "md")
                                     .BindVariant("path/to/asset-bundle.sd"), "sd"),

Loading of asset bundles during startup:

< ...>
var bundle = AssetBundle.LoadFromFile("res://asset-bundle-name");

// now use AssetBundle APIs to load assets
// or Application.LoadLevel to load scenes

var asset = bundle.LoadAsset("Asset");
< ...>

The easiest way to start exploring asset bundles and app resource slicing is to use our Asset Bundle Manager demo project, which is available on BitBucket. The landing page offers a comprehensible description of how to use and tweak the demo.

28 декабря 2015 г. через Технологии | 4 мин. читать
Unity, логотипы Unity и другие торговые знаки Unity являются зарегистрированными торговыми знаками компании Unity Technologies или ее партнеров в США и других странах (подробнее здесь). Остальные наименования и бренды являются торговыми знаками соответствующих владельцев.