PlatformUnderstanding.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. #if OPENXR_1_6_OR_NEWER
  3. using UnityEngine.XR.OpenXR;
  4. #endif
  5. namespace UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets
  6. {
  7. /// <summary>
  8. /// Enumeration of supported XR Platforms for OpenXR.
  9. /// </summary>
  10. public enum XRPlatformType
  11. {
  12. /// <summary>
  13. /// Meta Quest devices supported through OpenXR.
  14. /// </summary>
  15. [InspectorName("OpenXR: Meta")]
  16. OpenXRMeta,
  17. /// <summary>
  18. /// Android XR devices supported through OpenXR.
  19. /// </summary>
  20. [InspectorName("OpenXR: Android XR")]
  21. OpenXRAndroidXR,
  22. /// <summary>
  23. /// Other OpenXR devices.
  24. /// </summary>
  25. [InspectorName("OpenXR: Other")]
  26. OpenXROther,
  27. /// <summary>
  28. /// Other device that does not support OpenXR or not running on an OpenXR runtime.
  29. /// </summary>
  30. Other,
  31. }
  32. /// <summary>
  33. /// Helper class that determines the current XR platform based on the active runtime.
  34. /// Currently, this only supports OpenXR Runtimes from Meta and Google.
  35. /// </summary>
  36. public static class XRPlatformUnderstanding
  37. {
  38. const string k_RuntimeNameMeta = "Oculus";
  39. const string k_RuntimeNameAndroidXR = "Android XR";
  40. /// <summary>
  41. /// The current platform based on the OpenXR Runtime name.
  42. /// </summary>
  43. public static XRPlatformType CurrentPlatform
  44. {
  45. get
  46. {
  47. if (!s_Initialized)
  48. {
  49. s_CurrentPlatform = GetCurrentXRPlatform();
  50. s_Initialized = true;
  51. }
  52. return s_CurrentPlatform;
  53. }
  54. }
  55. static XRPlatformType s_CurrentPlatform = XRPlatformType.Other;
  56. static bool s_Initialized;
  57. /// <summary>
  58. /// Returns the current platform based on the active OpenXR Runtime name.
  59. /// </summary>
  60. /// <returns>The current platform based on the active OpenXR Runtime name.</returns>
  61. static XRPlatformType GetCurrentXRPlatform()
  62. {
  63. // If we have already initialized, just return the current platform
  64. if (s_Initialized)
  65. return s_CurrentPlatform;
  66. #if OPENXR_1_6_OR_NEWER
  67. try
  68. {
  69. var openXRRuntimeName = OpenXRRuntime.name;
  70. if (string.IsNullOrEmpty(openXRRuntimeName))
  71. {
  72. s_CurrentPlatform = XRPlatformType.Other;
  73. }
  74. else
  75. {
  76. switch (openXRRuntimeName)
  77. {
  78. case k_RuntimeNameMeta:
  79. Debug.Log("Meta runtime detected.");
  80. s_CurrentPlatform = XRPlatformType.OpenXRMeta;
  81. break;
  82. case k_RuntimeNameAndroidXR:
  83. Debug.Log("Android XR runtime detected.");
  84. s_CurrentPlatform = XRPlatformType.OpenXRAndroidXR;
  85. break;
  86. default:
  87. Debug.Log($"Unknown OpenXR runtime detected: \"{openXRRuntimeName}\"");
  88. s_CurrentPlatform = XRPlatformType.OpenXROther;
  89. break;
  90. }
  91. }
  92. }
  93. catch (Exception e)
  94. {
  95. Debug.LogWarning($"Failed to get OpenXR runtime: {e.Message}");
  96. s_CurrentPlatform = XRPlatformType.Other;
  97. }
  98. #else
  99. s_CurrentPlatform = XRPlatformType.Other;
  100. #endif
  101. s_Initialized = true;
  102. return s_CurrentPlatform;
  103. }
  104. }
  105. }