Table of Contents

Get the state of a target

During the operation of a session, the target will undergo state changes such as tracking and loss. Through the following content, you will learn how to obtain and use the state information of the target, as well as how to use the found and lost events to control the display of content.

Before you begin

  • Learn the basic concepts, composition, and workflow of a session through Introduction to ARSession.
  • Learn the basic concepts, states, and lifecycle of a target through Target.

Determine if the target is being tracked

You can use the TargetController.IsTracked property to determine whether the target is being tracked.

Use the found and lost events of the target

You can use the TargetController.TargetFound and TargetController.TargetLost events to handle situations where the target is tracked or lost.

For example, the following code demonstrates the process of playing a video when the target is tracked and pausing the video playback when the target is lost:

target.TargetFound += () =>
{
    if (player && player.gameObject.activeInHierarchy)
    {
        player.Play();
    }
};
target.TargetLost += () =>
{
    if (player && player.gameObject.activeInHierarchy)
    {
        player.Pause();
    }
};
Caution

If the target is not manually unloaded, TargetController.TargetLost may be called when the session stops. If the session is not manually stopped, it may be called during the OnDestroy process of the session. Since the execution order of Unity's OnDestroy is not guaranteed, objects used in the event need to be checked for validity to avoid accessing already destroyed objects during the OnDestroy process.

Next steps

  • Active control strategy introduces the default display and hide strategy for objects under the target, and how to adjust it as needed.