Unity - How do I create a simple Cloud Reco app

March 14, 2014 - 9:07am #1

This article explains how to create a basic Vuforia Cloud recognition app in Unity. The app in question simply reacts to a cloud target detection event and retrieves the Metadata associated to the target, displaying the metadata text on screen.

Steps:

  • create a new Unity project
  • import the Vuforia Unity Extension package (vuforia-unity-android-ios-X-Y-Z.unitypackage)
  • remove the Main Camera from your scene
  • browse your Project view to the 'Assets/Qualcomm Augmented reality/Prefabs' folder
  • drag the ARCamera and the CloudRecognition prefab into your scene
  • select the CloudRecognition object in your scene and look at the Inspector panel
  • within the CloudRecognition inspector, fill in the Access Key and Secret Key fields with the client keys of your Cloud Database (see also: https://developer.vuforia.com/resources/dev-guide/managing-targets-cloud-database-using-target-manager for information about creating a Cloud Database)
  • create a C# script called for example 'SimpleCloudHandler.cs'
  • attach the script to the CloudRecognition prefab in your scene
  • implement the SimpleCloudHandler script as explained in the following:
    • implement the ICloudRecoEventHandler interface and register it with the CloudRecognitionBehaviour, as illustrated in this code snippet:
public class SimpleCloudHandler : MonoBehaviour, ICloudRecoEventHandler {
	
    private CloudRecoBehaviour mCloudRecoBehaviour;
	
    private bool mIsScanning = false;
    private string mTargetMetadata = "";

    // Use this for initialization
    void Start () {
        // register this event handler at the cloud reco behaviour
        mCloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
        if (mCloudRecoBehaviour)
        {
            mCloudRecoBehaviour.RegisterEventHandler(this);
        }
    }
}
  • implement the methods of the OnInitialized(), OnInitError() and OnUpdateError() of the ICloudRecoEventHandler interface, as shown in this example:
public void OnInitialized() {
    Debug.Log ("Cloud Reco initialized");
}

public void OnInitError(TargetFinder.InitState initError) {
    Debug.Log ("Cloud Reco init error " + initError.ToString());
}

public void OnUpdateError(TargetFinder.UpdateState updateError) {
    Debug.Log ("Cloud Reco update error " + updateError.ToString());
}
  • implement the OnStateChanged() method of the OnStateChanged interface to keep track of the "scanning" mode (i.e. to know whether Vuforia is scanning the Cloud or not):
public void OnStateChanged(bool scanning) {
    mIsScanning = scanning;
		
    if (scanning)
    {
        // clear all known trackables
        ImageTracker tracker = TrackerManager.Instance.GetTracker<ImageTracker>();
        tracker.TargetFinder.ClearTrackables(false);
    }
}

 

As an additional example, suppose you want to show some 3D augmentation object on top of your Cloud target upon detection; these are the few additional steps that you could follow w.r.t the previous ones:

  • Add an Image Target to your scene (from 'Qualcomm Augmented Reality' folder, drag an ImageTarget prefab into the scene)
  • Add some 3D model as child of the Image Target (e.g., you can create a simple cube object) and center / scale it appropriately, so that it fits nicely on top of the Image Target plane in the scene
  • Select the mage Target in the scene and go to the inspector panel => change the target Type from 'Predefined' to 'Cloud Reco'
  • Modify your SimpleCloudHandler script, by adding the following code to the OnNewSearchResult() method; this is meant to programmatically instantiate an ImageTarget that corresponds to the one detected (recognized) by the Cloud Recognition engine at any time:
// Build augmentation based on target
if (ImageTargetTemplate) {
    // enable the new result with the same ImageTargetBehaviour:
    ImageTracker tracker = TrackerManager.Instance.GetTracker<ImageTracker>();
    ImageTargetBehaviour imageTargetBehaviour = 
        (ImageTargetBehaviour)tracker.TargetFinder.EnableTracking(
            targetSearchResult, ImageTargetTemplate.gameObject);
}
  • Also, add a public member variable called ImageTargetTemplate to the script:
public class CubeCloudHandler : MonoBehaviour, ICloudRecoEventHandler {
	
	public ImageTargetBehaviour ImageTargetTemplate;
  • Finally, back in the Unity scene, select the CloudRecognition object in your scene, so that the Inspector shows the 'Image Target Template' field in the Simple Cloud Handler component
  • From the scene hierarchy, drag the Image Target onto the Image Target Template in the Inspector.

 

Topic locked