2
0

SteamVR_AutoEnableVR_2019plus.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Prompt developers to use settings most compatible with SteamVR.
  4. //
  5. //=============================================================================
  6. #if (UNITY_2019_1_OR_NEWER)
  7. using UnityEngine;
  8. using UnityEditor;
  9. using System.IO;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System;
  13. using System.Reflection;
  14. using Valve.VR.InteractionSystem;
  15. using UnityEditor.Callbacks;
  16. #if OPENVR_XR_API
  17. using UnityEditor.XR.Management.Metadata;
  18. using UnityEngine.XR.Management;
  19. using UnityEditor.XR.Management;
  20. #endif
  21. #pragma warning disable CS0618
  22. #pragma warning disable CS0219
  23. #pragma warning disable CS0414
  24. namespace Valve.VR
  25. {
  26. #if (UNITY_2019_1_OR_NEWER && !UNITY_2020_1_OR_NEWER)
  27. public class SteamVR_AutoEnableVR_2019to2020
  28. {
  29. [DidReloadScripts]
  30. private static void OnReload()
  31. {
  32. #if !OPENVR_XR_API
  33. //if we don't have xr installed, check to see if we have vr installed. if we don't have vr installed, ask which they do want to install.
  34. SteamVR_AutoEnableVR_2019.CheckAndAsk();
  35. #else
  36. //since we already have xr installed, we know we just want to enable it
  37. SteamVR_AutoEnableVR_UnityXR.EnableUnityXR();
  38. #endif
  39. }
  40. }
  41. #endif
  42. #if (UNITY_2020_1_OR_NEWER)
  43. public class SteamVR_AutoEnableVR_2020Plus
  44. {
  45. [DidReloadScripts]
  46. private static void OnReload()
  47. {
  48. #if !OPENVR_XR_API
  49. SteamVR_AutoEnableVR_UnityXR.InstallAndEnableUnityXR();
  50. #else
  51. //since we already have xr installed, we know we just want to enable it
  52. SteamVR_AutoEnableVR_UnityXR.EnableUnityXR();
  53. #endif
  54. }
  55. }
  56. #endif
  57. #if (UNITY_2019_1_OR_NEWER && !UNITY_2020_1_OR_NEWER)
  58. public class SteamVR_AutoEnableVR_2019
  59. {
  60. public static void CheckAndAsk()
  61. {
  62. EditorApplication.update += Update;
  63. }
  64. protected const string openVRString = "OpenVR";
  65. protected const string unityOpenVRPackageString = "com.unity.xr.openvr.standalone";
  66. protected const string valveOpenVRPackageString = "com.valvesoftware.unity.openvr";
  67. private enum PackageStates
  68. {
  69. None,
  70. WaitingForList,
  71. Complete,
  72. Failed,
  73. }
  74. private static UnityEditor.PackageManager.Requests.ListRequest listRequest;
  75. private static PackageStates packageState = PackageStates.None;
  76. private static void End()
  77. {
  78. packageState = PackageStates.None;
  79. UnityEditor.EditorUtility.ClearProgressBar();
  80. EditorApplication.update -= Update;
  81. }
  82. private static void ShowDialog()
  83. {
  84. int shouldInstall = UnityEditor.EditorUtility.DisplayDialogComplex("SteamVR", "The SteamVR Unity Plugin can be used with the legacy Unity VR API (Unity 5.4 - 2019) or with the Unity XR API (2019+). Would you like to install in legacy VR mode or for Unity XR?", "Legacy VR", "Cancel", "Unity XR");
  85. switch (shouldInstall)
  86. {
  87. case 0: //legacy vr
  88. SteamVR_AutoEnableVR_UnityPackage.InstallAndEnableUnityVR();
  89. break;
  90. case 1: //cancel
  91. break;
  92. case 2: //unity xr
  93. SteamVR_AutoEnableVR_UnityXR.InstallAndEnableUnityXR();
  94. break;
  95. }
  96. End();
  97. }
  98. public static void Update()
  99. {
  100. if (!SteamVR_Settings.instance.autoEnableVR || Application.isPlaying)
  101. End();
  102. if (UnityEditor.PlayerSettings.virtualRealitySupported == false)
  103. {
  104. ShowDialog();
  105. return;
  106. }
  107. switch (packageState)
  108. {
  109. case PackageStates.None:
  110. //see if we have the package
  111. listRequest = UnityEditor.PackageManager.Client.List(true);
  112. packageState = PackageStates.WaitingForList;
  113. break;
  114. case PackageStates.WaitingForList:
  115. if (listRequest.IsCompleted)
  116. {
  117. if (listRequest.Error != null || listRequest.Status == UnityEditor.PackageManager.StatusCode.Failure)
  118. {
  119. packageState = PackageStates.Failed;
  120. break;
  121. }
  122. string packageName = unityOpenVRPackageString;
  123. bool hasPackage = listRequest.Result.Any(package => package.name == packageName);
  124. if (hasPackage == false)
  125. ShowDialog();
  126. else //if we do have the package, do nothing
  127. End();
  128. }
  129. break;
  130. case PackageStates.Failed:
  131. End();
  132. string failtext = "The Unity Package Manager failed to verify the OpenVR package. If you were trying to install it you may need to open the Package Manager Window and try to install it manually.";
  133. UnityEditor.EditorUtility.DisplayDialog("SteamVR", failtext, "Ok");
  134. Debug.Log("<b>[SteamVR Setup]</b> " + failtext);
  135. break;
  136. }
  137. }
  138. }
  139. #endif
  140. // todo: split the below into an install and an enable section
  141. public class SteamVR_AutoEnableVR_UnityXR
  142. {
  143. public static void InstallAndEnableUnityXR()
  144. {
  145. StartXRInstaller();
  146. EditorApplication.update += Update;
  147. }
  148. public static void EnableUnityXR()
  149. {
  150. EditorApplication.update += Update;
  151. }
  152. protected const string openVRString = "OpenVR";
  153. protected const string unityOpenVRPackageString = "com.unity.xr.openvr.standalone";
  154. protected const string valveOpenVRPackageString = "com.valvesoftware.unity.openvr";
  155. private enum PackageStates
  156. {
  157. None,
  158. WaitingForList,
  159. WaitingForAdd,
  160. WaitingForAddConfirm,
  161. Installed,
  162. Failed,
  163. }
  164. private static UnityEditor.PackageManager.Requests.ListRequest listRequest;
  165. private static UnityEditor.PackageManager.Requests.AddRequest addRequest;
  166. private static PackageStates packageState = PackageStates.None;
  167. private static System.Diagnostics.Stopwatch addingPackageTime = new System.Diagnostics.Stopwatch();
  168. private static System.Diagnostics.Stopwatch addingPackageTimeTotal = new System.Diagnostics.Stopwatch();
  169. private static float estimatedTimeToInstall = 80;
  170. private static int addTryCount = 0;
  171. private static string enabledLoaderKey = null;
  172. private static MethodInfo isLoaderAssigned;
  173. private static MethodInfo installPackageAndAssignLoaderForBuildTarget;
  174. private static Type[] isLoaderAssignedMethodParameters;
  175. private static object[] isLoaderAssignedCallParameters;
  176. private static void End()
  177. {
  178. addingPackageTime.Stop();
  179. addingPackageTimeTotal.Stop();
  180. UnityEditor.EditorUtility.ClearProgressBar();
  181. EditorApplication.update -= Update;
  182. }
  183. public static void Update()
  184. {
  185. if (!SteamVR_Settings.instance.autoEnableVR)
  186. End();
  187. #if OPENVR_XR_API
  188. EnableLoader();
  189. #endif
  190. }
  191. #if OPENVR_XR_API
  192. private static EditorWindow settingsWindow = null;
  193. private static int skipEditorFrames = 5;
  194. public static void EnableLoader()
  195. {
  196. if (skipEditorFrames > 0)
  197. {
  198. skipEditorFrames--;
  199. return;
  200. }
  201. if (enabledLoaderKey == null)
  202. enabledLoaderKey = string.Format(valveEnabledLoaderKeyTemplate, SteamVR_Settings.instance.editorAppKey);
  203. if (EditorPrefs.HasKey(enabledLoaderKey) == false)
  204. {
  205. if (UnityEditor.PlayerSettings.virtualRealitySupported == true)
  206. {
  207. UnityEditor.PlayerSettings.virtualRealitySupported = false;
  208. Debug.Log("<b>[SteamVR Setup]</b> Disabled virtual reality support in Player Settings. <b>Because you're using XR Manager. Make sure OpenVR Loader is enabled in XR Manager UI.</b> (you can disable this by unchecking Assets/SteamVR/SteamVR_Settings.autoEnableVR)");
  209. }
  210. var generalSettings = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTargetGroup.Standalone);
  211. if (generalSettings == null)
  212. {
  213. if (settingsWindow == null)
  214. {
  215. settingsWindow = SettingsService.OpenProjectSettings("Project/XR Plug-in Management");
  216. settingsWindow.Repaint();
  217. return;
  218. }
  219. if (settingsWindow == null || generalSettings == null)
  220. {
  221. Debug.LogWarning("<b>[SteamVR Setup]</b> Unable to access standalone xr settings while trying to enable OpenVR Loader. <b>You may need to manually enable OpenVR Loader in XR Plug-in Management (Project Settings).</b> (you can disable this by unchecking Assets/SteamVR/SteamVR_Settings.autoEnableVR)");
  222. return;
  223. }
  224. }
  225. if (generalSettings.AssignedSettings == null)
  226. {
  227. var assignedSettings = ScriptableObject.CreateInstance<XRManagerSettings>() as XRManagerSettings;
  228. generalSettings.AssignedSettings = assignedSettings;
  229. EditorUtility.SetDirty(generalSettings);
  230. }
  231. bool existing = generalSettings.AssignedSettings.loaders.Any(loader => loader.name == valveOpenVRLoaderType);
  232. foreach (var loader in generalSettings.AssignedSettings.loaders)
  233. {
  234. Debug.Log("loader: " + loader.name);
  235. }
  236. if (!existing)
  237. {
  238. bool status = XRPackageMetadataStore.AssignLoader(generalSettings.AssignedSettings, valveOpenVRLoaderType, BuildTargetGroup.Standalone);
  239. if (status)
  240. Debug.Log("<b>[SteamVR Setup]</b> Enabled OpenVR Loader in XR Management");
  241. else
  242. Debug.LogError("<b>[SteamVR Setup]</b> Failed to enable enable OpenVR Loader in XR Management. You may need to manually open the XR Plug-in Management tab in project settings and check the OpenVR Loader box.");
  243. }
  244. EditorPrefs.SetBool(enabledLoaderKey, true);
  245. }
  246. End();
  247. }
  248. #endif
  249. protected const string valveEnabledLoaderKeyTemplate = "valve.enabledxrloader.{0}";
  250. protected const string valveOpenVRLoaderType = "Unity.XR.OpenVR.OpenVRLoader";
  251. private static void StartXRInstaller()
  252. {
  253. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  254. for (int assemblyIndex = 0; assemblyIndex < assemblies.Length; assemblyIndex++)
  255. {
  256. Assembly assembly = assemblies[assemblyIndex];
  257. Type type = assembly.GetType("Unity.XR.OpenVR.OpenVRPackageInstaller");
  258. if (type != null)
  259. {
  260. MethodInfo preinitMethodInfo = type.GetMethod("Start");
  261. if (preinitMethodInfo != null)
  262. {
  263. preinitMethodInfo.Invoke(null, new object[] { true });
  264. return;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. #endif