diff --git a/Assets/Scripts/StartupHeadsetCheck.cs b/Assets/Scripts/StartupHeadsetCheck.cs new file mode 100644 index 0000000..311885e --- /dev/null +++ b/Assets/Scripts/StartupHeadsetCheck.cs @@ -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(); + 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 + } +} diff --git a/Assets/Scripts/StartupHeadsetCheck.cs.meta b/Assets/Scripts/StartupHeadsetCheck.cs.meta new file mode 100644 index 0000000..ad9c9b3 --- /dev/null +++ b/Assets/Scripts/StartupHeadsetCheck.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 3b9f3cb88b161274baaeee6f00477851 \ No newline at end of file