Removing the AR prompt during tracking

It is really great having the graphics on the screen to assist the user while capturing a target. But once it's captured, we want to hide the prompts and display only the annotation graphics instead. If the camera loses tracking, we can restore the UI so the user has the option to recapture.

To do this, we need a script that disables Canvas elements. Unfortunately, presently the Vuforia components are only implemented to disable object renderers, and it does not support canvases. So, we will write our own, and we will make it generic enough to work with any GameObjects. We can do this in a script, using the Vuforia Trackable Events that will be attached to the UserDefinedTarget object.

To begin, in your Scripts folder, create a C# script named TrackableObjectVisiblity, and open it for editing. The full script is as follows:

File: TrackableObjectVisiblity.cs 
using System; 
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.Events; 
using Vuforia; 
 
[RequireComponent(typeof(TrackableBehaviour))] 
public class TrackableObjectVisibility : MonoBehaviour, ITrackableEventHandler { 
 
    public UnityEvent OnTargetFound; 
    public UnityEvent OnTargetLost; 
 
    private TrackableBehaviour trackableBehaviour; 
 
    void Start() { 
        trackableBehaviour = GetComponent<TrackableBehaviour>(); 
        trackableBehaviour.RegisterTrackableEventHandler(this); 
    } 
 
    public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus) { 
 
        if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) { 
            OnTargetFound.Invoke(); 
        } else { 
            OnTargetLost.Invoke(); 
        } 
    } 
 
} 

At the top, we first declare that we will be using definitions from UnityEngine.Events and from Vuforia.

The script references the TrackableBehaviour component of the UserDefinedTarget so we require it up front.

Then we declare the class to implement the ITrackableEventHandler interface. If you use the Intellisense (light bulb), the editor will insert a prototype for the OnTrackableStateChanged function.

We add two public UnityEvent variables, OnTargetFound and OnTargetLost, which will let us declare in the Editor what object functions to call when the trackable events occur.

In the Start() function, we initialize the trackableBehavior and register this class as an event handler.

Then in OnTrackableStateChanged, we invoke either OnTargetFound or OnTargetLost. Vuforia provides a number of different tracking statuses. We don't need such a level of detail, as ours is a binary decision, tracking or not, and considers, DETECTED, TRACKED, and EXTENDED_TRACKED to all be tracking.

For more information on the Vuforia TrackableBehavior class and tracking statuses, see the documentation at https://library.vuforia.com/content/vuforia-library/en/reference/unity/classVuforia_1_1TrackableBehaviour.html

Save the script, go back to Unity, and attach the script to User Define Target, as follows:

  1. Select UserDefinedTarget in Hierarchy.
  2. Drag the TrackableObjectVisibility script as a component.

Now we can add the event handlers. For On Target Found():

  1. Press the + to add to the list.
  2. Drag the AR Prompt panel to the Object slot.
  3. For function, choose GameObject.SetActive.
  4. Leave the checkbox unchecked.

Similarly, for On Target Lost():

  1. Press the + to add to the list.
  2. Drag the AR Prompt panel to the Object slot.
  3. For function, choose GameObject.SetActive.
  4. This time check the checkbox.

The Trackable Object Visibility panel should look like this:

Now when you press Play, you will see the circle and capture button. Then when you capture and track, and the cube appears, the UI hides. If you move the camera too fast or otherwise lose tracking, the cube goes away and the capture UI reappears. Woot!

We will use this script again later when we integrate a world space version of the instruction canvas into the AR experience. But first, let's add one more feature.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset