App slicing allows you to dynamically download Assets according to the specification of the device the application is running on. For example, app slicing can download high-resolution Assets for larger devices and low-resolution Assets for smaller devices. App slicing works by defining AssetBundles, with the added provision of variants. You can decide which variant to use at startup and automatically append this to the Asset file name upon download.
To create a variant, use the following steps:
BuildiOSAppSlices.cs
.textures
and variants hd
and sd
with your own. In this code example, multiple folders are referenced: one containing HD textures and one containing SD textures. These folders have been given the variants hd and sd.using UnityEngine;
using UnityEditor;
public class BuildiOSAppSlices : MonoBehaviour
{
[InitializeOnLoadMethod]
static void SetupResourcesBuild( )
{
UnityEditor.iOS.BuildPipeline.collectResources += CollectResources;
}
static UnityEditor.iOS.Resource[] CollectResources( )
{
return new UnityEditor.iOS.Resource[]
{
new UnityEditor.iOS.Resource("textures").BindVariant( "Assets/ODR/textures.hd", "hd" )
.BindVariant( "Assets/ODR/textures.sd", "sd" )
.AddOnDemandResourceTags( "textures" ),
};
}
[MenuItem( "Bundle/Build iOS App Slices" )]
static void BuildAssetBundles( )
{
var options = BuildAssetBundleOptions.None;
bool shouldCheckODR = EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS;
#if UNITY_TVOS
shouldCheckODR |= EditorUserBuildSettings.activeBuildTarget == BuildTarget.tvOS;
#endif
if( shouldCheckODR )
{
#if ENABLE_IOS_ON_DEMAND_RESOURCES
if( PlayerSettings.iOS.useOnDemandResources )
options |= BuildAssetBundleOptions.UncompressedAssetBundle;
#endif
#if ENABLE_IOS_APP_SLICING
options |= BuildAssetBundleOptions.UncompressedAssetBundle;
#endif
}
BuildPipeline.BuildAssetBundles( "Assets/ODR", options, EditorUserBuildSettings.activeBuildTarget );
}
}
This code creates a new menu in the Unity Editor menu bar called Bundle. Click this and select Build iOS App Slices. This generates the AssetBundles in the ODR
folder.
To load an Asset, create a script file named LoadBundle.cs
in your Assets folder and copy in the following code. Replace textures
with the name of the variant you want to load.
using UnityEngine;
using UnityEngine.iOS;
using System;
using System.Collections;
public class LoadBundle : MonoBehaviour
{
public AssetBundle TextureBundle;
void Start( )
{
LoadAssetAsync( "textures", "textures" );
}
public IEnumerator LoadAsset( string resourceName, string odrTag )
{
// Create the request
using(OnDemandResourcesRequest request = OnDemandResources.PreloadAsync( new string[] { odrTag } ))
{
// Wait until request is completed
await request;
// Check for errors
if( request.error != null )
throw new Exception( "ODR request failed: " + request.error );
TextureBundle = AssetBundle.LoadFromFile( "res://" + resourceName );
}
}
}
There might be times where you want to use a variant on a specific device, or on devices that run over a set memory limit. To do this, you can change the settings of each variant you use.
To modify a variant, navigate to Player Settings > Other Settings > Configuration and expand Variant map for app slicing.
Note: Enable Use on demand resources in Player Settings > Other > Configuration to view the Variant map for app slicing options.
Setting | Description | |
---|---|---|
Variant name | Displays the name of the variant from the loading script. | |
Device | Choose which device this variant targets. | |
Memory | Select the minimum memory required for this variant. | |
Graphics | Choose the Metal framework to use with this variant. | |
Display color space | Select the color gamut to use with this variant. | |
Add custom entry | Click Add custom entry to add your own custom settings and values. | |
Key | Enter the name of the setting. | |
Value | Enter the value for this setting. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.