Log in or register to post comments

Cloud Reco with asset bundle and metadata

November 28, 2015 - 4:36am #1

Hi there!

I am using unity package 5.0.6 with Unity 5.2. I want to download 3D asset bundle from my dropbox and show it on my image target using metadata. but i getting this error message

"NullReferenceException: Object reference not set to an instance of an object Vuforia.SimpleCloudRecoEventHandler+<DownloadAndCache>c__Iterator1.MoveNext () (at Assets/Vuforia/Scripts/SimpleCloudRecoEventHandler.cs:155)"  

Any solution for this problem??

here is my SimpleCloudRecoEventHandler.cs code

using System;
using UnityEngine;
using System.Collections;

namespace Vuforia
{
	public class SimpleCloudRecoEventHandler : MonoBehaviour, ICloudRecoEventHandler
	{
		#region PRIVATE_MEMBER_VARIABLES

		private CloudRecoBehaviour mCloudRecoBehaviour;

		private ObjectTracker mObjectTracker;

		private bool mIsScanning = false;

		private string mTargetMetadata = "";
		
		private GameObject f1, f2;

		public string url;

		public string AssetName;

		private GameObject mBundleInstance = null;

		public int Version;

		public ImageTargetBehaviour ImageTargetTemplate;
		
		#endregion
		
		#region UNTIY_MONOBEHAVIOUR_METHODS
		
		/// <summary>
		/// register for events at the CloudRecoBehaviour
		/// </summary>
		void Start()
		{
			// register this event handler at the cloud reco behaviour
			CloudRecoBehaviour cloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
			if (cloudRecoBehaviour)
			{
				cloudRecoBehaviour.RegisterEventHandler(this);
			}
			
			// remember cloudRecoBehaviour for later
			mCloudRecoBehaviour = cloudRecoBehaviour;
			
			/*
	        teapot = GameObject.Find("teapot");
	        dragon = GameObject.Find("Dragon");
	         
	        Hide( teapot );
	        Hide( dragon );
	        */
			
			//teapot.SetActive( false );
			//dragon.SetActive( false );
			
		}
		
		#endregion // UNTIY_MONOBEHAVIOUR_METHODS
		
		
		#region ICloudRecoEventHandler_IMPLEMENTATION
		
		/// <summary>
		/// called when TargetFinder has been initialized successfully
		/// </summary>
		public void OnInitialized()
		{
			// get a reference to the Image Tracker, remember it
			mObjectTracker = (ObjectTracker)TrackerManager.Instance.GetTracker<ObjectTracker>();
		}
		
		/// <summary>
		/// visualize initialization errors
		/// </summary>
		public void OnInitError(TargetFinder.InitState initError)
		{
		}
		
		/// <summary>
		/// visualize update errors
		/// </summary>
		public void OnUpdateError(TargetFinder.UpdateState updateError)
		{
		}
		
		/// <summary>
		/// when we start scanning, unregister Trackable from the ImageTargetTemplate, then delete all trackables
		/// </summary>
		public void OnStateChanged(bool scanning)
		{
		}
		
		/// <summary>
		/// Handles new search results
		/// </summary>
		/// <param name="targetSearchResult"></param>
		public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
		{
			url = targetSearchResult.MetaData; 
			/// 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(DownloadAndCache());
			}
			
			ImageTargetBehaviour imageTargetBehaviour = (ImageTargetBehaviour)mObjectTracker.TargetFinder.EnableTracking(targetSearchResult, ImageTargetTemplate.gameObject);
			
			if (imageTargetBehaviour != null)
			{
				mCloudRecoBehaviour.CloudRecoEnabled = false;
				//mContentManager.TargetCreated(targetSearchResult.MetaData);
			}
			
		}

		IEnumerator DownloadAndCache() {
			while(!Caching.ready)
				yield return null;
			
			// example URL of file on PC filesystem (Windows)
			// string bundleURL = "file:///D:/Unity/AssetBundles/MyAssetBundle.unity3d";
			
			// example URL of file on Android device SD-card
			
			
			using (WWW www = WWW .LoadFromCacheOrDownload(url, Version)) {
				yield return www;
				
				if (www .error != null)
					throw new UnityException("WWW Download had an error: " + www .error);
				
				AssetBundle bundle = www .assetBundle;
				if (AssetName == "") {
					mBundleInstance = Instantiate (bundle.mainAsset) as GameObject;
				}
				else {
					mBundleInstance = Instantiate(bundle.LoadAsset(AssetName)) as GameObject;
				}
			}
		}
		
		
		#endregion // ICloudRecoEventHandler_IMPLEMENTATION
		
		private void Hide( GameObject model ){
			
			Debug.Log( "Hiding "+ model.name );
			
			Renderer[] rendererComponents = model.GetComponentsInChildren<Renderer>();
			Collider[] colliderComponents = model.GetComponentsInChildren<Collider>();
			
			// Disable rendering:
			foreach (Renderer component in rendererComponents)
			{
				component.enabled = false;
			}
			
			// Disable colliders:
			foreach (Collider component in colliderComponents)
			{
				component.enabled = false;
			}
			
			//Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
		}
		

		/*void OnGUI() {
			// Display current 'scanning' status
			GUI.Box (new Rect(100,100,200,50), mIsScanning ? "Scanning" : "Not scanning");
			// Display metadata of latest detected cloud-target
			GUI.Box (new Rect(100,200,200,50), "Metadata: " + mTargetMetadata);
			// If not scanning, show button
			// so that user can restart cloud scanning
			if (!mIsScanning) {
				if (GUI.Button(new Rect(100,300,200,50), "Restart Scanning")) {
					// Restart TargetFinder
					mCloudRecoBehaviour.CloudRecoEnabled = true;
				}
			}
		}*/
		
	}
}

I also attach my error screen.

Thanks

 

AttachmentSize
Image icon error.JPG148.28 KB
Log in or register to post comments