Package Augmentations in Unity AssetBundles

This article describes how to augment an Image Target with a custom 3D model that was instantiated from an AssetBundle at run time, upon target detection, using the Vuforia Unity extension.

For more general information about Unity Asset Bundles, see the AssetBundle guides on the Unity website:

NOTE: For accurate and up-to-date information, always consult the Unity website (see links above), since the license requirements can change over time.

AssetBundle Overview

Instantiate AssetBundles that are stored on a server or other directories. In our example, we assume that you previously uploaded the AssetBundle file (for example, MyAndroidAssetBundle.unity3d) to a server hosted by you.

A more typical and useful approach is to store your AssetBundle on a server and then point the AssetBundle URL to that server location, such as http://my.org/assetbundles/MyAndroidAssetBundle.unity3d.

AssetBundles can be built for different target platforms, such as Windows, Android, and iOS. Therefore, if you build the AssetBundle by yourself, be sure to select the appropriate BuildTarget option.

AssetBundles built with a previous Unity version may not be compatible with newer Unity versions. For more details, see:

Instructions

Assume that at run time we want to dynamically attach a custom 3D model to a Vuforia target GameObject. We’ll do this with a script that loads the model from an asset bundle and attaches it to an existing target game object in the scene.

  1. Open or create a Unity project, add a target GameObject in the scene.
  2. Create a C# script and copy-paste the code below into the script. Name the script: AssetBundleAugmenter.cs.
  3. Attach the script to your target GameObject, e.g. an Image Target that has been set up in the scene. The script will thenload a model from an AssetBundle at a specific URL and attach it to your target GameObject at run time.
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using Vuforia;

public class AssetBundleAugmenter : MonoBehaviour
{
    public string AssetName;
    private GameObject mBundleInstance = null;
    private ObserverBehaviour mObserverBehaviour;
    private AssetBundle bundle;
    private bool mAttached = false;
    void Start()
    {
        StartCoroutine(DownloadAndCache());
        mObserverBehaviour = GetComponent<ObserverBehaviour>();
        if (mObserverBehaviour)
        {
            mObserverBehaviour.gameObject.AddComponent<DefaultObserverEventHandler>();
        }
    }

    // Update is called once per frame
    IEnumerator DownloadAndCache()
    {
        while (!Caching.ready)
            yield return null;

        // example URL for retrieving bundle from a server
        string bundleURL = "http://localhost/assetbundles/testbundle";
        using (UnityWebRequest mAssetBundle = UnityWebRequestAssetBundle.GetAssetBundle(bundleURL))
        {
            yield return mAssetBundle;
            if (mAssetBundle.error != null)
                throw new UnityException("mAssetBundle download had an error: " + mAssetBundle.error);
            bundle = DownloadHandlerAssetBundle.GetContent(mAssetBundle);
            if (AssetName == "")
            {
                LoadAssetToTarget();
            }
        }
    }

    private void LoadAssetToTarget()
    {
        mBundleInstance = Instantiate(bundle.LoadAsset(AssetName)) as GameObject;
        if (!mAttached && mBundleInstance)
        {
            // If bundle has been loaded, let's attach it to this target
            mBundleInstance.transform.parent = this.transform;
            mBundleInstance.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
            mBundleInstance.transform.localPosition = new Vector3(0.0f, 0.15f, 0.0f);
            mBundleInstance.transform.gameObject.SetActive(true);
            mAttached = true;

        }
    }
}