usingUnityEngine.XR.ARFoundation;// Adds XRCameraConfigurationExtensions extension methods to XRCameraConfiguration.// This is for the Android platform only.usingGoogle.XR.ARCoreExtensions;// Must be set in the editor.publicARCameraManagercameraManager;// 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.vardesiredConfig=configurations[0];for(inti=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;}}
// Unity's Awake() methodpublicvoidAwake(){…// 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 functionintSelectCameraConfiguration(List<XRCameraConfiguration>supportedConfigurations){intindex=0;// Use custom logic here to choose the desired configuration from supportedConfigurations.returnindex;}
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eAR Foundation's camera configurations, utilizing ARCore Extensions, allow developers to control properties like depth sensor usage and frame rate.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can access and set camera configurations using \u003ccode\u003eARCameraManager\u003c/code\u003e and \u003ccode\u003eXRCameraConfiguration\u003c/code\u003e to optimize their AR experiences.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eARCoreExtensionsCameraConfigFilter\u003c/code\u003e enables filtering camera configurations based on specific needs, such as limiting frame rate or depth sensor usage.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can dynamically configure the camera during runtime via the \u003ccode\u003eARCoreExtensions.OnChooseXRCameraConfiguration\u003c/code\u003e callback, tailoring settings to device capabilities or user preferences.\u003c/p\u003e\n"]]],[],null,["# Configuring the camera\n\n| **Important:** The AR Foundation camera configurations require the **ARCore Extensions for AR Foundation** . Make sure that you have Extensions [installed and configured](/ar/develop/unity-arf/getting-started-extensions) before proceeding.\n\nCamera configurations describe the properties of an app's underlying camera\nsensor. In Unity, these configurations are accessible through\n[`XRCameraConfiguration`](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@3.1/api/UnityEngine.XR.ARSubsystems.XRCameraConfiguration).\n\nOn the Android platform, ARCore provides\n[`XRCameraConfigurationExtensions`](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/XRCameraConfigurationExtensions) to\nexpose additional ARCore-specific properties within\n[`XRCameraConfiguration`](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@3.1/api/UnityEngine.XR.ARSubsystems.XRCameraConfiguration). You\ncan use these properties to set up the appropriate camera configuration for your\napp.\n\nExtended camera configuration properties (Android)\n--------------------------------------------------\n\nThe following extended properties are supported by ARCore on the Android\nplatform.\n\n- [Depth sensor usage](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/XRCameraConfigurationExtensions#getdepthsensorusage)\n- [Range for target camera capture frame rate](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/XRCameraConfigurationExtensions#getfpsrange)\n- [The dimensions of the GPU-accessible external texture](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/XRCameraConfigurationExtensions#gettexturedimensions)\n- [Camera facing direction](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/XRCameraConfigurationExtensions#getfacingdirection)\n\nAccess supported camera configurations\n--------------------------------------\n\nUse [`ARCameraManager.GetConfigurations()`](https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.0/api/UnityEngine.XR.ARFoundation.ARCameraManager.html#UnityEngine_XR_ARFoundation_ARCameraManager_GetConfigurations_Allocator_)\nto access the supported camera configurations for a given device. This returns a\n[`NativeArray`](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.html) containing multiple\ninstances of\n[`XRCameraConfiguration`](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@3.1/api/UnityEngine.XR.ARSubsystems.XRCameraConfiguration). Each\ninstance is an individual camera configuration specifying properties such as\ndepth usage, target capture frame rate, resolution, and texture dimensions.\n| **Note:** Do not copy or store the instances of [`XRCameraConfiguration`](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@3.1/api/UnityEngine.XR.ARSubsystems.XRCameraConfiguration) from the [`NativeArray`](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.html) for later use. They become invalid after underlying native resources are released.\n\nConfigure the camera in your app's scene\n----------------------------------------\n\nFollow these steps to configure the camera in your app's scene.\n\n1. Use [`ARCameraManager`](https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@4.2/api/UnityEngine.XR.ARFoundation.ARCameraManager) with\n [`ARCameraManager.GetConfigurations()`](https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.0/api/UnityEngine.XR.ARFoundation.ARCameraManager.html#UnityEngine_XR_ARFoundation_ARCameraManager_GetConfigurations_Allocator_)\n to query the list of supported\n [`XRCameraConfiguration`](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@3.1/api/UnityEngine.XR.ARSubsystems.XRCameraConfiguration)s.\n\n2. If you are building for Android, use any combination of the functions in\n [`XRCameraConfigurationExtensions`](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/XRCameraConfigurationExtensions)\n to get ARCore-specific properties.\n\n3. Use [`cameraManager.currentConfiguration`](https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.0/api/UnityEngine.XR.ARFoundation.ARCameraManager.html#UnityEngine_XR_ARFoundation_ARCameraManager_currentConfiguration)\n to set the current configuration.\n\n using UnityEngine.XR.ARFoundation;\n\n\n // Adds XRCameraConfigurationExtensions extension methods to XRCameraConfiguration.\n // This is for the Android platform only.\n using Google.XR.ARCoreExtensions;\n\n // Must be set in the editor.\n public ARCameraManager cameraManager;\n\n // Use ARCameraManager to obtain the camera configurations.\n using (NativeArray\u003cXRCameraConfiguration\u003e configurations = cameraManager.GetConfigurations(Allocator.Temp))\n {\n if (!configurations.IsCreated || (configurations.Length \u003c= 0))\n {\n return;\n }\n\n // Iterate through the list of returned configs to locate the config you want.\n var desiredConfig = configurations[0];\n for (int i = 1; i \u003c configurations.Length; ++i)\n {\n // Choose a config for a given camera that uses the maximum\n // target FPS and texture dimension. If supported, this config also enables\n // the depth sensor.\n if (configurations[i].GetFPSRange().y \u003e desiredConfig.GetFPSRange().y &&\n configurations[i].GetTextureDimensions().x \u003e desiredConfig.GetTextureDimensions().x &&\n configurations[i].GetTextureDimensions().y \u003e desiredConfig.GetTextureDimensions().y &&\n configurations[i].CameraConfigDepthSensorUsage() == CameraConfigDepthSensorUsage.RequireAndUse)\n {\n desiredConfig = configurations[i];\n }\n }\n\n // Set the configuration you want. If it succeeds, the session\n // automatically pauses and resumes to apply the new configuration.\n // If it fails, cameraManager.currentConfiguration throws an exception.\n if (desiredConfig != cameraManager.currentConfiguration)\n {\n cameraManager.currentConfiguration = desiredConfig;\n }\n }\n\nCamera config filters\n---------------------\n\nYou can use\n[`ARCoreExtensionsCameraConfigFilter`](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ARCoreExtensionsCameraConfigFilter)\nto narrow down the available camera configs for a given device at runtime by\nfiltering based on your app's needs.\n\n### Limit camera capture frame rate to 30 FPS\n\nIf your app does not need a faster camera frame rate, you can limit it to 30\nFPS. On devices that support a 60 FPS camera frame rate, ARCore will prioritize\ncamera configs that support that frame rate by default. To filter out all camera\nconfigs that support 60 FPS, make sure that **Target Camera Framerate** is set\nto **Target 30FPS**.\n\n### Prevent ARCore from using the depth sensor\n\nIf your app doesn't require Depth, you can prevent ARCore from using the depth\nsensor. On devices that have a supported depth sensor, ARCore ARCore prioritizes\ncamera configs that use the depth sensor. To filter out all camera configs that\nuse the depth sensor, make sure that **Depth Sensor Usage** is set to\n**Do Not Use**.\n\n\u003cbr /\u003e\n\nUse camera config filters\n-------------------------\n\nFollow these steps to enable your app to filter camera configs.\n\nGo to **Assets \\\u003e Create \\\u003e XR \\\u003e Camera Config Filter**\nto create a new camera config filter.\n\nSelect the configs that you want your filter to use.\n\nOnce you have created the filter, use it in an **ARCoreExtensions** component.\n\nConfigure the camera during runtime\n-----------------------------------\n\nYou can use the callback event\n[`ARCoreExtensions.OnChooseXRCameraConfiguration`](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ARCoreExtensions#onchoosexrcameraconfiguration)\nto configure the camera during runtime, based on factors such as device type. \n\n // Unity's Awake() method\n public void Awake()\n {\n ...\n // If the return value is not a valid index (ex. the value if -1),\n // then no camera configuration will be set. If no previous selection exists, \n // the ARCore session will use the previously selected camera configuration \n // or a default configuration.\n arcoreExtensions.OnChooseXRCameraConfiguration = SelectCameraConfiguration;\n ...\n }\n\n // A custom camera configuration selection function\n int SelectCameraConfiguration(List\u003cXRCameraConfiguration\u003e supportedConfigurations)\n {\n int index = 0;\n\n // Use custom logic here to choose the desired configuration from supportedConfigurations.\n\n return index;\n }"]]