Log in or register to post comments

Unable to augment AssetBundles using CloudReco

March 24, 2015 - 5:24am #1

Hello,

I'm new using both Unity and Vuforia and I'm trying to load a AssetBundle from a server into my AR app.

I've read the FAQ https://developer.vuforia.com/forum/faq/unity-how-can-i-augment-my-image-target-model and my code is basically a copy&paste of that but I can't get my model augmented.

First I track the target that I previously upload to my targetManager and I get the metadata which contains the URL of the asset (www.musguide.net/admin/AR/Unity/CubeTest.unity3d) Then I call the function StartCoroutine(string assetURL) using the URL as a parameter. The download seems OK but when I try to instantiate the asset nothing happens and when I print the mBundleInstance GameObject is always null.

I tried to instantiate a prefab using Resources.Load("prefabName") and it works like a charm but I can't do it with the AssetBundle.

I show you the code:

 

using UnityEngine;
using System;
using System.Collections;
using Vuforia;


public class AssetBundleAugmenter : MonoBehaviour, ITrackableEventHandler {
	public string AssetName;

	private WWW www;
	
	private GameObject mBundleInstance = null;
	
	private TrackableBehaviour mTrackableBehaviour;
	
	private bool mAttached = false;

	//This function is called when the target is tracked
	//and the metadata (which contains the URL) is passed as a parameter
	public void StartCoroutine(string assetURL) {  
		StartCoroutine(DownloadAndCache(assetURL));
		mTrackableBehaviour = GetComponent<TrackableBehaviour>();
		if (mTrackableBehaviour) {
			mTrackableBehaviour.RegisterTrackableEventHandler(this);
		}
	}
	
	IEnumerator DownloadAndCache(string assetURL) {
		while(!Caching.ready)
			yield return null;

		string bundleURL = assetURL;
		//Just to check if the URL is correct
		Debug.Log ("bundleURL= " + bundleURL);
		
		using (www = WWW .LoadFromCacheOrDownload(bundleURL, 1)) {
			yield return www;
			if (www .error != null)
				throw new UnityException("WWW Download had an error: " + www .error);
			Debug.Log("IS DONE: "+www.isDone);
			AssetBundle bundle = www.assetBundle;
			//THE PROBLEM IS HEEEEREEE
			Debug.Log ("AssetName: "+AssetName);
			if (AssetName == "")
				mBundleInstance =(GameObject)Instantiate(bundle.mainAsset) as GameObject;
			else
				mBundleInstance= Instantiate(bundle.Load(AssetName)) as GameObject;
			bundle.Unload(false);
			//mBundleInstance is always null

		}
	}
	
	public void OnTrackableStateChanged(

		TrackableBehaviour.Status previousStatus,
		TrackableBehaviour.Status newStatus) {
		Debug.Log (newStatus);
		Debug.Log ("IF " + (newStatus == TrackableBehaviour.Status.DETECTED ||
						newStatus == TrackableBehaviour.Status.TRACKED ||
						newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED));
		if (newStatus == TrackableBehaviour.Status.DETECTED ||
		    newStatus == TrackableBehaviour.Status.TRACKED ||
		    newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) {
			Debug.Log (!mAttached);
			Debug.Log (mBundleInstance); //ALWAYS NULL
			if (!mAttached && mBundleInstance) {
				Debug.Log ("Attaching");
				// if bundle has been loaded, let's attach it to this trackable
				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;
			}
		}
	}
}

 

Unable to augment AssetBundles using CloudReco

March 30, 2015 - 8:32am #4

No errors when you fetch the asset bundle? So you set up a crossdomain.xml script on your server?

Asset bundle was exported for the platform you're importing on? (They're platform specific, need one for android, another for ios)

 

After you download it, can you debug out the www.mainAsset.name or something to see if it's actually got the asset bundle to instantiate from?

 

You also have to increment the version parameter to force it to download a new version. But sounds like it's not even getting the first version?

 

If you run in play mode, do you get any error in the console regarding the server from the WWW request?

 

Ideally I'd separate out the asset bundle logic from tracking in another script with less to go wrong. Try simply downloading the assetbundle with www, once that works, instantiate mainAsset from it, if that works, add it to your tracking script. 

 

Another thing when you get it instantiating, sometimes the scale is microscopic and it looks like it's not there. You said the instance is null so not this problem but just something to look for. I iterate in the editor so I can see that the objects are being added to the hierarchy, then when it's working add the platform specific stuff.

My workaround for the microscopic scale was to make a script I add to the object before making an asset bundle with [ExecuteInEditMode], add variables for localpos, localrot, localscale, and when I instantiate the asset I fetch that script, set it's localposition/rotation/scale to those stored in the script, and it works perfectly. 

 

Unable to augment AssetBundles using CloudReco

March 30, 2015 - 3:12am #3

Hellooooooooooooooo

Unable to augment AssetBundles using CloudReco

March 25, 2015 - 1:35am #2

Anyone?! Please I'm running out of time with this project!

Log in or register to post comments