OpenXRFeatureBase.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEngine.XR.OpenXR;
  6. using UnityEngine.XR.OpenXR.Features;
  7. namespace Unity.XR.OpenXR.Features.PICOSupport
  8. {
  9. public abstract class OpenXRFeatureBase : OpenXRFeature
  10. {
  11. protected static ulong xrInstance = 0ul;
  12. protected static ulong xrSession = 0ul;
  13. protected string extensionUrl = "";
  14. public bool _isExtensionEnable = false;
  15. protected override bool OnInstanceCreate(ulong instance)
  16. {
  17. extensionUrl = GetExtensionString();
  18. _isExtensionEnable = isExtensionEnabled();
  19. if (!_isExtensionEnable)
  20. {
  21. return false;
  22. }
  23. xrInstance = instance;
  24. xrSession = 0ul;
  25. Initialize(xrGetInstanceProcAddr);
  26. return true;
  27. }
  28. #if UNITY_EDITOR
  29. protected override void GetValidationChecks(List<ValidationRule> rules, BuildTargetGroup targetGroup)
  30. {
  31. var settings = OpenXRSettings.GetSettingsForBuildTargetGroup(targetGroup);
  32. rules.Add(new ValidationRule(this)
  33. {
  34. message = "No PICO OpenXR Features selected.",
  35. checkPredicate = () =>
  36. {
  37. if (null == settings)
  38. return false;
  39. foreach (var feature in settings.GetFeatures<OpenXRFeature>())
  40. {
  41. if (feature is OpenXRExtensions)
  42. {
  43. return feature.enabled;
  44. }
  45. }
  46. return false;
  47. },
  48. fixIt = () =>
  49. {
  50. if (null == settings)
  51. return ;
  52. var openXRExtensions = settings.GetFeature<OpenXRExtensions>();
  53. if (openXRExtensions != null)
  54. {
  55. openXRExtensions.enabled = true;
  56. }
  57. },
  58. error = true
  59. });
  60. }
  61. #endif
  62. public bool isExtensionEnabled()
  63. {
  64. string[] exts = extensionUrl.Split(' ');
  65. if (exts.Length>0)
  66. {
  67. foreach (var _ext in exts)
  68. {
  69. if (!string.IsNullOrEmpty(_ext) && !OpenXRRuntime.IsExtensionEnabled(_ext))
  70. {
  71. PLog.e(_ext + " is not enabled");
  72. return false;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. if (!string.IsNullOrEmpty(extensionUrl) && !OpenXRRuntime.IsExtensionEnabled(extensionUrl))
  79. {
  80. PLog.e(extensionUrl + " is not enabled");
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. protected override void OnSessionCreate(ulong xrSessionId)
  87. {
  88. xrSession = xrSessionId;
  89. base.OnSessionCreate(xrSession);
  90. SessionCreate();
  91. }
  92. protected override void OnInstanceDestroy(ulong xrInstance)
  93. {
  94. base.OnInstanceDestroy(xrInstance);
  95. xrInstance = 0ul;
  96. }
  97. protected override void OnSessionDestroy(ulong xrSessionId)
  98. {
  99. base.OnSessionDestroy(xrSessionId);
  100. xrSession = 0ul;
  101. }
  102. public virtual void Initialize(IntPtr intPtr)
  103. {
  104. }
  105. public abstract string GetExtensionString();
  106. public virtual void SessionCreate()
  107. {
  108. }
  109. public static bool IsSuccess(XrResult result) => result == 0;
  110. }
  111. }