Files
zumbi-game/Assets/Scripts/StartupHeadsetCheck.cs
T
2026-05-10 22:11:53 +02:00

72 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
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;
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
}
}