Adding a Custom TrackableEventHandler in Image Target – Vuforia

With the progression of the project, I have implemented a custom event for different image targets. This is to allow images to receive different commands when the image is targeted, or when the status of the image target is set to DETECTED or TRACKED. Vuforia provides status information for every track able images when it is available or not during run-time. The reason of applying this is to attempt set active the exact game object when the image is loaded in the ARCamera in the second scene.

By creating this function, I have named the file CustomImageTarget. By using a new Unity Event called onFound and onLost I have on public, mentioning On Tracking Found from Vuforia’s provided script which is the DefaultTrackableEventHandler, I have input an Invoke function based on OnFound and with the same action if the image target is On Tracking Lost.

using UnityEngine;
using Vuforia;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CustomImageTarget : DefaultTrackableEventHandler
{
    public UnityEvent onFound;
    public UnityEvent onLost;

    protected override void OnTrackingFound()
    {
        // if you also want the default behaviour in this
        // (enables Renderers, Colliders and Canvas)
        // base.OnTrackingFound();

        onFound.Invoke();
    }

    protected override void OnTrackingLost()
    {
        // if you also want the default behaviour in this
        // (Disables Renderers, Colliders and Canvas)
        // base.OnTrackingLost();

        onLost.Invoke();
    }
}

When inserting this script to one of the image targets. As seen from the screen capture below, it allows me to attach a reaction in the Inspector by using Drag&Drop, much like how an onClick of a button component would be set up also.

Leave a comment

Design a site like this with WordPress.com
Get started