Pages

Wednesday, 20 January 2016

Downloading asset bundles and loading scene from it with Unity 5.x


In this post we will create an asset bundle with simple scene and then we will load this from a different scene.

Creating the Asset Bundle

Let myscene be the name of the scene which we want to put in to an asset bundle. To create an asset bundle click on to the scene and  at the bottom of your inspector window give a name for your AssetBundle.


We will need a script to export our scene as an asset bundle. Use the following editor script code export the asset bundles.
using UnityEditor;

public class CreateAsset
{
    [MenuItem ("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles ()
    {
        // Assets/AssetBundles is the out put path
        BuildPipeline.BuildAssetBundles ("Assets/AssetBundles");
    }
}

This script requires the folder Assets/AssetBundles to be present. As you might have guessed, this script should be kept in Assets/Editor folder.

Now create the asset bundle using the menu item Assets->Build AssetBundles

Please see http://docs.unity3d.com/Manual/BuildingAssetBundles.html for more information on creating asset bundles.

Downloading the asset bundle

Use the following code snippet to download the asset bundle and then to load the scene from it
string ASSET_URL = "your asset url";

var download = WWW.LoadFromCacheOrDownload(ASSET_URL, 0);

yield return download;

if (download.error != null) {
     Debug.LogError("Error in downloading the asset bundles : " + download.error); 
    yield break;
}

AssetBundle bundle = download.assetBundle;
bundle.LoadAllAssets();

Application.LoadLevel ("myscene");

1 comment: