📜 ⬆️ ⬇️

Cutting resources - the way to optimize the size of iOS-applications

Application slicing is a new feature that appeared in iOS and tvOS version 9.0. Now developers can upload multiple resource options for different types of devices to the App Store. This significantly reduces the size of the application, since the user downloads a data packet only for his particular device.



Splitting the application is popular with developers, because it allows you to pack more assets into the original application and at the same time stay within the 100 MB limit for downloading via the mobile Internet. To achieve this result, developers can also refer to a similar method of using resources on request, which we wrote about earlier . However, this method is not always convenient and optimal: the application must be pre-adapted in order to dynamically load the necessary resources. The use of resources on demand is more complicated in all respects, and there are no difficulties with cutting the application.

Splitting an application can be of two types: slicing executable files and slicing resources. Cutting executable files is the removal of unused executable code from an application. This type is automatically applied to all App Store applications for iOS / tvOS version 9.0 and higher. The method of cutting resources, in turn, requires great effort from the developer, since it involves the removal of unused assets. This is the method we will consider in this article.
')
The main purpose of slicing application resources is to use unused bandwidth and memory capacity with several variants of the same asset. It is often necessary to use different versions of the asset, because the capabilities of older, but still widely used iOS devices are very different from the new versions. Prior to iOS 9.0, all assets had to be in the main application package, which led to a loss of bandwidth and memory on devices, since only one of all the options was used. The use of the resource slicing method allowed excluding unused assets from the main application package.

It is most convenient to cut application resources through asset packages. This is the easiest way to structure. Moreover, asset packages are easy to integrate into Unity, and their performance is directly proportional to loading regular arrays of data. Assets packages can also be used as a backend when using resources on demand, so the slicing function can be easily added to an application that already uses resources on demand and vice versa. After setting up the asset packages, you will need to make a few more changes in order to use resource slicing, as shown in the code examples below. In this article we will not consider in detail the asset package, so if you hear about them for the first time, we recommend that you read the materials on the link.

To cut resources, the developer needs to specify which devices are suitable for all options of assets, and load the necessary assets when the application starts. In the development of applications for the original iOS, resource slicing happens like this: inside the assets directory, data or image objects are created and resource options for devices are configured, and then resources are assigned to placeholders in the UI. In Unity, the developer himself configures resource options, which, in essence, are packages with a potentially different set of assets. All package variants involved in slicing application resources should be fixed in the code using the API callback function: UnityEditor.iOS.BuildPipeline.collectResources. Then the exact requirements for the devices are specified in the game settings UI. Finally, the developer must manually download asset packages via the AssetBundle.CreateFromFile API when launching the application.



Code examples that demonstrate the basic use of slicing application resources:

Script registration resources involved in the cutting application:

using UnityEditor.iOS; #if ENABLE_IOS_APP_SLICING public class BuildResources { [InitializeOnLoadMethod] 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"), }; } 


Download asset packages at launch:

 < ...> 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"); < ...> 


It is best to start exploring asset assemblies and slicing application resources from our Asset Bundle Manager demo project (Bit Assets Manager) on BitBucket . Follow the link for a detailed description of how to use and customize the demo project.

Source: https://habr.com/ru/post/276069/


All Articles