2
0

SteamVR_Behaviour.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. internal static bool isPlaying = false;
  36. private static bool initializing = false;
  37. public static void Initialize(bool forceUnityVRToOpenVR = false)
  38. {
  39. if (_instance == null && initializing == false)
  40. {
  41. initializing = true;
  42. GameObject steamVRObject = null;
  43. if (forceUnityVRToOpenVR)
  44. forcingInitialization = true;
  45. SteamVR_Render renderInstance = GameObject.FindObjectOfType<SteamVR_Render>();
  46. if (renderInstance != null)
  47. steamVRObject = renderInstance.gameObject;
  48. SteamVR_Behaviour behaviourInstance = GameObject.FindObjectOfType<SteamVR_Behaviour>();
  49. if (behaviourInstance != null)
  50. steamVRObject = behaviourInstance.gameObject;
  51. if (steamVRObject == null)
  52. {
  53. GameObject objectInstance = new GameObject("[SteamVR]");
  54. _instance = objectInstance.AddComponent<SteamVR_Behaviour>();
  55. _instance.steamvr_render = objectInstance.AddComponent<SteamVR_Render>();
  56. }
  57. else
  58. {
  59. behaviourInstance = steamVRObject.GetComponent<SteamVR_Behaviour>();
  60. if (behaviourInstance == null)
  61. behaviourInstance = steamVRObject.AddComponent<SteamVR_Behaviour>();
  62. if (renderInstance != null)
  63. behaviourInstance.steamvr_render = renderInstance;
  64. else
  65. {
  66. behaviourInstance.steamvr_render = steamVRObject.GetComponent<SteamVR_Render>();
  67. if (behaviourInstance.steamvr_render == null)
  68. behaviourInstance.steamvr_render = steamVRObject.AddComponent<SteamVR_Render>();
  69. }
  70. _instance = behaviourInstance;
  71. }
  72. if (_instance != null && _instance.doNotDestroy)
  73. GameObject.DontDestroyOnLoad(_instance.transform.root.gameObject);
  74. initializing = false;
  75. }
  76. }
  77. protected void Awake()
  78. {
  79. isPlaying = true;
  80. if (initializeSteamVROnAwake && forcingInitialization == false)
  81. InitializeSteamVR();
  82. }
  83. public void InitializeSteamVR(bool forceUnityVRToOpenVR = false)
  84. {
  85. if (forceUnityVRToOpenVR)
  86. {
  87. forcingInitialization = true;
  88. if (initializeCoroutine != null)
  89. StopCoroutine(initializeCoroutine);
  90. if (XRSettings.loadedDeviceName == openVRDeviceName)
  91. EnableOpenVR();
  92. else
  93. initializeCoroutine = StartCoroutine(DoInitializeSteamVR(forceUnityVRToOpenVR));
  94. }
  95. else
  96. {
  97. SteamVR.Initialize(false);
  98. }
  99. }
  100. private Coroutine initializeCoroutine;
  101. #if UNITY_2018_3_OR_NEWER
  102. private bool loadedOpenVRDeviceSuccess = false;
  103. private IEnumerator DoInitializeSteamVR(bool forceUnityVRToOpenVR = false)
  104. {
  105. XRDevice.deviceLoaded += XRDevice_deviceLoaded;
  106. XRSettings.LoadDeviceByName(openVRDeviceName);
  107. while (loadedOpenVRDeviceSuccess == false)
  108. {
  109. yield return null;
  110. }
  111. XRDevice.deviceLoaded -= XRDevice_deviceLoaded;
  112. EnableOpenVR();
  113. }
  114. private void XRDevice_deviceLoaded(string deviceName)
  115. {
  116. if (deviceName == openVRDeviceName)
  117. {
  118. loadedOpenVRDeviceSuccess = true;
  119. }
  120. else
  121. {
  122. Debug.LogError("<b>[SteamVR]</b> Tried to async load: " + openVRDeviceName + ". Loaded: " + deviceName, this);
  123. loadedOpenVRDeviceSuccess = true; //try anyway
  124. }
  125. }
  126. #else
  127. private IEnumerator DoInitializeSteamVR(bool forceUnityVRToOpenVR = false)
  128. {
  129. XRSettings.LoadDeviceByName(openVRDeviceName);
  130. yield return null;
  131. EnableOpenVR();
  132. }
  133. #endif
  134. private void EnableOpenVR()
  135. {
  136. XRSettings.enabled = true;
  137. SteamVR.Initialize(false);
  138. initializeCoroutine = null;
  139. forcingInitialization = false;
  140. }
  141. #if UNITY_EDITOR
  142. //only stop playing if the unity editor is running
  143. private void OnDestroy()
  144. {
  145. isPlaying = false;
  146. }
  147. #endif
  148. #if UNITY_2017_1_OR_NEWER
  149. protected void OnEnable()
  150. {
  151. Application.onBeforeRender += OnBeforeRender;
  152. SteamVR_Events.System(EVREventType.VREvent_Quit).Listen(OnQuit);
  153. }
  154. protected void OnDisable()
  155. {
  156. Application.onBeforeRender -= OnBeforeRender;
  157. SteamVR_Events.System(EVREventType.VREvent_Quit).Remove(OnQuit);
  158. }
  159. protected void OnBeforeRender()
  160. {
  161. PreCull();
  162. }
  163. #else
  164. protected void OnEnable()
  165. {
  166. Camera.onPreCull += OnCameraPreCull;
  167. SteamVR_Events.System(EVREventType.VREvent_Quit).Listen(OnQuit);
  168. }
  169. protected void OnDisable()
  170. {
  171. Camera.onPreCull -= OnCameraPreCull;
  172. SteamVR_Events.System(EVREventType.VREvent_Quit).Remove(OnQuit);
  173. }
  174. protected void OnCameraPreCull(Camera cam)
  175. {
  176. if (!cam.stereoEnabled)
  177. return;
  178. PreCull();
  179. }
  180. #endif
  181. protected static int lastFrameCount = -1;
  182. protected void PreCull()
  183. {
  184. if (OpenVR.Input != null)
  185. {
  186. // Only update poses on the first camera per frame.
  187. if (Time.frameCount != lastFrameCount)
  188. {
  189. lastFrameCount = Time.frameCount;
  190. SteamVR_Input.OnPreCull();
  191. }
  192. }
  193. }
  194. protected void FixedUpdate()
  195. {
  196. if (OpenVR.Input != null)
  197. {
  198. SteamVR_Input.FixedUpdate();
  199. }
  200. }
  201. protected void LateUpdate()
  202. {
  203. if (OpenVR.Input != null)
  204. {
  205. SteamVR_Input.LateUpdate();
  206. }
  207. }
  208. protected void Update()
  209. {
  210. if (OpenVR.Input != null)
  211. {
  212. SteamVR_Input.Update();
  213. }
  214. }
  215. protected void OnQuit(VREvent_t vrEvent)
  216. {
  217. #if UNITY_EDITOR
  218. UnityEditor.EditorApplication.isPlaying = false;
  219. #else
  220. Application.Quit();
  221. #endif
  222. }
  223. }
  224. }