Log in or register to post comments

AssetBundleAugmenter Problem

February 18, 2014 - 7:43am #1

I am trying to integrate it to cloud example. In order to get bundles for each image target from cloud. Here is the code from this link https://developer.vuforia.com/forum/faq/unity-how-can-i-augment-my-image-target-model

using System;
using UnityEngine;
using System.Collections;

public class AssetBundleAugmenter : MonoBehaviour, ITrackableEventHandler {
	
	public string AssetName;
	public int Version;
	private GameObject mBundleInstance = null;
	private string bundleLink;
	private TrackableBehaviour mTrackableBehaviour;
	
	private bool mAttached = false;
	
	void Start()
	{
		StartCoroutine(DownloadAndCache());
		
		mTrackableBehaviour = GetComponent<TrackableBehaviour>();
		if (mTrackableBehaviour)
		{
			mTrackableBehaviour.RegisterTrackableEventHandler(this);
		}
	}
	
	
	// Update is called once per frame
	IEnumerator DownloadAndCache() {
		while(!Caching.ready)
			yield return null;
                // to get the metadata as bundleURL
		if (newStatus == TrackableBehaviour.Status.DETECTED ||
		    newStatus == TrackableBehaviour.Status.TRACKED)
			bundleLink = TargetFinder.targetSearchResult.MetaData;
		// example URL of file on PC filesystem (Windows)
		//string bundleURL = "file:///D:/Unity/AssetBundles/MyAssetBundle.unity3d";
		string bundleURL = bundleLink; 

		// example URL of file on Android device SD-card


		using (WWW www = WWW.LoadFromCacheOrDownload(bundleURL, Version)) {
			yield return www;
			
			if (www.error != null)
				throw new Exception("WWW download had an error:" + www.error);
			
			AssetBundle bundle = www.assetBundle;
			if (AssetName == "") {
				mBundleInstance = Instantiate (bundle.mainAsset) as GameObject;
			}
			else {
				mBundleInstance = Instantiate(bundle.Load (AssetName)) as GameObject;
			}
		}
	}
	
	public void OnTrackableStateChanged(
		TrackableBehaviour.Status previousStatus,
		TrackableBehaviour.Status newStatus)
	{
		if (newStatus == TrackableBehaviour.Status.DETECTED ||
		    newStatus == TrackableBehaviour.Status.TRACKED)
		{
			if (!mAttached && mBundleInstance) {
				// 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;
			}
		}
	}
}

But this code gives an error as below

Assets/Qualcomm Augmented Reality/Scripts/AssetBundleAugmenter.cs(32,21): error CS0103: The name `newStatus' does not exist in the current context
Assets/Qualcomm Augmented Reality/Scripts/AssetBundleAugmenter.cs(34,51): error CS0117: `TargetFinder' does not contain a definition for `targetSearchResult'

Should I do this metadata part in CloudRecoEventHandler.cs? I simply need to try to get each metadata info and use it as assetbundle link and downlad the asset bundle to corresponding image target.

Thanks in advance!

 

AssetBundleAugmenter Problem

February 19, 2014 - 7:46am #10

Thanks but when I pulled that out there is still the same error. I am uploading a screenshot, it gives error on the paranthesis. I look up the problem and it is caused sometimes because of a copy paste problem (character encoding) but I deleted and written all of the raw myself and still the same.

AttachmentSize
Image icon Screen Shot 2014-02-19 at 17.44.04.png195.23 KB

AssetBundleAugmenter Problem

February 19, 2014 - 7:42am #9

Ok. So, yes, first thing to do is to pull the definition of this method:

IEnumerator DownloadAndCache() { ... part

out of the OnNewSearchResult.

 

 

 

AssetBundleAugmenter Problem

February 19, 2014 - 7:40am #8

Actually on my code there isnt html code, it happened when I paste it here. But you are right. Actually I am not much of a coder but we have a short of developer so I need to do some stuff and it is really painful to correct mistakes.

AssetBundleAugmenter Problem

February 19, 2014 - 7:36am #7

The code contains several syntax errors, such as html notations inside it (probably due to copy-pasting formatted text from the forum to the code and viceversa);

also, the DownloadAndCache must be placed outside the OnNewSearchResult(), and called from there...

May I suggest to learn a bit about C# programming ? This would really help to boost your development.

 

AssetBundleAugmenter Problem

February 19, 2014 - 7:30am #6

I have been trying solve getting metadata problem but unity constantly gives this error and I couldn't fix it.

Assets/Scripts/CloudRecoScripts/CloudRecoEventHandler.cs(159,53): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='

Here is my OnNewSearchResult method:

public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
        // This code demonstrates how to reuse an ImageTargetBehaviour for new search results and modifying it according to the metadata
        // Depending on your application, it can make more sense to duplicate the ImageTargetBehaviour using Instantiate(), 
        // or to create a new ImageTargetBehaviour for each new result

        // Vuforia will return a new object with the right script automatically if you use
        // TargetFinder.EnableTracking(TargetSearchResult result, string gameObjectName)
        

        //Check if the metadata isn't null
        if(targetSearchResult.MetaData == null)
        {
            return;
        }

		if(targetSearchResult.MetaData != null)
		{
			bundleLink = targetSearchResult.MetaData;
		}

        // enable the new result with the same ImageTargetBehaviour:
        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, mParentOfImageTargetTemplate);

        if (imageTargetBehaviour != null)
        {
            // stop the target finder
            mCloudRecoBehaviour.CloudRecoEnabled = false;
            
            // Download the unitypackage from the link from metadat
			IEnumerator DownloadAndCache() { //where the error is
				while(!Caching.ready)
					yield return null;
				
				// example URL of file on Android device SD-card
				
				
				using (WWW www = WWW.LoadFromCacheOrDownload(bundleLink, 1)) {
					yield return www;
					
					if (www.error != null)
						throw new Exception("WWW download had an error:" + www.error);
					
					AssetBundle bundle = www.assetBundle;
					if (AssetName == "") {
						mBundleInstance = Instantiate (bundle.mainAsset) as GameObject;
					}
					else {
						mBundleInstance = Instantiate(bundle.Load (AssetName)) as GameObject;
					}
				}
			}
    }

 

AssetBundleAugmenter Problem

February 19, 2014 - 1:18am #5

Thanks Alessandro I wil give it a try.

AssetBundleAugmenter Problem

February 19, 2014 - 12:54am #4

There are probably many ways of doing this; if the bundleURL of the AssetBundle depends on the Metadata of your Cloud Target (i.e. if you have stored the URL string in the metadata associated to a cloud target and you want to reatrieve it from there), then the best would be to trigger the DownloadAndCache from the CloudRecoEventHandler script.

Then, once your AssetBundle is Instantiated, you could make it available to another script, such as the TrackableEventHandler for instance, and there you could just control its visibility (ON/OFF) and attach it (parent it) to the target if it is available for the first time.

 

AssetBundleAugmenter Problem

February 19, 2014 - 12:02am #3

Thanks Alessandro! Now I can see that dummy mistake :) 

I have one other question. First sorry for silly questions I am not so much of a coder here. Should I also download the bundle in CloudRecoEventHandler or can simply get the metadata in CloudRecoEventHandler and use it as bundleLink in this script? Which one is possible or more convenient?

AssetBundleAugmenter Problem

February 18, 2014 - 8:15am #2

First, you have put this code:

if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED)

in the DownloadAndCache method, but as you can see, the newStatus variable is not defined in such method (it is instead defined below in the OnTrackableStateChanged method...

so, this is a simple C# programming error.

Second:

the metadata should be retrieved from the CloudRecoEventHandler, yes.

 

Log in or register to post comments