Which Function/Class/Script from EasyAR I need to use to detect if a model placed on its target?

0 votes
asked Apr 19, 2018 by hamayonbaig (130 points)
Hey Guys, How can I check if a model is placed on the image target in runtime and then run another piece of code. Lets say When the model is placed on the target I want to play a sound effect. How can I check if the model placed by the EasyAR on the target and then play the sound?

I am using unity.

Please help.

1 Answer

+1 vote
answered Apr 19, 2018 by chadidi (510 points)

Run your code inside OnTargetFound method inside ImageTargetBehaviour class

for example, set the screen to never turn off when the target found and to return to normal when target lost code below :

using UnityEngine;
using EasyAR;
using System;

public class MyImageTargetBehaviour : ImageTargetBehaviour
{
    protected override void Awake()
    {
        base.Awake();
        TargetFound += OnTargetFound;
        TargetLost += OnTargetLost;
        TargetLoad += OnTargetLoad;
        TargetUnload += OnTargetUnload;
    }

    void OnTargetFound(TargetAbstractBehaviour behaviour)
    {
        Debug.Log("Found: " + Target.Id);
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
    }

    void OnTargetLost(TargetAbstractBehaviour behaviour)
    {
        Debug.Log("Lost: " + Target.Id);
        Screen.sleepTimeout = SleepTimeout.SystemSetting;
    }

    void OnTargetLoad(ImageTargetBaseBehaviour behaviour, ImageTrackerBaseBehaviour tracker, bool status)
    {
        Debug.Log("Load target (" + status + "): " + Target.Id + " (" + Target.Name + ") " + " -> " + tracker);
    }

    void OnTargetUnload(ImageTargetBaseBehaviour behaviour, ImageTrackerBaseBehaviour tracker, bool status)
    {
        Debug.Log("Unload target (" + status + "): " + Target.Id + " (" + Target.Name + ") " + " -> " + tracker);
    }
}

commented Apr 20, 2018 by hamayonbaig (130 points)
Thanks man, but this function means that the target is found but it does not ensures that the model is placed on the target. Is there any function in Camera etc that runs when the model is placed on the target? Thanks
commented Apr 20, 2018 by chadidi (510 points)
You can add Audio source to an empty object or any object inside image target so that whenever the image target found the audio will be played.
Welcome to EasyAR SDK Q&A, where you can ask questions and receive answers from other members of the community.
...