Table of Contents

Get target status

During session operation, the target undergoes status changes such as tracking and loss. The following content explains how to obtain and use the target's status information, and how to control content display using found and lost events.

Before you start

  • Understand the basic concepts, components, and workflow of sessions through Introduction to ARSession.
  • Understand the basic concepts, statuses, and lifecycle of targets through Target.

Check if target is tracked

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

Use target found and lost events

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

For example, the following code demonstrates playing a video when the target is tracked and pausing 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 session's OnDestroy process. Since the execution order of Unity's OnDestroy is not guaranteed, validity checks must be performed on objects used in events to avoid accessing destroyed objects during the OnDestroy process.

Next steps

  • Active control strategy introduces the default show/hide strategy for objects under targets and how to adjust it as needed.