How Change Image target behaviour in Vuforia

Hi,

Am using Unity and Vuforia for creating non-game Android application. I have small doubt. Whether we can change the detect behavior of the image target (Augmented Reality)?. For example: When we target out picture , instead of opening the animation, I want to open my new scene. if there is a way for this, please help me with this. Thank you in advance.

Hi,

You need to define Your own Handler script which inherited from ITrackableEventHandler. I have example with cloud reco (which inherited form ICloudRecoEventHandler not ITrackableEventHandler) . In case of cloud reco script is attached to CloudRecognition prefab, but in your case it should be ARCamera I think. ImageTargetTemplate is our imageTarget from vuforia database.

using UnityEngine;
using System.Collections;
using Vuforia;
using UnityEngine.SceneManagement;

public class SimpleCloudHandler : MonoBehaviour, ICloudRecoEventHandler {
	public ImageTargetBehaviour ImageTargetTemplate;
	private CloudRecoBehaviour mCloudRecoBehaviour;
	private bool mIsScanning = false;
	private string mTargetMetadata = "";
	// Use this for initialization
	void Start () {
		// register this event handler at the cloud reco behaviour
		mCloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
		if (mCloudRecoBehaviour)
		{
			mCloudRecoBehaviour.RegisterEventHandler(this);
		}
	}
	public void OnInitialized(){
		Debug.Log ("cloud reco init");
	}
	public void OnInitError(TargetFinder.InitState initError){
		Debug.Log ("Cloud reco init error " + initError.ToString ());
	}
	public void OnUpdateError(TargetFinder.UpdateState updateError){
		Debug.Log ("cloud reco update error " + updateError.ToString ());
	}
	public void OnStateChanged(bool scanning){
		mIsScanning = scanning;
		if (scanning) {
			TrackerManager.Instance.GetTracker<ObjectTracker> ().TargetFinder.ClearTrackables (false);
		}
	}
	public void OnNewSearchResult(TargetFinder.TargetSearchResult tsr){
		mTargetMetadata = tsr.MetaData;
		mCloudRecoBehaviour.CloudRecoEnabled = false;
		if(ImageTargetTemplate){
		ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker> ();
			**SceneManager.LoadScene ("cloud_test2");**
				}
	}
	void OnGui(){
		GUI.Box (new Rect (100, 100, 200, 50), mIsScanning ? "Scanning" : "Not Scanning");
		GUI.Box (new Rect (100, 200, 200, 50), "Metadata: " + mTargetMetadata);
		if (!mIsScanning) {
			if (GUI.Button (new Rect (100, 300, 200, 50), "Restart Scanning")) {
				mCloudRecoBehaviour.CloudRecoEnabled = true;
			}
		}
	}

}

Thank you for your reply. Am beginner with unity and vuforia. Can you give me example script regarding ITrackableEventHandler ?

Make custom class inherited from MonoBehaviour, ITrackableEventHandler (you can copy DefaultTrackableEventHandler ). Put custom behaviour. Use CustomTrackableEventHandler instead DefaultTrackableEventHandler.


Here is the sample:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
 
public class CustomTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
{
    #region PROTECTED_MEMBER_VARIABLES

    protected TrackableBehaviour mTrackableBehaviour;
    protected TrackableBehaviour.Status m_PreviousStatus;
    protected TrackableBehaviour.Status m_NewStatus;

    #endregion // PROTECTED_MEMBER_VARIABLES

    #region UNITY_MONOBEHAVIOUR_METHODS

    protected virtual void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
    }

    protected virtual void OnDestroy()
    {
        if (mTrackableBehaviour)
            mTrackableBehaviour.UnregisterTrackableEventHandler(this);
    }

    #endregion // UNITY_MONOBEHAVIOUR_METHODS

    #region PUBLIC_METHODS

    /// <summary>
    ///     Implementation of the ITrackableEventHandler function called when the
    ///     tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
        TrackableBehaviour.Status previousStatus,
        TrackableBehaviour.Status newStatus)
    {
        m_PreviousStatus = previousStatus;
        m_NewStatus = newStatus;

        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
            // Custom code here ...
            AppManager.Instance.FoundTrackable(mTrackableBehaviour.TrackableName);
            OnTrackingFound();
        }
        else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
                 newStatus == TrackableBehaviour.Status.NO_POSE)
        {
            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
            // Custom code here...
            AppManager.Instance.LostTrackable();
            OnTrackingLost();
        }
        else
        {
            // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
            // Vuforia is starting, but tracking has not been lost or found yet
            // Call OnTrackingLost() to hide the augmentations
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS

    #region PROTECTED_METHODS

    protected virtual void OnTrackingFound()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);

        // Enable rendering:
        foreach (var component in rendererComponents)
            component.enabled = true;

        // Enable colliders:
        foreach (var component in colliderComponents)
            component.enabled = true;

        // Enable canvas':
        foreach (var component in canvasComponents)
            component.enabled = true;
    }


    protected virtual void OnTrackingLost()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);

        // Disable rendering:
        foreach (var component in rendererComponents)
            component.enabled = false;

        // Disable colliders:
        foreach (var component in colliderComponents)
            component.enabled = false;

        // Disable canvas':
        foreach (var component in canvasComponents)
            component.enabled = false;
    }

    #endregion // PROTECTED_METHODS
}