Android XR SDK 現已在開發人員預覽版中推出。敬請提供意見回饋!請前往
支援頁面與我們聯絡。
檢查空間功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
空間功能可能會隨著使用者與應用程式或系統互動而變更,甚至可能會由應用程式本身變更,例如移至首頁空間或全螢幕模式。為避免發生問題,應用程式需要檢查空間功能,以判斷目前環境支援哪些 API。
使用適用於 XR 的 Jetpack Compose 檢查空間功能
適用於 XR 的 Jetpack Compose 會建立 Composition Local,用於檢查空間功能。可用於檢查是否已啟用空間 UI、空間音訊、環境、透視或 3D 內容。
您可以使用 LocalSpatialCapabilities.current
檢查目前是否提供下列空間功能:
以下範例說明如何檢查是否已啟用空間 UI:
if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
Subspace {
SpatialPanel(
modifier = SubspaceModifier
.width(1488.dp)
.fillMaxHeight()
) {
AppContent()
}
}
} else {
AppContent()
}
使用 SceneCore 檢查空間功能
使用 SceneCore 程式庫時,您必須建立工作階段。建立工作階段後,請在工作階段上呼叫 spatialCapabilities
,查詢目前可用的空間功能。
您也可以選擇訂閱回呼 addSpatialCapabilitiesChangedListener
,在空間功能變更時通知您。
// Example 1: check if enabling passthrough mode is allowed
if (xrSession.scene.spatialCapabilities.hasCapability(
SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL
)
) {
xrSession.scene.spatialEnvironment.preferredPassthroughOpacity = 1f
}
// Example 2: multiple capability flags can be checked simultaneously:
if (xrSession.scene.spatialCapabilities.hasCapability(
SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL and
SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT
)
) {
// ...
}
另請參閱
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-23 (世界標準時間)。
[null,null,["上次更新時間:2025-08-23 (世界標準時間)。"],[],[],null,["# Check for spatial capabilities\n\nSpatial capabilities can change as users interact with your app or the system,\nor can even be changed by your app itself---for example, moving into Home Space or\nFull Space. To avoid issues, your app needs to check for spatial capabilities to\ndetermine which APIs are supported in the current environment.\n\nCheck for spatial capabilities using Jetpack Compose for XR\n-----------------------------------------------------------\n\nJetpack Compose for XR creates a Composition Local for checking spatial\ncapabilities. Use this to check whether spatial UI, spatial audio, environments,\npassthrough, or 3D content is enabled.\n\nYou can use [`LocalSpatialCapabilities.current`](/reference/kotlin/androidx/xr/compose/platform/package-summary#LocalSpatialCapabilities()) to check if the following\nspatial capabilities are currently available:\n\n- [`isSpatialUiEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isSpatialUiEnabled()): Indicates whether the application may create spatial UI elements (for example, `SpatialPanel`).\n- [`isContent3dEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isContent3dEnabled()): Indicates whether the application may create 3D objects.\n- [`isAppEnvironmentEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isAppEnvironmentEnabled()): Indicates whether the application may set the environment.\n- [`isPassthroughControlEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isPassthroughControlEnabled()): Indicates whether the application may control the passthrough state.\n- [`isSpatialAudioEnabled`](/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isSpatialAudioEnabled()): Indicates whether the application may use spatial audio.\n\nThe following example shows how to check if spatial UI is enabled:\n\n\n```kotlin\nif (LocalSpatialCapabilities.current.isSpatialUiEnabled) {\n Subspace {\n SpatialPanel(\n modifier = SubspaceModifier\n .width(1488.dp)\n .fillMaxHeight()\n ) {\n AppContent()\n }\n }\n} else {\n AppContent()\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/xr/src/main/java/com/example/xr/compose/SpatialCapabilities.kt#L51-L63\n```\n\n\u003cbr /\u003e\n\nCheck for spatial capabilities using SceneCore\n----------------------------------------------\n\nWhen using the SceneCore library, you'll have to create a [session](/develop/xr/jetpack-xr-sdk/add-session). Once the\nsession is created, call [`spatialCapabilities`](/reference/kotlin/androidx/xr/scenecore/Scene#spatialCapabilities()) on the session to query\nwhich spatial capabilities are currently available.\n\n- [`SPATIAL_CAPABILITY_3D_CONTENT`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_3D_CONTENT()): The activity can create 3D contents.\n- [`SPATIAL_CAPABILITY_APP_ENVIRONMENT`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_APP_ENVIRONMENT()): The activity can set its own environment.\n- [`SPATIAL_CAPABILITY_EMBED_ACTIVITY`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_EMBED_ACTIVITY()): The activity can spatially embed another activity.\n- [`SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL()): The activity can enable or disable passthrough.\n- [`SPATIAL_CAPABILITY_SPATIAL_AUDIO`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_SPATIAL_AUDIO()): The activity can use spatial audio.\n- [`SPATIAL_CAPABILITY_UI`](/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_UI()): The activity can spatialize itself (for example, adding a spatial panel).\n\nYou can also choose to subscribe to a callback,\n[`addSpatialCapabilitiesChangedListener`](/reference/kotlin/androidx/xr/scenecore/Scene#addSpatialCapabilitiesChangedListener(java.util.function.Consumer)) that notifies you when spatial\ncapabilities have changed.\n\n\n```kotlin\n// Example 1: check if enabling passthrough mode is allowed\nif (xrSession.scene.spatialCapabilities.hasCapability(\n SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL\n )\n) {\n xrSession.scene.spatialEnvironment.preferredPassthroughOpacity = 1f\n}\n// Example 2: multiple capability flags can be checked simultaneously:\nif (xrSession.scene.spatialCapabilities.hasCapability(\n SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL and\n SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT\n )\n) {\n // ...\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/xr/src/main/java/com/example/xr/scenecore/SpatialCapabilities.kt#L25-L39\n```\n\n\u003cbr /\u003e\n\nSee also\n--------\n\n- [Create a session](/develop/xr/jetpack-xr-sdk/check-spatial-capabilities)\n- [Transition between HSM and FSM](/develop/xr/jetpack-xr-sdk/transition-home-space-to-full-space)\n- [Add spatial environments to your app](/develop/xr/jetpack-xr-sdk/add-environments)\n- [Add 3D models to your app](/develop/xr/jetpack-xr-sdk/add-3d-models)"]]