Hello,
We are releasing a second preview build for iOS 9 On Demand Resources (ODR) and
App Slicing support. We’re planning to release this later in 5.2 cycle. For now
the build is based on a recent 5.2f2 build with additional fixes from 5.2f3.
Download link:
http://beta.unity3d.com/download/747ba33418da/download.html
For actual ODR and/or App Slicing usage you can check or use either an
AssetBundle demo located here:
https://bitbucket.org/Unity-Technologies/assetbundledemo
or see an example bellow. Note that you need Xcode 7 and iOS 9 for this.
Changes since last build
- App Slicing support
- ODR and sliced resources are stored in asset catalogs. At the moment the only
API that can retrieve resources in asset catalogs is AssetBundle.CreateFromFile(). - ODR collection API has been changed to support sliced resources in a
consistent way
AssetBundle demo
Explanation:
The project contains several demo assets and scenes for ODR and App Slicing demonstration purposes. All assets are put into asset bundles and some of the asset bundles are loaded via ODR on request. Asset bundles are built via separate menu option. When doing an iOS build, the asset bundles are included into Xcode project and configured to be included into Xcode asset catalog. Depending on settings, the asset bundles are configured to be included into the initial app download, or be loaded on demand. In addition, several asset bundles may be assigned to the same path; whichever of them is included into the initial app download, or be loaded on demand depends on device requirements configuration and target device specifics.
Preparation steps:
- Extract assetbundle-demo-odr.zip and open demo folder in Unity.
- Switch platform to iOS
- Build asset bundles through menu option “AssetBundles->Build
AssetBundles”. - Set “Enable on demand resources” iOS player setting.
Testing ODR:
- Make the build for AssetLoader or SceneLoader scene. Make sure it’s not a development player.
- Open and run built Xcode project.
- If everything goes right, in the log you should see messages like:
[AssetBundleManager] Requesting bundle iOS through ODR
[AssetBundleManager] Requesting bundle cube.unity3d through ODR
[AssetBundleManager] Requesting bundle material.unity3d through ODR
[AssetBundleManager] Requesting bundle texture.unity3d through ODR - On a screen you should see a pink texture with Unity logo in the case of AssetLoader scene and a circle in the case of SceneLoader scene.
Testing App slicing:
- Make the build for VariantsLoader scene. Make sure it’s not a development player.
- Open and run built Xcode project.
- If everything goes right, in the log you should see messages like (note that
ODR is used for some resources)
[AssetBundleManager] Requesting bundle iOS through ODR
[AssetBundleManager] Loading variant-scene from variants/variant-scene.unity3d bundle
[AssetBundleManager] Loading Asset Bundle : variants/variant-scene.unity3d
[AssetBundleManager] Requesting bundle variants/variant-scene.unity3d through ODR
[AssetBundleManager] Requesting bundle variants/myassets through asset catalog
Finished loading scene variant-scene in 0.3671752 seconds
[AssetBundleManager] variants/variant-scene.unity3d has been unloaded successfully
[AssetBundleManager] variants/myassets has been unloaded
- On iPad, “Hi res” text should be visible
- On iPhone, “Low res” text should be visible
Troubleshooting:
-
If you get errors like this “This AssetBundle was not created with
UncompressedAssetBundle flag, expected id ‘UnityRaw’, got ‘UnityWeb’”
then make sure that platform is set to iOS, delete AssetBundles/iOS folder in
demo project folder and rebuild the bundles. -
If you want to run scenes in the editor, then first you need to launch
asset bundle server through menu option “AssetBundles->Launch
AssetBundleServer”.
See LoadAssets.cs for an example of how asset bundles are retrieved through
ODR. If you’d like to use the same infrastructure for your project, then
just copy demo/Assets/AssetBundleManager folder to your project.
A quick start guide for ODR
To Use ODR, first enable ODR feature in iOS player settings: “Enable on
demand resources”, and then add a callback to report a list of ODR
resources you want to include in the build.
In the asset bundle demo, the ODR tags attached to reported on demand resources
must match resource names. There’s no such limitation for custom user resources.
At the moment only asset bundles may be actually read from downloaded asset
catalogs.
To set up ODR reporting callback, add a new editor script. Script example:
using UnityEditor.iOS;
#if ENABLE_IOS_ON_DEMAND_RESOURCES
public class BuildResources
{
[InitializeOnLoadMethod]
static void SetupResourcesBuild()
{
UnityEditor.iOS.BuildPipeline.collectResources += CollectResources;
}
static UnityEditor.iOS.Resource[] CollectResources()
{
return new Resource[]
{
new Resource("iOS", "AssetBundles/iOS/iOS").AddOnDemandResourceTags("iOS"),
new Resource("cube.unity3d", "AssetBundles/iOS/cube.unity3d").AddOnDemandResourceTags("cube.unity3d"),
new Resource("resource", "path/to/resource.file").AddOnDemandResourceTags("resource_tag"),
};
}
}
#endif
An example of actually using those resources:
using UnityEngine.iOS;
// Coroutine that can be asynchronously executed with
StartCoroutine(LoadAsset("asset.data"));
public static IEnumerator LoadAsset(string resourceName)
{
// Create the request
var request = OnDemandResources.PreloadAsync(new string[] { "resource_tag"
});
// Wait until request is completed
yield return request;
// Check for errors
if (request.error != null)
throw new Exception("ODR request failed: " + request.error);
// Get path to the resource and use it. Note that at the moment the only API
// that can load ODR or sliced resources is AssetBundle.CreateFromFile()
var path = "res://" + resourceName;
var bundle = AssetBundle.CreateFromFile(path);
// Call Dispose() when resource is no longer needed. This will release a
pin on ODR resource.
request.Dispose();
}
A quick start guide for App Slicing
To use App Slicing add a callback to report a list of sliced resources you
want to include in the build. For each resource, several variants may be added
each of which has corresponding device requirements. For each resource, the
App Store will select the best variant that matches the device requirements and
include it into the application bundle.
Device requirements can be either specified explicitly, or via “variant names”
which are later configured in the player settings UI. Multiple resource variants
from different resources may correspond to a single variant name, to which a
single device requirements definition may be attached.
At the moment app slicing is implemented only with asset bundle variants in
mind. App slicing variant names correspond directly to the asset bundle
variant name. When reporting sliced resources, the variant name must match the
extension of the asset bundle file (i.e. the asset bundle variant name).
At the moment, ODR support also needs to be enabled in player settings.
Sliced resources may also be downloaded on-demand. The process of loading them
is the same as simple on-demand resources. To set up sliced resource also as
on-demand resource simply add ODR tags via AddOnDemandResourceTags call.
To set up app slicing callback, add a new editor script. Script example:
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()
{
var iPadRequirement = new iOSDeviceRequirement();
iPadRequirement.values.Add("idiom", "ipad");
return new Resource[]
{
// simple sliced resource
new Resource("variants/myassets").BindVariant("AssetBundles/iOS/variants/myassets.hd", "hd")
.BindVariant("AssetBundles/iOS/variants/myassets.sd", "sd"),
// sliced ODR resource
new Resource("variants/odrassets").BindVariant("AssetBundles/iOS/variants/odrassets.hd", "hd")
.BindVariant("AssetBundles/iOS/variants/odrassets.sd", "sd")
.AddOnDemandResourceTags("variants/odrassets")
// custom device requirements
new Resource("variants/odrassets2").BindVariant("AssetBundles/iOS/variants/odrassets2.hd", iPadRequirement)
.BindVariant("AssetBundles/iOS/variants/odrassets2.sd", new iOSDeviceRequirement())
.AddOnDemandResourceTags("variants/odrassets2")
};
}
}
#endif
To use sliced resources, prepend “res://” to the name of the resource and use
it in Unity APIs that accept filesystem paths. At the moment the only API that
can load such paths is AssetBundle.CreateFromFile().
Sliced on demand resources may be downloaded through ODR. The process to load such
resources is the same as regular ODR resources.
EDIT: Updated link to a newer demo project, improved explanation.
EDIT2: Updated link to a open-source demo project repository.