Configuring the camera

Camera configurations describe the properties of an app’s underlying camera sensor. In Unity, these configurations are accessible through XRCameraConfiguration.

On the Android platform, ARCore provides XRCameraConfigurationExtensions to expose additional ARCore-specific properties within XRCameraConfiguration. You can use these properties to set up the appropriate camera configuration for your app.

Extended camera configuration properties (Android)

The following extended properties are supported by ARCore on the Android platform.

Access supported camera configurations

Use ARCameraManager.GetConfigurations() to access the supported camera configurations for a given device. This returns a NativeArray containing multiple instances of XRCameraConfiguration. Each instance is an individual camera configuration specifying properties such as depth usage, target capture frame rate, resolution, and texture dimensions.

Configure the camera in your app's scene

Follow these steps to configure the camera in your app's scene.

  1. Use ARCameraManager with ARCameraManager.GetConfigurations() to query the list of supported XRCameraConfigurations.

  2. If you are building for Android, use any combination of the functions in XRCameraConfigurationExtensions to get ARCore-specific properties.

  3. Use cameraManager.currentConfiguration to set the current configuration.

using UnityEngine.XR.ARFoundation;


// Adds XRCameraConfigurationExtensions extension methods to XRCameraConfiguration.
// This is for the Android platform only.
using Google.XR.ARCoreExtensions;

// Must be set in the editor.
public ARCameraManager cameraManager;

// Use ARCameraManager to obtain the camera configurations.
using (NativeArray<XRCameraConfiguration> configurations = cameraManager.GetConfigurations(Allocator.Temp))
{
    if (!configurations.IsCreated || (configurations.Length <= 0))
    {
        return;
    }

    // Iterate through the list of returned configs to locate the config you want.
    var desiredConfig = configurations[0];
    for (int i = 1; i < configurations.Length; ++i)
    {
        // Choose a config for a given camera that uses the maximum
        // target FPS and texture dimension. If supported, this config also enables
        // the depth sensor.
        if (configurations[i].GetFPSRange().y > desiredConfig.GetFPSRange().y &&
            configurations[i].GetTextureDimensions().x > desiredConfig.GetTextureDimensions().x &&
            configurations[i].GetTextureDimensions().y > desiredConfig.GetTextureDimensions().y &&
            configurations[i].CameraConfigDepthSensorUsage() == CameraConfigDepthSensorUsage.RequireAndUse)
        {
            desiredConfig = configurations[i];
        }
    }

    // Set the configuration you want. If it succeeds, the session
    // automatically pauses and resumes to apply the new configuration.
    // If it fails, cameraManager.currentConfiguration throws an exception.
    if (desiredConfig != cameraManager.currentConfiguration)
    {
        cameraManager.currentConfiguration = desiredConfig;
    }
}

Camera config filters

You can use ARCoreExtensionsCameraConfigFilter to narrow down the available camera configs for a given device at runtime by filtering based on your app's needs.

Limit camera capture frame rate to 30 FPS

If your app does not need a faster camera frame rate, you can limit it to 30 FPS. On devices that support a 60 FPS camera frame rate, ARCore will prioritize camera configs that support that frame rate by default. To filter out all camera configs that support 60 FPS, make sure that Target Camera Framerate is set to Target 30FPS.

Prevent ARCore from using the depth sensor

If your app doesn’t require Depth, you can prevent ARCore from using the depth sensor. On devices that have a supported depth sensor, ARCore ARCore prioritizes camera configs that use the depth sensor. To filter out all camera configs that use the depth sensor, make sure that Depth Sensor Usage is set to Do Not Use.

Use camera config filters

Follow these steps to enable your app to filter camera configs.

Go to Assets > Create > XR > Camera Config Filter to create a new camera config filter.

Select the configs that you want your filter to use.

Once you have created the filter, use it in an ARCoreExtensions component.

Configure the camera during runtime

You can use the callback event ARCoreExtensions.OnChooseXRCameraConfiguration to configure the camera during runtime, based on factors such as device type.

// Unity's Awake() method
public void Awake()
{
    
    // If the return value is not a valid index (ex. the value if -1),
    // then no camera configuration will be set. If no previous selection exists, 
    // the ARCore session will use the previously selected camera configuration 
    // or a default configuration.
    arcoreExtensions.OnChooseXRCameraConfiguration = SelectCameraConfiguration;
    
}

// A custom camera configuration selection function
int SelectCameraConfiguration(List<XRCameraConfiguration> supportedConfigurations)
{
    int index = 0;

    // Use custom logic here to choose the desired configuration from supportedConfigurations.

    return index;
}