Skip to main content
Version: 21 Aug 2024

Platform Detection

This section includes details on detecting if an application is running on a Magic Leap 2 and if a developer is targeting Magic Leap in Unity's XR Manager.

Runtime Detection

The following script checks if the application is being run on a Magic Leap by checking the device's architecture and device's Model Info.

Why use SystemInfo.deviceModel instead of SystemInfo.deviceName?

When identifying the device in Unity, it is recomended to use SystemInfo.deviceModel instead of SystemInfo.deviceName. Unity's SystemInfo.deviceName relies on system settings accessible only to the primary user on Android devices. For secondary users, this may result in an <unknown> value. For reliable identification, use SystemInfo.deviceModel which directly queries the device's hardware information.

using UnityEngine;

public class RuntimeDetectionExample : MonoBehaviour
{
void Start()
{
if (GetArchitecture() == "x86_64 &&" && SystemInfo.deviceModel == "Magic Leap Magic Leap 2")
{
Debug.Log("Device is most likely a Magic Leap 2");
}
}

public static string GetArchitecture()
{
using var system = new AndroidJavaClass("java.lang.System");
return system.CallStatic<string>("getProperty", "os.arch");
}
}

In-Editor Detection

The sample script below checks if the user is building for Android and if the Magic Leap Feature is enabled under the OpenXR Features in the project's XR Manager settings.

using UnityEngine;
using UnityEngine.XR.Management;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.XR.Management;
[ExecuteInEditMode]
#endif
public class EditorDetectionExample : MonoBehaviour
{
private const string MAGIC_LEAP_FEATURE_ID = "MagicLeapFeature";

void Update()
{
#if UNITY_EDITOR
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android && IsMagicLeapFeatureEnabled())
{
Debug.Log("Developer is most likely developing for Magic Leap 2");
}
#endif
}

public static bool IsMagicLeapFeatureEnabled()
{
bool hasMagicLeapFeature = false;
#if UNITY_EDITOR
// Get the active OpenXR settings for the build target
var settings = OpenXRSettings.ActiveBuildTargetInstance;

// Check if the settings are not null
if (settings != null)
{
// Loop through the feature groups
foreach (var featureGroup in settings.GetFeatures())
{
// Check if the feature group is the Magic Leap feature group and enabled
if (featureGroup.name == "MagicLeapFeature" && featureGroup.enabled)
{
// Set the flag to true
hasMagicLeapFeature = true;
}
}
}
#endif
return hasMagicLeapFeature;
}
}