Working with Cloud Image Recognition in Unity¶
This article introduces you to the steps for creating a basic Vuforia Engine Cloud Image Recognition app in Unity. Use Cloud Image 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 Image 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¶
- A Supported Unity version with the latest Vuforia SDK Package. See Supported Versions and Getting Started with Vuforia Engine in Unity for a setup guide.
- Your preferred code editor (Visual Studio, Mono Develop, Xcode, etc.).
- A Vuforia Engine Developer Account.
Instructions¶
- Create a new project in Unity.
- Create a license key for your project.
- Add the license key to your Vuforia Engine app.
- In the Hierarchy window, delete the Main Camera object.
- In the GameObject menu, select and add Vuforia Engine -> AR Camera.
- In the GameObject menu, select Vuforia Engine -> Cloud Recognition -> Cloud Recognition. A new Cloud Recognition object is created.
- 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.
- Create a C# script named
SimpleCloudRecoEventHandler.cs
- Attach the
SimpleCloudRecoEventHandler
script to the Cloud Recognition GameObject. -
Implement the
SimpleCloudRecoEventHandler
script by referencing the following code samples:-
Implement the
RegisterEventHandlers
interface and register it with the CloudRecoBehaviour: -
Implementing the
OnInitialized()
,OnInitError()
andOnUpdateError()
methods are optional. These methods provide useful log data in case an error occurs: -
Implement the
OnStateChanged()
method of the interface to determine whether Vuforia Engine is currently performing a Cloud Reco scan: -
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. -
Implement the
OnGUI()
method to display the current scanning state and the metadata of the last cloud target detected:
-
-
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:
- In the GameObject menu, select Vuforia Engine -> Cloud Image -> Cloud Image Target to add a new Image Target object.
- Right-click ImageTarget and select 3D Object -> Cube to create a simple cube object.
- Center and scale the cube object appropriately so that it fits nicely on top of the Image Target plane in the scene.
- Optionally, add directional lighting to the scene so that any augmentation appears shaded.
- Add the
SimpleCloudRecoEventHandler.cs
script as a component to the Cloud Image Target GameObject. -
Modify the
SimpleCloudRecoEventHandler.cs
script by adding the following code to theOnNewSearchResult()
method. This code programmatically instantiates an Image Target that corresponds to the one detected by the Cloud Image Recognition engine: -
Select the CloudRecognition object in the scene.
The Inspector should display the Image Target Template field in the Simple Cloud Reco Event Handler component.
-
In the Hierarchy window, drag the ImageTarget into the Image Target Template field in the Inspector window.
- Save the current scene.
- Build and run your project or test it with Vuforia Play Mode.
Learn More¶
How to Perform an Image Recognition Query