using System;
using Unity.Collections;
using UnityEngine.XR.ARSubsystems;
using Unity.XR.CoreUtils;
namespace UnityEngine.XR.ARFoundation
{
///
/// A [trackable manager](xref:arfoundation-managers#trackables-and-trackable-managers) that detects and tracks
/// 3D bounding boxes in the physical environment. Add this component to your GameObject to enable 3D bounding box
/// detection in your app.
///
[DefaultExecutionOrder(ARUpdateOrder.k_BoundingBoxManager)]
[DisallowMultipleComponent]
[RequireComponent(typeof(XROrigin))]
[HelpURL(typeof(ARBoundingBoxManager))]
public class ARBoundingBoxManager : ARTrackableManager<
XRBoundingBoxSubsystem,
XRBoundingBoxSubsystemDescriptor,
XRBoundingBoxSubsystem.Provider,
XRBoundingBox,
ARBoundingBox>, IRaycaster
{
[SerializeField]
[Tooltip(
"If not null, this prefab is instantiated for each detected 3D bounding box. " +
"If the prefab does not contain an ARBoundingBox component, ARBoundingBoxManager will add one.")]
GameObject m_BoundingBoxPrefab;
///
/// Get or set the prefab to instantiate for each detected 3D bounding box. Can be .
///
public GameObject boundingBoxPrefab
{
get => m_BoundingBoxPrefab;
set => m_BoundingBoxPrefab = value;
}
///
/// The name to be used for the instantiated GameObject whenever a new 3D bounding box is detected.
///
protected override string gameObjectName => "ARBoundingBox";
BoundingBoxRaycaster m_BoundingBoxRaycaster;
///
/// Saves a reference to the XR Origin component and initializes the `BoundingBoxRaycaster`.
///
protected override void Awake()
{
base.Awake();
m_BoundingBoxRaycaster = new(trackables);
}
///
/// Attempt to retrieve an existing by .
///
/// The `TrackableId` of the bounding box to retrieve.
/// The output bounding box, if this method returns .
/// if a bounding box exists with the given . Otherwise, .
public bool TryGetBoundingBox(TrackableId trackableId, out ARBoundingBox arBoundingBox)
{
return m_Trackables.TryGetValue(trackableId, out arBoundingBox);
}
///
/// Performs a raycast against all currently tracked 3D bounding boxes.
///
/// The ray, in Unity world space, to cast.
/// A mask of raycast types to perform.
/// The Allocator to use when creating the returned NativeArray.
///
/// A new NativeArray of raycast results allocated with .
/// The caller owns the memory and is responsible for calling Dispose on the NativeArray.
///
public NativeArray Raycast(
Ray ray,
TrackableType trackableTypeMask,
Allocator allocator)
{
return m_BoundingBoxRaycaster.Raycast(ray, trackableTypeMask, allocator);
}
///
/// Get the prefab to instantiate for each detected 3D bounding box. Can be .
///
/// The Prefab which will be instantiated for each .
protected override GameObject GetPrefab() => m_BoundingBoxPrefab;
///
/// Invoked when Unity enables this `MonoBehaviour`. Used to register with the .
///
protected override void OnEnable()
{
base.OnEnable();
if (subsystem != null)
{
var raycastManager = GetComponent();
if (raycastManager != null)
raycastManager.RegisterRaycaster(this);
}
}
///
/// Invoked when this component is disabled. Used to unregister with the .
///
protected override void OnDisable()
{
base.OnDisable();
var raycastManager = GetComponent();
if (raycastManager != null)
raycastManager.UnregisterRaycaster(this);
}
}
}