Get target status
During session running, target goes through state changes such as tracking and lost. Through the following content, you will learn how to get and use target status information, and how to use found and lost events to control content display.
Before you begin
- Learn the basic concepts, composition, and workflow of session through ARSession introduction.
- Learn the basic concepts, status, and lifecycle of target through Target.
Determine whether target is tracked
You can use the TargetController.IsTracked property to determine whether target is tracked.
Use target found and lost events
You can use the TargetController.TargetFound and TargetController.TargetLost events to handle cases where target is tracked or lost.
For example, the following code shows the process of playing video when target is tracked and pausing video playback when target is lost:
target.TargetFound += () =>
{
if (player && player.gameObject.activeInHierarchy)
{
player.Play();
}
};
target.TargetLost += () =>
{
if (player && player.gameObject.activeInHierarchy)
{
player.Pause();
}
};
Caution
If target is not manually unloaded, TargetController.TargetLost may be called when session stops. If session is not manually stopped, it may be called during session OnDestroy. Because Unity's OnDestroy execution order is not guaranteed, objects used in the event need to be checked for validity to avoid accessing objects that have already been destroyed during OnDestroy.
Next steps
- active control policy introduces the default show and hide policy for objects under target, and how to adjust it as needed.