docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    AR Bounding Box Manager component

    The ARBoundingBoxManager component is a type of trackable manager that detects and tracks 3D bounding boxes in the physical environment. As a trackable manager, it creates GameObjects in your scene for each detected 3D bounding box.

    AR Bounding Box Manager component
    AR Bounding Box Manager component

    Property Description
    trackablesChanged Invoked when trackables have changed (been added, updated, or removed).
    Bounding Box Prefab If not null, this prefab is instantiated for each detected 3D bounding box. If the prefab does not contain an AR Bounding Box component, ARBoundingBoxManager will add one.

    Getting started

    Add an AR Bounding Box Manager component to your XR Origin GameObject to enable bounding box tracking in your app. If your scene does not contain an XR Origin GameObject, first follow the Scene setup instructions.

    Whenever your app doesn't need bounding box detection functionality, disable the AR Bounding Box Manager component to improve app performance. If the user's device does not support bounding box detection, the AR Bounding Box Manager component will disable itself during OnEnable.

    Respond to detected bounding boxes

    While enabled, the AR Bounding Box Manager component will get changes reported by the XRBoundingBoxSubsystem every frame. If any bounding boxes were added, updated, or removed, the trackablesChanged event is invoked with the relevant information.

    You can subscribe to trackablesChanged using either the Inspector window or C# scripting.

    Use the Inspector

    1. Create a public method on a MonoBehaviour or ScriptableObject with a single parameter of type ARTrackablesChangedEventArgs<ARBoundingBox>, as shown in the example code below:
    public void OnTrackablesChanged(ARTrackablesChangedEventArgs<ARBoundingBox> changes)
    {
        foreach (var boundingBox in changes.added)
        {
            // handle added bounding boxes
        }
    
        foreach (var boundingBox in changes.updated)
        {
            // handle updated bounding boxes
        }
    
        foreach (var boundingBox in changes.removed)
        {
            // handle removed bounding boxes
        }
    }
    
    1. Select your XR Origin GameObject, then click the Add (+) button on the AR Bounding Box Manager component's trackablesChanged property.

    2. Using the Object picker (⊙), select either a GameObject that contains an instance of your component or an instance of your ScriptableObject, whichever is applicable.

    ARBoundingBoxManager's trackablesChanged event is shown in the Inspector with a subscribed MonoBehaviour
    Subscribe to the trackablesChanged event

    1. In the dropdown, select your class name and the name of your method. The method name appears in the Dynamic section of the methods list.

    Use C# scripting

    1. Create a public method with a single parameter of type ARTrackablesChangedEventArgs<ARBoundingBox> as shown in step 1a above.

    2. Use the example code below to subscribe to the trackablesChanged event:

    void SubscribeToBoundingBoxesChanged()
    {
        // This is inefficient. You should re-use a saved reference instead.
        var manager = Object.FindAnyObjectByType<ARBoundingBoxManager>();
    
        manager.trackablesChanged.AddListener(OnTrackablesChanged);
    }
    

    Visualize bounding boxes in the scene

    By default, the AR Bounding Box Manager does not render a textured mesh in the scene when 3D bounding boxes are detected. To enable bounding box visualization, set a prefab as the AR Bounding Box Manager's Bounding Box Prefab.

    The AR Foundation Samples GitHub repository contains a prefab that you could use to get started:

    Prefab Description
    AR Bounding Box Debug Visualizer Visualize bounding box meshes with a solid color, and optionally visualize additional information such as the bounding box's trackableId, trackingState, classifications, and orientation. You can configure the visualization options via the Inspector.

    Custom bounding box visualizers

    You can also create your own custom bounding box visualizer for use in your bounding box prefab. Some common ways to visualize a bounding box in your scene include using a MeshFilter and MeshRenderer, or MeshCollider.

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)