Working with Cloud Recognition in Unity

This article introduces you to the steps for creating a basic Vuforia Engine Cloud Recognition app in Unity. Use Cloud Recognition in your application to recognize and track millions of Image Targets.

To learn more about the benefits, licensing, and use cases of Cloud Recognition, please refer to the Cloud Recognition article.

Accessing Sample Code

A great way to start is to see the Cloud Recognition feature in action. It is available in Unity as a part of the Vuforia Core Samples. The sample scene demonstrates how the OnNewSearchResult method retrieves a CloudRecoSearchResult from which the metadata is available in the CloudRecoEventHandler.cs script.

Unity Setup

The following steps explain how to create a cloud recognition app in Unity. The app reacts to a cloud target detection event, retrieves the metadata associated with the target, and displays the metadata text.

Prerequisites 

Instructions

  1. Create a new project in Unity.
  2. Create a license key for your project.
  3. Add the license key to your Vuforia Engine app.
  4. In the Hierarchy window, delete the Main Camera object.
  5. In the GameObject menu, select and add Vuforia Engine -AR Camera
  6. In the GameObject menu, select Vuforia Engine -> Cloud Recognition -Cloud Recognition. A new Cloud Recognition object is created.
  7. In the Inspector window of your Cloud Recognition GameObject, enter your cloud database client keys in the client keys in the Access Key and Secret Key fields. For information about creating a cloud database, refer to the Cloud Databases article. 
  8. Create a C# script named SimpleCloudRecoEventHandler.cs
  9. Attach the SimpleCloudRecoEventHandler script to the Cloud Recognition GameObject.
  10. Implement the SimpleCloudRecoEventHandler script by referencing the following code samples:
  1. Implement the RegisterEventHandlers interface and register it with the CloudRecoBehaviour:
using UnityEngine;
using Vuforia;

public class SimpleCloudRecoEventHandler : MonoBehaviour
{
    CloudRecoBehaviour mCloudRecoBehaviour;
    bool mIsScanning = false;
    string mTargetMetadata = "";

    public ImageTargetBehaviour ImageTargetTemplate;

    // Register cloud reco callbacks
    void Awake()
    {
        mCloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
        mCloudRecoBehaviour.RegisterOnInitializedEventHandler(OnInitialized);
        mCloudRecoBehaviour.RegisterOnInitErrorEventHandler(OnInitError);
        mCloudRecoBehaviour.RegisterOnUpdateErrorEventHandler(OnUpdateError);
        mCloudRecoBehaviour.RegisterOnStateChangedEventHandler(OnStateChanged);
        mCloudRecoBehaviour.RegisterOnNewSearchResultEventHandler(OnNewSearchResult);
    }
    //Unregister cloud reco callbacks when the handler is destroyed
    void OnDestroy()
    {
        mCloudRecoBehaviour.UnregisterOnInitializedEventHandler(OnInitialized);
        mCloudRecoBehaviour.UnregisterOnInitErrorEventHandler(OnInitError);
        mCloudRecoBehaviour.UnregisterOnUpdateErrorEventHandler(OnUpdateError);
        mCloudRecoBehaviour.UnregisterOnStateChangedEventHandler(OnStateChanged);
        mCloudRecoBehaviour.UnregisterOnNewSearchResultEventHandler(OnNewSearchResult);
    }
  1. Implementing the OnInitialized()OnInitError() and OnUpdateError() methods are optional. These methods provide useful log data in case an error occurs:
    public void OnInitialized(CloudRecoBehaviour cloudRecoBehaviour)
    {
        Debug.Log("Cloud Reco initialized");
    }

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

    public void OnUpdateError(CloudRecoBehaviour.QueryError updateError)
    {
        Debug.Log("Cloud Reco update error " + updateError.ToString());

    }
  1. Implement the OnStateChanged() method of the interface to determine whether Vuforia Engine is currently performing a Cloud Reco scan:
    public void OnStateChanged(bool scanning)
    {
        mIsScanning = scanning;

        if (scanning)
        {
            // Clear all known targets
        }
    }
  1. Implement the OnNewSearchResult() method. This code only processes a cloud recognition event without displaying a 3D augmentation for the target. See Add a Cloud Image Target below.
    // Here we handle a cloud target recognition event
    public void OnNewSearchResult(CloudRecoBehaviour.CloudRecoSearchResult cloudRecoSearchResult )
    {
        // Store the target metadata
        mTargetMetadata = cloudRecoSearchResult.MetaData;

        // Stop the scanning by disabling the behaviour
        mCloudRecoBehaviour.enabled = false;
    }
  1. Implement the OnGUI() method to display the current scanning state and the metadata of the last cloud target detected:
  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")) {
          // Reset Behaviour
          mCloudRecoBehaviour.enabled = true;
          mTargetMetadata="";
          }
      }
  }
  1. Save the current scene.

Add a Cloud Image Target

Perform the following steps to add a 3D augmentation object on top of your Cloud Image Target upon detection:

  1. In the GameObject menu, select Vuforia Engine -> Cloud Image -> Cloud Image Target to add a new Image Target object. 
  2. Right-click ImageTarget and select 3D Object -> Cube to create a simple cube object. 
  3. Center and scale the cube object appropriately so that it fits nicely on top of the Image Target plane in the scene.
  4. Optionally, add directional lighting to the scene so that any augmentation appears shaded.
  5. Add the SimpleCloudRecoEventHandler.cs script as a component to the Cloud Image Target GameObject.
  6. Modify the SimpleCloudRecoEventHandler.cs script by adding the following code to the OnNewSearchResult() method. This code programmatically instantiates an Image Target that corresponds to the one detected by the Cloud Recognition engine:
// Build augmentation based on target 
if (ImageTargetTemplate)
{
    /* Enable the new result with the same ImageTargetBehaviour: */
    mCloudRecoBehaviour.EnableObservers(cloudRecoSearchResult, ImageTargetTemplate.gameObject);
}
  1. Select the CloudRecognition object in the scene.
    The Inspector should display the Image Target Template field in the Simple Cloud Reco Event Handler component.
  2. In the Hierarchy window, drag the  ImageTarget into the Image Target Template field in the Inspector window.
  3. Save the current scene.
  4. Build and run your project or test it with Vuforia Play Mode.

Can this page be better?
Share your feedback via our issue tracker