docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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:

    1. Create a class that inherits OpenXRFeature.
    2. Fill out the required information for the OpenXRFeatureAttribute constructor, including the extensions you wish to enable.
    3. Implement OnInstanceCreate. Return false if any requirements to enable your feature are not met, such as if runtime failed to enable the required extensions.
    4. Go to Project Settings > XR Plug-in Management and enable your feature in the OpenXR tab.
    5. At runtime, use OpenXRSettings.Instance.GetFeature<T> to get your feature. If feature != 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");
        }
    }
    
    In This Article
    Back to top
    Copyright © 2025 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)