Loads and tracks asset bundles at runtime.
One instance manages one IBundleManifest worth of content. It is safe to support multiple IBundleManifests with additional instances.
Getting started:
We offer three levels of request API:
It is possible to swap out IBundleManifests while retaining the same BundleManager. This is useful for changing datasets while your app is running.
All bundle names are passed in as "sanitized", meaning no Variants (bundleName.variant
) or hashes (bundleName_123123098
). It is the manifest's job to translate those to the current Variant and hash as needed.
When naming bundles, avoid using periods (confuses Variant system). Adding slashes to bundle names is a good way of organizing bundles, creates 'folders' of them. Be careful however of double slashes together (//), that messes with the menu displays. Do not use underscores in variant names either.
When errors occur in a Load/Preload, they must still be balanced by UnloadAssetBundle so that this system knows the caller has seen the error.
Reminder: you can add built bundles to your Project's StreaminAssets folder to embed a set of them in your shipped binary. They are always automatically searched when loading bundles. This is a great way to "pre-download" bundle content while still maintaining the ability to override them in production.
Mid-Level API | |
ILoadAsset | LoadAsset (string assetBundleName, string assetName, System.Type type, Priority priority=Priority.Immediate) |
ILoadAsset | LoadAsset< T > (string assetBundleName, string assetName, Priority priority=Priority.Immediate) |
IOperation | LoadLevel (string assetBundleName, string levelName, bool isAdditive, Priority priority=Priority.Immediate) |
Low-level API | |
ILoadBundle | LoadBundle (string assetBundleName, Priority priority=Priority.WifiOnly) |
IGetVersion | GetBundleVersion (string assetBundleName) |
IOperation | PreloadBundle (string assetBundleName, Priority priority=Priority.WifiOnly) |
void | UnloadBundle (string assetBundleName) |
void | UnloadUnusedBundles () |
Coroutine | UnloadUnusedAssets () |
Tools for IManifestLoaders | |
Coroutine | LoadManifest (string absolutePath, int version, IOperation loadOp) |
static IEnumerator | LoadManifest (BundleManager manager, string absolutePath, int version, int retries, float retryDelay, IOperation loadOp) |
static bool | IsNonRetryableErrorFromWWW (WWW www) |
static int | ParseErrorCodeFromWWW (WWW www) |
Debugging | |
static ILogger | Logger |
System.Text.StringBuilder | DumpState (System.Text.StringBuilder dest=null) |
AssetBundle[] | GetLoadedBundles () |
Classes | |
class | ManagerSettings |
Public Types | |
enum | Priority { WifiOnly, Low, Immediate } |
enum | UnloadMode { Immediate, Delayed, AutomaticDelayed } |
enum |
BundleState { Waiting, SelfNetworking, ChildrenNetworking, SelfOpening, ChildrenOpening, DoingCallbacks, Complete } |
Public Member Functions | |
delegate void | DownloadCompleteProc (string key, float[] stats) |
IOperation | BustCacheIfNeeded (string prefKey, int currentVersion) |
void | ReworkShaders () |
Static Public Member Functions | |
static string | SanitizeBundleName (string name) |
static string | GetUrlPath (string url) |
static string | GetPlatformForAssetBundles (RuntimePlatform platform) |
static string | GetPlatformForAssetBundles () |
static string | GetLibCompiledTarget () |
Public Attributes | |
ManagerSettings | Settings |
Properties | |
uint |
AmountToProcess [get]
|
uint |
AmountProcessed [get]
|
IBundleManifest |
Manifest [get, set]
|
static bool |
SimulateAssetBundleInEditorPref [get, set]
|
static bool |
SimulateAssetBundleInEditor [get]
|
Events | |
DownloadCompleteProc | DownloadComplete |
Action | DeletedEvent |
|
strong |
Each state a bundle goes through in the loading process.
Corresponds with values DownloadCompleteProc returns. Do not assume this is set in stone!
|
strong |
Each of these represents a separate download queue.
|
strong |
How to deal with unloading bundles when the last reference is unloaded
|
inline |
Bust local cache if passed version number doesn't match what's in PlayerPrefs.
Should be done once on startup, before anything is loaded.
We had a situation where the asset bundle data was junk on live clients. We fixed what was on the server just fine, but the file hashes that Unity uses only includes the source assets, not the resulting bundle. This means the clients would always see the damaged bundles in the cache and never reach out for updated ones.
By increasing the following constant(preferably for a single platform only), we will dump the local cache and force-get an all new bundle set. For a new device, there will be no local cache data anyway, so nothing is lost/wasted.
delegate void DownloadCompleteProc | ( | string | key, |
float[] | stats | ||
) |
Record stats of a completed (or failed) download.
key | typically the bundle id |
stats | each entry is how long in seconds that the download was in each BundleState |
|
inline |
Snapshot the state for debugging purposes
|
inline |
Get a string representing the version of the bundle.
Avoid using/relying on this!!! Useful for comparing that a previously loaded version is same vs. different.
assetBundleName | Sanitized bundle name. |
|
inlinestatic |
What platform was this library built for?
This is for debugging purposes only!
|
inline |
purely for debugging only
|
inlinestatic |
Make consistent strings between runtime and build-time for what the "current platform" is.
In the Editor, will automatically get the current target platform for you.
|
inlinestatic |
Make consistent strings between runtime and build-time for what the "current platform" is.
platform | As gotten from Application.platform
|
|
inlinestatic |
Get the path of the URL including the trailing slash
url | http://this/that/other/file.what |
|
inlinestatic |
Is this an error that there is no point in retrying?
On a 403 (Amazon s3 uses those) or FnF, drop out immediately.
|
inline |
Load asset from the given assetBundle.
Remember to UnloadBundle when you are done with the content!
assetBundleName | Sanitized bundle name. |
assetName | Name of asset, without extension. |
type | Expected asset type, ex: GameObject, Texture2d. Cannot directly request Components! |
priority | How quickly is this needed? |
|
inline |
Load asset from the given assetBundle.
Remember to UnloadBundle when you are done with the content!
assetBundleName | Sanitized bundle name. |
assetName | Name of asset, without extension. |
priority | How quickly is this needed? |
|
inline |
Load a bundle directly.
Also preloads dependent bundles.
Remember to UnloadBundle when you are done with the content!
assetBundleName | Sanitized bundle name. |
priority | How quickly is this needed? |
|
inline |
Load level from the given assetBundle.
Remember to UnloadBundle when you are done with the content!
assetBundleName | Sanitized bundle name. |
levelName | Name of level. |
priority | How quickly is this needed? |
isAdditive | Once loaded, should it replace the current scene immediately? Otherwise it is added to the current scene. |
|
inlinestatic |
Utility to load a directory object.
Absolutely intentional that it's static, as it should not rely on any current manager state.
loadOp | Caller must pass in a response object (typically ILoadAssetFullAsync) already prepared to accept the manifest object |
absolutePath | Path passed to WWW as 'source url' |
version | Cache version. Using a different value here forces WWW to ignore a locally cached version of the data. |
manager | Target manager, only used for the loadOp |
retries | How many times to attempt download. Minimum 1. |
retryDelay | How long to wait between retries. Special value<0 is double each retry. |
|
inline |
Load a manifest, using this BundleManager to do it.
loadOp | Caller must pass in a response object (typically ILoadAssetFullAsync) already prepared to accept the manifest object |
absolutePath | Path passed to WWW as 'source url' |
version | Cache version. Using a different value here forces WWW to ignore a locally cached version of the data. |
|
inlinestatic |
Work out what HTTP error was returned, if any.
Only really valid after WWW completes the request.
www |
|
inline |
Preloads the bundle without loading the content
Also preloads dependent bundles.
Remember to UnloadBundle when you are done waiting for the result!
assetBundleName | Sanitized bundle name. |
priority | How quickly is this needed? |
|
inline |
in Editor only, after each item loads, check for new shaders.
Deal with the fact that shaders are embedded in asset bundles and will use fallbacks in the Editor. Like an iOS asset bundle will play fine on device, but the Editor will draw everything in pink "no shader" colors. This bit of code will replace each shader reference with the Editor versions.
This is done automatically for you when bundles are loaded, but you can call this to do an extra trigger. Safe to call outside the Editor, it's a NOP.
|
inlinestatic |
Take a bundle name and remove bad things like underscores.
name |
|
inline |
Unload a bundle, and all its dependencies.
assetBundleName | Sanitized bundle name. |
|
inline |
Wrapper for Resources.UnloadUnusedAssets()
Never call Unity's version directly, use this. This makes sure that no loads are going on at the same time as an unload. What would happen is a bunch of errors about scripts missing on gameObjects, which is not actually true.
|
inline |
Anything unused, unload it now.
Typically only needed in Delayed mode.
|
static |
Assign a logging handler
ManagerSettings Settings |
Do not change these settings after your first SetSourceURL().
|
get |
How much data has been downloaded since startup? Perfect for progress bars.
How much has completed. How much is planned is in AmountToProcess.
|
get |
How much data we're going to download.
Perfect for progress bars.
How much we need to process. Eventually AmountProcessed will match this.
|
getset |
The current directory
|
staticget |
Flag to indicate if we want to simulate assetBundles in Editor without actually building them.
To prevent confusion, we don't allow write mode. Use SimulateAssetBundleInEditorPref.
|
staticgetset |
Pref value for simulation in Editor
This is what you write to change the value. Use SimulateAssetBundleInEditor for indicating real state.
Action DeletedEvent |
Get notified when this BundleManager is deleted by Unity