OpenVREvents.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using Valve.VR;
  6. namespace Unity.XR.OpenVR
  7. {
  8. public class OpenVREvent : UnityEvent<VREvent_t> { }
  9. public class OpenVREvents
  10. {
  11. private static OpenVREvents instance;
  12. //dictionaries are slow/allocate in mono for some reason. So we just allocate a bunch at the beginning.
  13. private OpenVREvent[] events;
  14. private int[] eventIndicies;
  15. private VREvent_t vrEvent;
  16. private uint vrEventSize;
  17. private bool preloadedEvents = false;
  18. private const int maxEventsPerUpdate = 64;
  19. private static bool debugLogAllEvents = false;
  20. private static bool enabled = true;
  21. public static void Initialize(bool lazyLoadEvents = false)
  22. {
  23. instance = new OpenVREvents(lazyLoadEvents);
  24. }
  25. public bool IsInitialized()
  26. {
  27. return instance != null;
  28. }
  29. public OpenVREvents(bool lazyLoadEvents = false)
  30. {
  31. if (OpenVRHelpers.IsUsingSteamVRInput())
  32. {
  33. enabled = false; //let the steamvr plugin handle events
  34. return;
  35. }
  36. instance = this;
  37. events = new OpenVREvent[(int)EVREventType.VREvent_VendorSpecific_Reserved_End];
  38. vrEvent = new VREvent_t();
  39. vrEventSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VREvent_t));
  40. if (lazyLoadEvents == false)
  41. {
  42. for (int eventIndex = 0; eventIndex < events.Length; eventIndex++)
  43. {
  44. events[eventIndex] = new OpenVREvent();
  45. }
  46. }
  47. else
  48. {
  49. preloadedEvents = true;
  50. }
  51. RegisterDefaultEvents();
  52. }
  53. public void RegisterDefaultEvents()
  54. {
  55. AddListener(EVREventType.VREvent_Quit, On_VREvent_Quit);
  56. }
  57. public static void AddListener(EVREventType eventType, UnityAction<VREvent_t> action, bool removeOtherListeners = false)
  58. {
  59. instance.Add(eventType, action, removeOtherListeners);
  60. }
  61. public void Add(EVREventType eventType, UnityAction<VREvent_t> action, bool removeOtherListeners = false)
  62. {
  63. if (!enabled)
  64. {
  65. Debug.LogError("[OpenVR XR Plugin] This events class is currently not enabled, please use SteamVR_Events instead.");
  66. return;
  67. }
  68. int eventIndex = (int)eventType;
  69. if (preloadedEvents == false && events[eventIndex] == null)
  70. {
  71. events[eventIndex] = new OpenVREvent();
  72. }
  73. if (removeOtherListeners)
  74. {
  75. events[eventIndex].RemoveAllListeners();
  76. }
  77. events[eventIndex].AddListener(action);
  78. }
  79. public static void RemoveListener(EVREventType eventType, UnityAction<VREvent_t> action)
  80. {
  81. instance.Remove(eventType, action);
  82. }
  83. public void Remove(EVREventType eventType, UnityAction<VREvent_t> action)
  84. {
  85. int eventIndex = (int)eventType;
  86. if (preloadedEvents || events[eventIndex] != null)
  87. {
  88. events[eventIndex].RemoveListener(action);
  89. }
  90. }
  91. public static void Update()
  92. {
  93. instance.PollEvents();
  94. }
  95. public void PollEvents()
  96. {
  97. if (Valve.VR.OpenVR.System != null && enabled)
  98. {
  99. for (int eventIndex = 0; eventIndex < maxEventsPerUpdate; eventIndex++)
  100. {
  101. if (Valve.VR.OpenVR.System == null || !Valve.VR.OpenVR.System.PollNextEvent(ref vrEvent, vrEventSize))
  102. break;
  103. int uEventType = (int)vrEvent.eventType;
  104. if (debugLogAllEvents)
  105. {
  106. EVREventType eventType = (EVREventType)uEventType;
  107. Debug.Log(string.Format("[{0}] {1}", Time.frameCount, eventType.ToString()));
  108. }
  109. if (events[uEventType] != null)
  110. {
  111. events[uEventType].Invoke(vrEvent);
  112. }
  113. }
  114. }
  115. }
  116. private bool exiting = false;
  117. #region DefaultEvents
  118. private void On_VREvent_Quit(VREvent_t pEvent)
  119. {
  120. if (exiting == true)
  121. {
  122. return;
  123. }
  124. exiting = true;
  125. if (Valve.VR.OpenVR.System != null)
  126. {
  127. Valve.VR.OpenVR.System.AcknowledgeQuit_Exiting();
  128. }
  129. #if UNITY_EDITOR
  130. Debug.Log("<b>[OpenVR]</b> Quit requested from OpenVR. Exiting application via EditorApplication.isPlaying = false");
  131. UnityEditor.EditorApplication.isPlaying = false;
  132. #else
  133. Debug.Log("<b>[OpenVR]</b> Quit requested from OpenVR. Exiting application via Application.Quit");
  134. Application.Quit();
  135. #endif
  136. }
  137. #endregion
  138. }
  139. }