Init
It's all or nothing
The Init
function should always be the first async function you call from AssetBundles, and it HAS to succeed if you want to use it. It's main job is to somehow retrieve the platform bundle which contains the AssetBundleManifest
that has all the bundles hashes and dependencies. This step is crucial and without the manifest it is not possible to load the bundles or check if they are outdated.
Let's look at the Init
signature.
public static async Task Init(string sourceUrl, uint manifestVersion = 0, bool forceDownloadManifest = false)
This function can be used in numerous ways. Before talking about them, let's look at each argument one by one.
sourceUrl
A local path or remote url from where to download the bundles if they are not in cache. It's a simple concept that allows for unexpected things like the possibility of giving an empty string if you want to load exclusively from the cache and prevent the user from making web requests (e.g. playing in offline mode).
Example:
- Assets
- StreamingAssets
- Bundles (This is your bundles SourceURL)
- Windows
- Android
- iOS
- etc...
- Bundles (This is your bundles SourceURL)
- StreamingAssets
Here your sourceURL is Path.Combine(Application.streamingAssetsPath, Bundles)
. You could use this as a helper to do the same thing AssetBundlesUtility.GetStreamingPath("Bundles")
.
Internally the plugin will detect the current platform and append its name to your sourceUrl.
If we are on windows, the complete sourceUrl will end with Bundles/Windows/
and it will load the Bundles/Windows/Windows
manifest bundle.
Platforms
In unity, the bundles are built using the BuildTarget enum but when your application is built, you only have access to the RuntimePlatform enum (via Application.platform) which is incompatible with the BuildTarget enum.
AssetBundles unifies that by renaming some of the RuntimePlatform / BuildTarget to use a common naming convention.
The platform (or build target) is automatically detected and made available via the AssetBundles.PlatformName
variable. See below for the naming rules.
switch (Application.platform) {
case RuntimePlatform.IPhonePlayer:
return "iOS";
case RuntimePlatform.WebGLPlayer:
return "WebGL";
case RuntimePlatform.WindowsPlayer:
return "Windows";
case RuntimePlatform.OSXPlayer:
return "OSX";
case RuntimePlatform.LinuxPlayer:
return "Linux";
default:
return Application.platform.ToString();
}
This means that for instance, if you are building bundles for the WindowsStandalone64 target, you should name the platform folder and the manifest bundle in it "Windows" (and not WindowsStandalone or WindowsStandalone64 like the Asset Bundles Browser does).
manifestVersion
If not zero, it will try to load that specific version of the manifest from the cache or download the manifest from your sourceUrl and tag it with this version. Otherwise it will always load the most recent version from the cache.
forceDownloadManifest
The platform bundle is cached on disk like all the other bundles. If you want to make sure that it is downloaded to get the latest manifest information, use true. This is usefull when you want to make sure the user always has the latest version (or if you use the Patch system).
Look at the examples section for a better understanding on how to use these parameters.
Use cases
try {
// From folder in StreamingAssets folder
await AssetBundles.Init(AssetBundlesUtility.GetStreamingPath("Bundles"));
} catch { }
try {
// From an url
await AssetBundles.Init("http://wonderland.eode.studio/AssetBundles/Bundles");
} catch { }
Simulation mode
You can enable the simulation mode from the Wonderland/AssetBundles/Simulation menu. When it is active, all asset load will be made directly using the editor informations. You won't have to build any bundles but we will still check that the asset is indeed in the intended bundle and issue a warning if it is not. Even with this mode enabled, you still have to call the Init
function.
Reset
You have the ability to reset the AssetBundles to change the remote location or simply unload all bundles from memory using the Reset
function. After using this function, it will be needed to call the Init
function before using the AssetBundles class again.
// Optionnaly you can pass true to the function to unload all loaded objects
AssetBundles.Reset();