SteamVR_Behaviour.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. #if UNITY_2017_2_OR_NEWER
  8. using UnityEngine.XR;
  9. #else
  10. using XRSettings = UnityEngine.VR.VRSettings;
  11. using XRDevice = UnityEngine.VR.VRDevice;
  12. #endif
  13. namespace Valve.VR
  14. {
  15. public class SteamVR_Behaviour : MonoBehaviour
  16. {
  17. private const string openVRDeviceName = "OpenVR";
  18. public static bool forcingInitialization = false;
  19. private static SteamVR_Behaviour _instance;
  20. public static SteamVR_Behaviour instance
  21. {
  22. get
  23. {
  24. if (_instance == null)
  25. {
  26. Initialize(false);
  27. }
  28. return _instance;
  29. }
  30. }
  31. public bool initializeSteamVROnAwake = true;
  32. public bool doNotDestroy = true;
  33. [HideInInspector]
  34. public SteamVR_Render steamvr_render;
  35. private static bool initializing = false;
  36. public static void Initialize(bool forceUnityVRToOpenVR = false)
  37. {
  38. if (_instance == null && initializing == false)
  39. {
  40. initializing = true;
  41. GameObject steamVRObject = null;
  42. if (forceUnityVRToOpenVR)
  43. forcingInitialization = true;
  44. SteamVR_Render renderInstance = GameObject.FindObjectOfType<SteamVR_Render>();
  45. if (renderInstance != null)
  46. steamVRObject = renderInstance.gameObject;
  47. SteamVR_Behaviour behaviourInstance = GameObject.FindObjectOfType<SteamVR_Behaviour>();
  48. if (behaviourInstance != null)
  49. steamVRObject = behaviourInstance.gameObject;
  50. if (steamVRObject == null)
  51. {
  52. GameObject objectInstance = new GameObject("[SteamVR]");
  53. _instance = objectInstance.AddComponent<SteamVR_Behaviour>();
  54. _instance.steamvr_render = objectInstance.AddComponent<SteamVR_Render>();
  55. }
  56. else
  57. {
  58. behaviourInstance = steamVRObject.GetComponent<SteamVR_Behaviour>();
  59. if (behaviourInstance == null)
  60. behaviourInstance = steamVRObject.AddComponent<SteamVR_Behaviour>();
  61. if (renderInstance != null)
  62. behaviourInstance.steamvr_render = renderInstance;
  63. else
  64. {
  65. behaviourInstance.steamvr_render = steamVRObject.GetComponent<SteamVR_Render>();
  66. if (behaviourInstance.steamvr_render == null)
  67. behaviourInstance.steamvr_render = steamVRObject.AddComponent<SteamVR_Render>();
  68. }
  69. _instance = behaviourInstance;
  70. }
  71. if (behaviourInstance != null && behaviourInstance.doNotDestroy)
  72. GameObject.DontDestroyOnLoad(behaviourInstance.transform.root.gameObject);
  73. initializing = false;
  74. }
  75. }
  76. protected void Awake()
  77. {
  78. if (initializeSteamVROnAwake && forcingInitialization == false)
  79. InitializeSteamVR();
  80. }
  81. public void InitializeSteamVR(bool forceUnityVRToOpenVR = false)
  82. {
  83. if (forceUnityVRToOpenVR)
  84. {
  85. forcingInitialization = true;
  86. if (initializeCoroutine != null)
  87. StopCoroutine(initializeCoroutine);
  88. if (XRSettings.loadedDeviceName == openVRDeviceName)
  89. EnableOpenVR();
  90. else
  91. initializeCoroutine = StartCoroutine(DoInitializeSteamVR(forceUnityVRToOpenVR));
  92. }
  93. else
  94. {
  95. SteamVR.Initialize(false);
  96. }
  97. }
  98. private Coroutine initializeCoroutine;
  99. #if UNITY_2018_3_OR_NEWER
  100. private bool loadedOpenVRDeviceSuccess = false;
  101. private IEnumerator DoInitializeSteamVR(bool forceUnityVRToOpenVR = false)
  102. {
  103. XRDevice.deviceLoaded += XRDevice_deviceLoaded;
  104. XRSettings.LoadDeviceByName(openVRDeviceName);
  105. while (loadedOpenVRDeviceSuccess == false)
  106. {
  107. yield return null;
  108. }
  109. XRDevice.deviceLoaded -= XRDevice_deviceLoaded;
  110. EnableOpenVR();
  111. }
  112. private void XRDevice_deviceLoaded(string deviceName)
  113. {
  114. if (deviceName == openVRDeviceName)
  115. {
  116. loadedOpenVRDeviceSuccess = true;
  117. }
  118. else
  119. {
  120. Debug.LogError("<b>[SteamVR]</b> Tried to async load: " + openVRDeviceName + ". Loaded: " + deviceName);
  121. loadedOpenVRDeviceSuccess = true; //try anyway
  122. }
  123. }
  124. #else
  125. private IEnumerator DoInitializeSteamVR(bool forceUnityVRToOpenVR = false)
  126. {
  127. XRSettings.LoadDeviceByName(openVRDeviceName);
  128. yield return null;
  129. EnableOpenVR();
  130. }
  131. #endif
  132. private void EnableOpenVR()
  133. {
  134. XRSettings.enabled = true;
  135. SteamVR.Initialize(false);
  136. initializeCoroutine = null;
  137. forcingInitialization = false;
  138. }
  139. #if UNITY_2017_1_OR_NEWER
  140. protected void OnEnable()
  141. {
  142. Application.onBeforeRender += OnBeforeRender;
  143. SteamVR_Events.System(EVREventType.VREvent_Quit).Listen(OnQuit);
  144. }
  145. protected void OnDisable()
  146. {
  147. Application.onBeforeRender -= OnBeforeRender;
  148. SteamVR_Events.System(EVREventType.VREvent_Quit).Remove(OnQuit);
  149. }
  150. protected void OnBeforeRender()
  151. {
  152. PreCull();
  153. }
  154. #else
  155. protected void OnEnable()
  156. {
  157. Camera.onPreCull += OnCameraPreCull;
  158. SteamVR_Events.System(EVREventType.VREvent_Quit).Listen(OnQuit);
  159. }
  160. protected void OnDisable()
  161. {
  162. Camera.onPreCull -= OnCameraPreCull;
  163. SteamVR_Events.System(EVREventType.VREvent_Quit).Remove(OnQuit);
  164. }
  165. protected void OnCameraPreCull(Camera cam)
  166. {
  167. if (!cam.stereoEnabled)
  168. return;
  169. PreCull();
  170. }
  171. #endif
  172. protected static int lastFrameCount = -1;
  173. protected void PreCull()
  174. {
  175. // Only update poses on the first camera per frame.
  176. if (Time.frameCount != lastFrameCount)
  177. {
  178. lastFrameCount = Time.frameCount;
  179. SteamVR_Input.OnPreCull();
  180. }
  181. }
  182. protected void FixedUpdate()
  183. {
  184. SteamVR_Input.FixedUpdate();
  185. }
  186. protected void LateUpdate()
  187. {
  188. SteamVR_Input.LateUpdate();
  189. }
  190. protected void Update()
  191. {
  192. SteamVR_Input.Update();
  193. }
  194. protected void OnQuit(VREvent_t vrEvent)
  195. {
  196. #if UNITY_EDITOR
  197. UnityEditor.EditorApplication.isPlaying = false;
  198. #else
  199. Application.Quit();
  200. #endif
  201. }
  202. }
  203. }