Log in or register to post comments

AssetBundles on individual targets

January 19, 2014 - 8:02am #1

Hi there,

 

Since the release of the latest Vuforia version, I've been working on a way to load AssetBundles when target is recognized, and letting the MetaData define which AssetBundle, specifically, to fetch. I've been editing the 'Books' example, so below you'll see part of my edited version of the CloudRecoEventHandler.cs included in that example.

I've got it working, roughly, with getting a specific AssetBundle, like this:

 public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
        /// Checking if MetaData's content is null (which, of course, it is not)
        if(targetSearchResult.MetaData == null)
        {
            return;
        }
        /// Confirming that MetaData is indeed not null, and if all's right, start the IEnumerator which downloads an AssetBundle
        if(targetSearchResult.MetaData != null)
        {
            StartCoroutine(DownloadMyBundle(1.0F));
        }

        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, mParentOfImageTargetTemplate) as ImageTargetBehaviour;

        if (imageTargetBehaviour != null)
        {
            mCloudRecoBehaviour.CloudRecoEnabled = false;
            mContentManager.TargetCreated(targetSearchResult.MetaData);
        }
    }

    IEnumerator DownloadMyBundle(float waitTime)
    {
        string url = "http://myServerWhereTheAssetBundlesAreHosted.com/AssetBundle.unity3d";
        yield return new WaitForSeconds(waitTime);
        WWW www = WWW.LoadFromCacheOrDownload(url, 1);
        yield return www;
        AssetBundle bundle = www.assetBundle;
        GameObject MyAssetBundleName = bundle.Load("MyAssetBundleName", typeof(GameObject)) as GameObject;
        Instantiate(MyAssetBundleName);
    }

My problem is, since I want the application to be as small in size as possible, while still have access to a wide range of different targets with individual augmented content, I need to edit the above script in a way that let's me define the url dynamically. My guess is the best way to do this would be to simply use the MetaData-files attached to each target in the database. I tried applying the following small change to my script:

 public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
        /// Checking if MetaData's content is null (which, of course, it is not)
        if(targetSearchResult.MetaData == null)
        {
            return;
        }
        /// Confirming that MetaData is indeed not null, and if all's right, start the IEnumerator which downloads an AssetBundle
        if(targetSearchResult.MetaData != null)
        {
            StartCoroutine(DownloadMyBundle(1.0F));
        }

        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, mParentOfImageTargetTemplate) as ImageTargetBehaviour;

        if (imageTargetBehaviour != null)
        {
            mCloudRecoBehaviour.CloudRecoEnabled = false;
            mContentManager.TargetCreated(targetSearchResult.MetaData);
        }
    }

    IEnumerator DownloadMyBundle(float waitTime)
    {
        string url = targetSearchResult.MetaData;
        yield return new WaitForSeconds(waitTime);
        WWW www = WWW.LoadFromCacheOrDownload(url, 1);
        yield return www;
        AssetBundle bundle = www.assetBundle;
        GameObject MyAssetBundleName = bundle.Load("MyAssetBundleName", typeof(GameObject)) as GameObject;
        Instantiate(MyAssetBundleName);
    }

But I'm just getting an error stating that "targetSearchResult does not exist in the current context"

How do I apply the content of the MetaData-string into the url of my IEnumerator "DownloadMyBundle"?

 

Best regards,

Thomas

AssetBundles on individual targets

February 17, 2014 - 5:37am #6

Hi aure,

My solution to this was to assign the value of MetaData to BundleURL. The MetaData can be a direct link to your package (like http://www.download.my/prefab), and allows you to individually specify, for each target, what content to be downloaded. 
One thing though, ikf you're loading a .prefab from the AssetBundle, you'll need to transfer whatever content inthere you need to a new GameObject, in order to properly child it to the AugmentationObject - this saves you a lot of headaches, as Vuforia will then treat your AssetBundle as it treats any other AR content, in terms of disabling rendering when target is lost etc.

Feel free to message me if you need anything :)

 

AssetBundles on individual targets

February 17, 2014 - 5:25am #5

I have been trying to achieve what Thomas achieved. I can downlad assetbundles and make it appear on trackable with constant link. But I want it to be dynamic. I haev been trying to use metadata but I couldnt get metadata information as link to feed "bundleURL". 

I think what Alessandro has said in his first sentence could help me but I couldnt figure it out how to do it. Can anyone help? How can I get metadata info out of the CloudRecoEventHandler script?

AssetBundles on individual targets

January 21, 2014 - 12:43am #4

You're welcome.

AssetBundles on individual targets

January 20, 2014 - 7:53pm #3

Wow, thanks! I didn't know this existed! 

I'll look into it - thanks, again :)

AssetBundles on individual targets

January 20, 2014 - 1:18am #2

I would mayb try  storing the URL into a member variable of your class, and set its value in the OnNewSearchResult.

Also, we have a small tutorial here about integrating Asset Bundles with image targets, which could be of help:

https://developer.vuforia.com/forum/faq/unity-how-can-i-augment-my-image-target-model

 

 

Log in or register to post comments