Get Started         Best Practices         Developer Guide         FAQ        

Tutorial: Create a Unity app with CraftAR Augmented Reality SDK

Last Updated: Apr 30, 2018

The CraftAR Augmented Reality Unity SDK provides a set of tools to create apps directly from the context menu. The very first step that you'll need to take is to set up your Unity project. Once the project is set, you can follow the steps below. Note that you'll need to create and publish at least one item with Unity to use the app that we're going to create with this tutorial.

  1. Create the CraftAR app.
  2. Connect the app with your account in the CraftAR service.
  3. Build and run on your smartphone

At the end of this tutorial, we also explain the script that is generated once the app is built.

1. Create the CraftAR app

Create a new CraftAR Application by clicking on the context menu CraftAR > New CraftAR App.

Set the name of your CraftAR app script and click ‘Create’

This wizard will create a new Unity Scene, containing an object named CraftARSDK. This object provides the following capabilities in your Unity application:

  • Manage all the interaction with the CraftAR service
  • Draw the frames captured by the camera
  • Render the Augmented Reality scene.

2. Connect the app with your account in the CraftAR service

Click on the CraftARSDK Object, go to the Unity Inspector, and edit the field labeled ‘Collection token’ with the Token of your collection (see What is a token and how can I get it).

3. Build and run on your smartphone

First, set your the App ID (Bundle identifier) in order to be able to add AR items from CraftAR. We recommend that, for testing purposes you set he Bundle Identifier to com.catchoom.test

For more information about App IDs check this tutorial

In order to run this application, go to File > Build Settings and click on Add Current to set the current scene.

Plug your smartphone or tablet to your computer. Finally, click on Build And Run and select a destination.

In the case of Android, it generates an APK file and runs the application. For iOS, it launches XCode and creates a new project, compiles it and runs the application. All this process is the standard of Unity.

Once the app is running, point to an image of any of your ARItems in your collection, and the AR experience will be shown.

Script of the app we just created

If you’re curious about the script that this app runs, you can open the script to edit it. Make your script implement the CraftARSDKCallbacks interface, and all of its methods. Note that you have to use the namespace System.Collections.Generic.

using UnityEngine;
using System.Collections.Generic;
using System.Collections;

public class MyCraftARApp : MonoBehaviour,CraftARSDK.CraftARSDKCallbacks, CraftARSDK.CraftARItemEvents {

// Use this for initialization
void Start () {}

// Update is called once per frame
void Update () {}

// CraftARSDK callbacks:
void CraftARSDK.CraftARSDKCallbacks.CraftARReady() {}
void CraftARSDK.CraftARSDKCallbacks.SearchResults(List<CraftARSearchResult> results){}
void CraftARSDK.CraftARSDKCallbacks.SearchError(CraftARError error) {}
void CraftARSDK.CraftARSDKCallbacks.CollectionReady() {}
void CraftARSDK.CraftARSDKCallbacks.TokenValidationError(CraftARError error) {}
void CraftARSDK.CraftARSDKCallbacks.PictureTaken(Texture2D image) {}
void CraftARSDK.CraftARSDKCallbacks.TakePictureFailed() {}

// Item Events
void CraftARSDK.CraftARItemEvents.ItemAdded(CraftARItemAR item) {}
void CraftARSDK.CraftARItemEvents.AddItemError(CraftARItemAR item, CraftARError error) {}
void CraftARSDK.CraftARItemEvents.TrackingStarted(CraftARItemAR item) {}
void CraftARSDK.CraftARItemEvents.TrackingLost(CraftARItemAR item) {}

}

In the Start() method set the current script as the callbacksHandler for the calls of the SDK.

void Start () { 
    //Set this app as Callback handler in the SDK.
    CraftARSDK.instance.setCraftARSDKCallbacksHandler(this);
    CraftARSDK.instance.setCraftARItemEventsHandler(this);
}

In the CraftARReadyMethod(), you can start using all the other SDK functions. As an example, you can setup your token here. In this case, we will start the finder mode once we have validated the token in the server:

void CraftARSDK.CraftARSDKCallbacks.CraftARReady() {
    CraftARSDK.instance.setToken ("yourcollectiontoken");
}
void CraftARSDK.CraftARSDKCallbacks.CollectionReady() {
    CraftARSDK.instance.startFinderMode();
}

​After calling CraftARSDK.instance.startFinderMode(), you will start receiving callbacks to your SearchResults() method (or to SearchError if any error was produced). In this callback, you will receive a list of items that were found in the last request.

In this example, we will first check if any result was found. If one or more results were found, we will take just the first one, which is the one with the highest score (i.e. best match), and check if it was an AR item or an IR item. When you find an item, you may want to stop the finder mode, and show a result to the user or start the AR experience:

void CraftARSDK.CraftARSDKCallbacks.SearchResults(Listresults){
    if (results.Count <= 0) {
        Debug.Log ("No results found");
        return;
    }

    // We found a result: stopping the finder mode
    CraftARSDK.instance.stopFinderMode ();

	//As an example, we take only the best match (the first in the list of matches). 
	CraftARItem bestMatch = results [0].Item;
	switch (bestMatch.ItemType) {
	case CraftARItem.CraftARItemType.AUGMENTED_REALITY_ITEM:
		Debug.Log ("Found AR Item with name: "+bestMatch.itemName);
		CraftARItemAR arItem = bestMatch as CraftARItemAR;
		//Add item to the ARScene, start tracking and load item content.
		CraftARSDK.instance.AddSceneARItem(arItem);
		break;
	case  CraftARItem.CraftARItemType.IMAGE_RECOGNITION_ITEM:
		Debug.Log ("Found IR Item with name: "+bestMatch.itemName);
		break;
	} 
}

Go to the Unity editor, run the app, point to your AR reference image, and you should be able to see the contents drawn in top of it.

Couldn't find what you were looking for?

support@catchoom.com
https://cdn.desk.com/
false
catchoom
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
false
Invalid characters found
/customer/en/portal/articles/autocomplete