Marker detection in Unity3D

Hi everyone,

I often get the question: “How to tell if the marker is detected in Unity3D?”

The easiest thing to find that out is to listen for the OnMarkerFound(ARMarker)  and OnMarkerTracked(ARMarker)  events. ARToolKit5 is sending the OnMarkerFound(ARMarker)  event everytime a new marker is found. The OnMarkerTracked(ARMarker)  is called every frame for as long as the marker is visible in the video stream.

Register for events

To register for such events it is easiest if you create a new script that implements the OnMarkerFound()  and OnMarkerTracked()  functions and to attach this script to a GameObject.

public class MarkerEvent : MonoBehaviour {
    void OnMarkerFound(ARMarker marker)
    {
        if (marker.Tag.Equals("Marker1")) {
            //Do something
        }
    }
    void OnMarkerTracked(ARMarker marker)
    {
        if (marker.Tag.Equals("Marker1")) {
            //Do something each frame
        }
    }

Unity3D MarkerEventNow you need to go to your GameObject that contains the AR Tracked Object script. At the field Event Receiver click the circle on the right and choose your GameObject that contains the MarkerEvent-class.

 

This is it, now your script receives the marker events.

 

Bonus

Thanks for reading till the end As special bonus I’d like to tell you that there is another event function that you might be interested in. This is the onMarkerLost(ARMarker)  event. You can use is to clean-up once the marker is not visible in the scene any longer.

Additional documentation

The event generation happens in these lines of code:

https://github.com/artoolkit/arunity5/blob/master/src/Unity/Assets/ARToolKit5-Unity/Scripts/ARTrackedObject.cs#L148

https://github.com/artoolkit/arunity5/blob/master/src/Unity/Assets/ARToolKit5-Unity/Scripts/ARTrackedObject.cs#L163

https://github.com/artoolkit/arunity5/blob/master/src/Unity/Assets/ARToolKit5-Unity/Scripts/ARTrackedObject.cs#L175

 

Let me know how if this works for you, thanks for reading and happy coding.

4 Comments

  1. Shadab January 14, 2017
    • Thor_Bux January 23, 2017
  2. Amir Ibnu Alfian May 21, 2017
    • Thor_Bux May 22, 2017