Log in or register to post comments

How to call Asset Bundle Models after image tracking

September 1, 2013 - 3:52pm #1

Hello,

I have been able to load models at runtime with Asset Bundles in Unity Pro. However, currently my models load right at runtime instead of after image tracking. After image tracking the correct model will lock to the trackable image and rotate normally but do not dissappear after the trackable image is lost. My question is how to only load the model after image tracking? I am loading the asset bundle models into an empty object that is a child of my ImageTarget. On this empty object I attached the following script, if I use a regular object as a child of my imagetarget it does not show up early only the dynamically pulled objects.

Thank you for any help!

 

 

 

 

using System;

using UnityEngine;

using System.Collections;

 

public class CachingLoadExample : MonoBehaviour {

public string BundleURL;

public string AssetName;

public int version;

 

void Start() {

StartCoroutine (DownloadAndCache());

}

 

IEnumerator DownloadAndCache (){

// Wait for the Caching system to be ready

while (!Caching.ready)

yield return null;

 

// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache

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 == "")

Instantiate(bundle.mainAsset);

else

Instantiate(bundle.Load(AssetName));

                // Unload the AssetBundles compressed contents to conserve memory

                bundle.Unload(false);

 

                       // Frees the memory from the web stream

                       www.Dispose();

}

}

}

 

 

How to call Asset Bundle Models after image tracking

June 11, 2019 - 4:46am #13

Hello YamanakaYue

I have some problem with you. do you found to solve this problem?? if you solve the problem can you post your code.

Thanks. i hope you can hlep me

How to call Asset Bundle Models after image tracking

April 30, 2019 - 11:18pm #12

Hello!



I am relatively new to Vuforia and have come across a similar problem to the origin of this post. I am trying to have an object from an AssetBundle (Stored on S3) appear when a target is shown. As it is it loads the image when it sees the target for the first time which is good, except that it doesn't attach to the image target unless I remove the target and then show it again. I know that DavidBeard has responded to almost this exact issue however it is in deprecated language because the post is so old and I'm unsure of most of it.



Thankyou in advance to anyone with advice! I have been trying to rectify this problem for hours now.



My code is as follows:

 

using UnityEngine;

using Vuforia;

using System.Collections;

public class AssetBundleAugmenter : MonoBehaviour, ITrackableEventHandler

{

    public string url;

    public string AssetName;

    public int Version;

    private GameObject mBundleInstance = null;

    private TrackableBehaviour mTrackableBehaviour;

    private bool mAttached = false;

    void Start()

     

{

        mTrackableBehaviour = GetComponent<TrackableBehaviour>();

        if (mTrackableBehaviour)

        {

            mTrackableBehaviour.RegisterTrackableEventHandler(this);

        }

    }

    // Update is called once per frame

    IEnumerator DownloadAndCache()

    {



        while (!Caching.ready)

            yield return null;

            string bundleURL = url;

        using (WWW www = WWW.LoadFromCacheOrDownload(bundleURL, 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;

            }

        }

    }

 

    public void OnTrackableStateChanged(

        TrackableBehaviour.Status previousStatus,

        TrackableBehaviour.Status newStatus)



    {

        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;

        }



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

            newStatus == TrackableBehaviour.Status.TRACKED ||

            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)

        {

            //I have moved this down here to prevent it loading on start (that was causing it to appear before it was tracking)

            StartCoroutine(DownloadAndCache());

         



        }

    }

}

 

How to call Asset Bundle Models after image tracking

May 25, 2014 - 11:42am #11

Just to update.. got the cloud to work now :D

scanning is expensive :P it runs like water :D

How to call Asset Bundle Models after image tracking

May 25, 2014 - 1:42am #10

Ok i've already started with the cloud reco sample... now all i gotta do is make sure each image tracked has a metadata in it with url and in the app make sure it uses that metadata to read the url to download dynamically...

Any documentation i could read to see how the metadata is called/used for this purpose ?

How to call Asset Bundle Models after image tracking

May 24, 2014 - 6:03pm #9

I recommend using the simpler Cloud Reco sample, not Books - take a look at books if you want to see how to call out to your own webserver in response to a query result, but start your project in the more basic sample project. 

Try logging the code path you're expecting to verify the expected behavior - I'd also validate that you're parenting the object as you intend (e.g. check the names )

How to call Asset Bundle Models after image tracking

May 24, 2014 - 3:35pm #8

well.. started this app on the image tracker to test the fetching ability for asset bundle... n slowly got all the pieces to work together...

now i am plannin to move it to the cloud reco scene... which im still confused to use the book demo or the cloud reco sample...

anyways this is the script im using atm which works well but it seems like the asset bundle doesnt wait for track to start at the second run of the app...

 

using System;
using UnityEngine;
using System.Collections;
	
public class AssetBundleAugmenter : MonoBehaviour, ITrackableEventHandler {
     
	public string BundleURL;
    public string AssetName;
    public int version;
     
    private GameObject mBundleInstance = null;
     
    private TrackableBehaviour mTrackableBehaviour;
     
    private bool mAttached = false;
     
    void Start() {  
        StartCoroutine(DownloadAndCache());
         
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour) {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
    }

    IEnumerator DownloadAndCache (){

		while (!Caching.ready)
			yield return null;
		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 == "") {
    GameObject instantiated = Instantiate(bundle.mainAsset) as GameObject;
    GameObject cloudRecoObject = GameObject.Find("GameObject");
    instantiated.transform.parent = cloudRecoObject.transform;
}
          bundle.Unload(false);      	
			} 
	}
  
public void OnTrackableStateChanged(
        TrackableBehaviour.Status previousStatus,
        TrackableBehaviour.Status newStatus) {
         
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_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;
            }
        }
    }
	
}

 

How to call Asset Bundle Models after image tracking

May 24, 2014 - 12:38pm #7

Are you reloading the asset bundle in the second run of the app? 

It's hard to guess, but be sure that rendering for those assets is disabled when your app starts - so they won't appear.

How to call Asset Bundle Models after image tracking

May 24, 2014 - 7:23am #6

It is working now on the editor but once built on the device... it seems like the loaded assetbundle works like a charm for the first time.... but if the app is opened the second time... the asset bundle is stuck on the screen until the marker is shown... then it goes back to the marker and dissapears as usual... how can i fix this pls?

How to call Asset Bundle Models after image tracking

May 22, 2014 - 3:28pm #5

Are you attempting to download the asset once the target is found, or are you preloading and want to associate the asset with a target and have it rendered only when the target is detected?

For the technique I describe you are setting a public variable from the DefaultTrackableEventHandler - the asset's parent GameObject contains a script that handle the loading. So you first use GameObject.Find( 'parent object's name' ) in the OnTargetFound method of the event handler and then reference the script that loads the asset using GetComponent. 

Is that what you're trying to do?

How to call Asset Bundle Models after image tracking

April 10, 2014 - 7:16am #4

Hi,

From David's post;

To reference the script, first find the GameObject using GameObject.Find([name]) and then use go.GetComponent<script_name>() to reference the script from that GameObject reference. The boolean can then be set directly using ref.targetIsFound.

this part is a little blur to me. Forgive me I am not much of a coder. Can you explain how to achieve this? The model is always on the screen unlike other examples.

How to call Asset Bundle Models after image tracking

September 27, 2013 - 2:33pm #3

Thanks very much, that worked for me very well. 

How to call Asset Bundle Models after image tracking

September 1, 2013 - 5:16pm #2

To accomplish that, I think you'll want to call StartCoroutine (DownloadAndCache()); from Update() instead of Start() and have Update check a boolean that is set from the ImageTargets trackable event handler on the GameObjects script component for this scripts.

 

e.g.

public bool targetIsFound = false;

Update(){

   if( targetIsFound ){

       StartCoroutine (DownloadAndCache());

       // reset the bool or the method will be be called for each subsequent frame

      targetIsFound = false;

   }

}

 

To reference the script, first find the GameObject using GameObject.Find([name]) and then use go.GetComponent<script_name>() to reference the script from that GameObject reference. The boolean can then be set directly using ref.targetIsFound.

Log in or register to post comments