add script to check if VR headset is connected

This commit is contained in:
2026-05-10 22:03:45 +02:00
parent 80738d1fbb
commit 686045a7b1
2 changed files with 71 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.Management;
public class StartupHeadsetCheck : MonoBehaviour
{
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int MessageBox(System.IntPtr hWnd, string text, string caption, uint type);
#endif
private IEnumerator Start()
{
// Let OpenXR/XR Management initialize.
yield return null;
yield return null;
yield return null;
if (!IsOpenXRRunning())
{
ShowWindowsErrorMessage();
Application.Quit();
}
}
private bool IsOpenXRRunning()
{
XRManagerSettings xrManager = XRGeneralSettings.Instance?.Manager;
if (xrManager == null || xrManager.activeLoader == null)
{
return false;
}
var displays = new List<XRDisplaySubsystem>();
SubsystemManager.GetSubsystems(displays);
foreach (XRDisplaySubsystem display in displays)
{
if (display.running)
{
return true;
}
}
return false;
}
private void ShowWindowsErrorMessage()
{
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
MessageBox(
System.IntPtr.Zero,
"No VR headset detected.\n\n" +
"Please connect your headset and make sure your OpenXR runtime is active.\n\n" +
"SteamVR headset: start SteamVR.\n" +
"Meta Quest Link/Air Link: start Meta Quest Link.\n" +
"WMR headset: start Mixed Reality Portal.\n\n" +
"Then restart the game.",
"VR Headset Not Detected",
0x10 // MB_ICONERROR
);
#else
Debug.LogError("No VR headset detected.");
#endif
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3b9f3cb88b161274baaeee6f00477851