Log in or register to post comments

Loading prefabs through .json-files

November 14, 2013 - 9:47am #1

Hi all,

I was thinking if it's possible to have the .json-file fetch a prefab from a designated URL and drop this into the rendered scene, upon the assigned image-target. 
If this is possible, wouldn't I be able to basically create an application, which after it's initial Google Play and AppStore release, could be updated with new targets and completely new content, without having to actually update the binary? 

So basically, what I'm asking is, if this is possible:

- Scan an image

- Fetch the metadata from that image

- Fetch the .json-file, assigned inside the metadata

- Connect to a specific URL, assigned inside the .json-file, and download a Unity .prefab

- Place the prefab inside the rendered scene, on top of the image-target

Any suggestions?

Loading prefabs through .json-files

November 14, 2013 - 11:42am #7

Good idea - thank you :)

I'll look properly into it when I get back infront of my computer, and find a suitable structure for it. Again, thank you for the support! 

Loading prefabs through .json-files

November 14, 2013 - 11:37am #6

Yes, you've got it. Though you might want to implement the asset loading in a seperate script component, and call that via GetComponent<classname>() to free up you event handler to catch new search results, in case the download hangs or the user wants to search on another target.

Loading prefabs through .json-files

November 14, 2013 - 10:55am #5

Okay, I'm on my phone atm, but I'll try just to put it easily understandable.

In the sample code, under this specific section:
 

public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
        // duplicate the referenced image target
        GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;         
        GameObject augmentation = null;         
        string model_name = targetSearchResult.MetaData;                  
        if( augmentation != null )
            augmentation.transform.parent = newImageTarget.transform; 
        // enable the new result with the same ImageTargetBehaviour:
        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);         
        Debug.Log("Metadata value is " + model_name );                           
        switch( model_name ){             
            case "teapot" :                 
                Destroy( imageTargetBehaviour.gameObject.transform.Find("Dragon").gameObject );                
                break;             
            case "dragon" :                 
                Destroy( imageTargetBehaviour.gameObject.transform.Find("teapot").gameObject );             
                break;            
        }
        if (imageTargetBehaviour != null)
        {
            // stop the target finder
            mCloudRecoBehaviour.CloudRecoEnabled = false;
        }
    }
  

This is where I supposedly could do something along the lines of this:
 


public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
        // duplicate the referenced image target
        GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;         
        GameObject augmentation = null; 

        //// CHANGING model_name TO AN EXPECTED URL, CALLED LATER BELOW    
        public string BundleURL = targetSearchResult.MetaData;   
               
        if( augmentation != null )
            augmentation.transform.parent = newImageTarget.transform; 
        // enable the new result with the same ImageTargetBehaviour:
        ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);         
        Debug.Log("Metadata value is " + model_name );
                           
        using (WWW www = new WWW(BundleURL)) {
		   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));
        
        }
        if (imageTargetBehaviour != null)
        {
            // stop the target finder
            mCloudRecoBehaviour.CloudRecoEnabled = false;
        }
    }

This code is just a quick patch-up on my phone, so it's probably far from valid, but I hope you get the idea. What I'm trying is to simply call a WWW inside the handling of the metadata, in order to specify a to a downloadable prefab. As an example, the metadata-file could containt a line like this:

http://www.mydomain.com/folder/on/server/my.prefab

This is the idea, isn't it? Or am I wrong?

Loading prefabs through .json-files

November 14, 2013 - 10:30am #4

Yes you can exclude the JSON file completely.

Here's a tutorial on developing Cloud Recognition apps in Unity - https://developer.vuforia.com/resources/tutorials/creating-cloud-recognition-apps-unity

You'll retrieve the metadata in the event handler that you implement to receive search results. See the sample code in this thread - https://developer.vuforia.com/forum/cloud-recognition/create-cloud-recognition-sample-project

* use that instead of the event handler in the sample.

 

Loading prefabs through .json-files

November 14, 2013 - 10:08am #3

Thank you for the fast response.

In which script, inside the Vuforia CloudReco Unity Package, is the handling of the metadata-file processed? Am I right in thinking, that it is in such script I'm supposed to tell the application to expect a second line inside the metadata-file, that specifies which prefab to be loaded? And furthermore, does that mean that I could potentially exclude the use of .json-files completely?

 

EDIT: Does any actual tutorial exist on how to accomplish this? The way I understand the Vuforia Sample Application is that it's initially restricted to the use of already hardcoded textures and other assets, and not actually fetching the assets from an online server.

Loading prefabs through .json-files

November 14, 2013 - 9:54am #2

Yes. You can accomplish that using Unity AssetBundles - http://docs.unity3d.com/Documentation/Manual/AssetBundlesIntro.html

Also you don't need to use the .json file, rather you could provide the asset path in the metadata file attached to the search result. The use of a second JSON file is  feature of the sample, but not necessary for all applications. 

What you're describing is the way that many Vuforia apps version their content. 

Log in or register to post comments