OpenXR native API
For users familiar with the OpenXR specification, the class OpenXRNativeApi contains a partial public C# port of OpenXR. You can use the OpenXR native API to write low-level OpenXR code if you prefer a lower-level abstraction.
Refer to the OpenXR specification for more detailed information about each extension.
Supported extensions
The OpenXR native API currently supports the following OpenXR extensions:
- XR_EXT_future
- XR_EXT_spatial_anchor
- XR_EXT_spatial_entity
- XR_EXT_spatial_marker_tracking
- XR_EXT_spatial_persistence
- XR_EXT_spatial_persistence_operations
- XR_EXT_spatial_plane_tracking
Enable extensions
The OpenXR native API is a static class, so C# allows you to call its methods at any time from any C# script. However, methods provided by an extension will return the XrResult.FunctionUnsupported
error code unless the required extension is enabled.
The OpenXR Plug-in supports enabling extensions via OpenXR features.
To enable the required extensions for the native API:
- Create a class that inherits
OpenXRFeature
. - Fill out the required information for the
OpenXRFeatureAttribute
constructor, including the extensions you wish to enable. - Implement OnInstanceCreate. Return
false
if any requirements to enable your feature are not met, such as if runtime failed to enable the required extensions. - Go to Project Settings > XR Plug-in Management and enable your feature in the OpenXR tab.
- At runtime, use
OpenXRSettings.Instance.GetFeature<T>
to get your feature. Iffeature != null && feature.enabled
, your app can use the native API methods provided by the enabled extensions.
Example code
The following example code demonstrates how to implement a simple OpenXR feature that enables XR_EXT_spatial_plane_tracking
and its required dependencies:
using UnityEngine.XR.OpenXR;
using UnityEngine.XR.OpenXR.Features;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.XR.OpenXR.Features;
#endif
#if UNITY_EDITOR
[OpenXRFeature(UiName = k_DisplayName,
BuildTargetGroups = new[]
{ BuildTargetGroup.Android, BuildTargetGroup.Standalone },
Company = "Your company name",
Desc = "Enables XR_EXT_spatial_plane_tracking and dependencies.",
DocumentationLink = "https://yourcompany.com/documentation/thisfeature.html",
OpenxrExtensionStrings = k_OpenXRRequestedExtensions,
Category = FeatureCategory.Feature,
FeatureId = "com.yourcompany.featurename",
Version = "0.1.0")]
#endif
class SpatialPlaneTrackingFeatureEXT : OpenXRFeature
{
const string k_DisplayName = "Spatial Entities EXT: Plane Tracking";
const string k_OpenXRRequestedExtensions =
"XR_EXT_future"
+ " XR_EXT_spatial_entity"
+ " XR_EXT_spatial_plane_tracking";
protected override bool OnInstanceCreate(ulong xrInstance)
{
return OpenXRRuntime.IsExtensionEnabled("XR_EXT_future")
&& OpenXRRuntime.IsExtensionEnabled("XR_EXT_spatial_entity")
&& OpenXRRuntime.IsExtensionEnabled("XR_EXT_spatial_plane_tracking");
}
}