2
0

SteamVR_AutoEnableVR_2018to2019.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Prompt developers to use settings most compatible with SteamVR.
  4. //
  5. //=============================================================================
  6. //2019 will use some of this
  7. #if (UNITY_2018_1_OR_NEWER && !UNITY_2020_1_OR_NEWER)
  8. using UnityEngine;
  9. using UnityEditor;
  10. using System.IO;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System;
  14. using System.Reflection;
  15. using Valve.VR.InteractionSystem;
  16. using UnityEditor.Callbacks;
  17. #pragma warning disable CS0618
  18. #pragma warning disable CS0219
  19. #pragma warning disable CS0414
  20. namespace Valve.VR
  21. {
  22. #if (UNITY_2018_1_OR_NEWER && !UNITY_2019_1_OR_NEWER)
  23. public class SteamVR_AutoEnableVR_2018to2019
  24. {
  25. [DidReloadScripts]
  26. private static void OnReload()
  27. {
  28. SteamVR_AutoEnableVR_UnityPackage.InstallAndEnableUnityVR();
  29. }
  30. }
  31. #endif
  32. public class SteamVR_AutoEnableVR_UnityPackage
  33. {
  34. private static bool? _forceInstall;
  35. private const string forceInstallKey = "steamvr.autoenablevr.forceInstall";
  36. private static bool? _forceEnable;
  37. private const string forceEnableKey = "steamvr.autoenablevr.forceEnable";
  38. private static PackageStates? _updateState;
  39. private const string updateStateKey = "steamvr.autoenablevr.updateState";
  40. private static bool forceInstall
  41. {
  42. get
  43. {
  44. if (_forceInstall.HasValue == false)
  45. {
  46. if (EditorPrefs.HasKey(forceInstallKey))
  47. _forceInstall = EditorPrefs.GetBool(forceInstallKey);
  48. else
  49. _forceInstall = false;
  50. }
  51. return _forceInstall.Value;
  52. }
  53. set
  54. {
  55. _forceInstall = value;
  56. EditorPrefs.SetBool(forceInstallKey, value);
  57. }
  58. }
  59. private static bool forceEnable
  60. {
  61. get
  62. {
  63. if (_forceEnable.HasValue == false)
  64. {
  65. if (EditorPrefs.HasKey(forceEnableKey))
  66. _forceEnable = EditorPrefs.GetBool(forceEnableKey);
  67. else
  68. _forceEnable = false;
  69. }
  70. return _forceEnable.Value;
  71. }
  72. set
  73. {
  74. _forceEnable = value;
  75. EditorPrefs.SetBool(forceEnableKey, value);
  76. }
  77. }
  78. private static void UpdateUpdateStateFromPrefs()
  79. {
  80. if (_updateState.HasValue == false)
  81. {
  82. if (EditorPrefs.HasKey(updateStateKey))
  83. _updateState = (PackageStates)EditorPrefs.GetInt(updateStateKey);
  84. else
  85. _updateState = PackageStates.None;
  86. }
  87. }
  88. private static PackageStates updateState
  89. {
  90. get
  91. {
  92. if (_updateState.HasValue == false)
  93. UpdateUpdateStateFromPrefs();
  94. return _updateState.Value;
  95. }
  96. set
  97. {
  98. _updateState = value;
  99. EditorPrefs.SetInt(updateStateKey, (int)value);
  100. }
  101. }
  102. public static void InstallAndEnableUnityVR(bool forceInstall = false, bool forceEnable = false)
  103. {
  104. _forceInstall = forceInstall;
  105. _forceEnable = forceEnable;
  106. EditorApplication.update += Update;
  107. }
  108. protected const string openVRString = "OpenVR";
  109. protected const string unityOpenVRPackageString = "com.unity.xr.openvr.standalone";
  110. protected const string valveOpenVRPackageString = "com.valvesoftware.unity.openvr";
  111. private enum PackageStates
  112. {
  113. None,
  114. WaitingForList,
  115. WaitingForAdd,
  116. WaitingForAddConfirm,
  117. Installed,
  118. Failed,
  119. }
  120. private static UnityEditor.PackageManager.Requests.ListRequest listRequest;
  121. private static UnityEditor.PackageManager.Requests.AddRequest addRequest;
  122. private static System.Diagnostics.Stopwatch addingPackageTime = new System.Diagnostics.Stopwatch();
  123. private static System.Diagnostics.Stopwatch addingPackageTimeTotal = new System.Diagnostics.Stopwatch();
  124. private static float estimatedTimeToInstall = 80;
  125. private static int addTryCount = 0;
  126. private static void End()
  127. {
  128. updateState = PackageStates.None;
  129. addingPackageTime.Stop();
  130. addingPackageTimeTotal.Stop();
  131. UnityEditor.EditorUtility.ClearProgressBar();
  132. EditorApplication.update -= Update;
  133. }
  134. public static void Update()
  135. {
  136. if (!SteamVR_Settings.instance.autoEnableVR || Application.isPlaying)
  137. End();
  138. if (UnityEditor.PlayerSettings.virtualRealitySupported == false)
  139. {
  140. if (forceInstall == false)
  141. {
  142. int shouldInstall = UnityEditor.EditorUtility.DisplayDialogComplex("SteamVR", "Would you like to enable Virtual Reality mode?\n\nThis will install the OpenVR for Desktop package and enable it in Player Settings.", "Yes", "No, and don't ask again", "No");
  143. switch (shouldInstall)
  144. {
  145. case 0: //yes
  146. UnityEditor.PlayerSettings.virtualRealitySupported = true;
  147. break;
  148. case 1: //no
  149. End();
  150. return;
  151. case 2: //no, don't ask
  152. SteamVR_Settings.instance.autoEnableVR = false;
  153. SteamVR_Settings.Save();
  154. End();
  155. return;
  156. }
  157. }
  158. Debug.Log("<b>[SteamVR Setup]</b> Enabled virtual reality support in Player Settings. (you can disable this by unchecking Assets/SteamVR/SteamVR_Settings.autoEnableVR)");
  159. }
  160. switch (updateState)
  161. {
  162. case PackageStates.None:
  163. //see if we have the package
  164. listRequest = UnityEditor.PackageManager.Client.List(true);
  165. updateState = PackageStates.WaitingForList;
  166. break;
  167. case PackageStates.WaitingForList:
  168. if (listRequest == null)
  169. {
  170. listRequest = UnityEditor.PackageManager.Client.List(true);
  171. updateState = PackageStates.WaitingForList;
  172. }
  173. else if (listRequest.IsCompleted)
  174. {
  175. if (listRequest.Error != null || listRequest.Status == UnityEditor.PackageManager.StatusCode.Failure)
  176. {
  177. updateState = PackageStates.Failed;
  178. break;
  179. }
  180. string packageName = unityOpenVRPackageString;
  181. bool hasPackage = listRequest.Result.Any(package => package.name == packageName);
  182. if (hasPackage == false)
  183. {
  184. //if we don't have the package - then install it
  185. addRequest = UnityEditor.PackageManager.Client.Add(packageName);
  186. updateState = PackageStates.WaitingForAdd;
  187. addTryCount++;
  188. Debug.Log("<b>[SteamVR Setup]</b> Installing OpenVR package...");
  189. addingPackageTime.Start();
  190. addingPackageTimeTotal.Start();
  191. }
  192. else
  193. {
  194. //if we do have the package, make sure it's enabled.
  195. updateState = PackageStates.Installed; //already installed
  196. }
  197. }
  198. break;
  199. case PackageStates.WaitingForAdd:
  200. if (addRequest.IsCompleted)
  201. {
  202. if (addRequest.Error != null || addRequest.Status == UnityEditor.PackageManager.StatusCode.Failure)
  203. {
  204. updateState = PackageStates.Failed;
  205. break;
  206. }
  207. else
  208. {
  209. //if the package manager says we added it then confirm that with the list
  210. listRequest = UnityEditor.PackageManager.Client.List(true);
  211. updateState = PackageStates.WaitingForAddConfirm;
  212. }
  213. }
  214. else
  215. {
  216. if (addingPackageTimeTotal.Elapsed.TotalSeconds > estimatedTimeToInstall)
  217. {
  218. if (addTryCount == 1)
  219. estimatedTimeToInstall *= 2; //give us more time to retry
  220. else
  221. updateState = PackageStates.Failed;
  222. }
  223. string dialogText;
  224. if (addTryCount == 1)
  225. dialogText = "Installing OpenVR from Unity Package Manager...";
  226. else
  227. dialogText = "Retrying OpenVR install from Unity Package Manager...";
  228. bool cancel = UnityEditor.EditorUtility.DisplayCancelableProgressBar("SteamVR", dialogText, (float)addingPackageTimeTotal.Elapsed.TotalSeconds / estimatedTimeToInstall);
  229. if (cancel)
  230. updateState = PackageStates.Failed;
  231. if (addingPackageTime.Elapsed.TotalSeconds > 10)
  232. {
  233. Debug.Log("<b>[SteamVR Setup]</b> Waiting for package manager to install OpenVR package...");
  234. addingPackageTime.Stop();
  235. addingPackageTime.Reset();
  236. addingPackageTime.Start();
  237. }
  238. }
  239. break;
  240. case PackageStates.WaitingForAddConfirm:
  241. if (listRequest.IsCompleted)
  242. {
  243. if (listRequest.Error != null)
  244. {
  245. updateState = PackageStates.Failed;
  246. break;
  247. }
  248. string packageName = unityOpenVRPackageString;
  249. bool hasPackage = listRequest.Result.Any(package => package.name == packageName);
  250. if (hasPackage == false)
  251. {
  252. if (addTryCount == 1)
  253. {
  254. addRequest = UnityEditor.PackageManager.Client.Add(packageName);
  255. updateState = PackageStates.WaitingForAdd;
  256. addTryCount++;
  257. Debug.Log("<b>[SteamVR Setup]</b> Retrying OpenVR package install...");
  258. }
  259. else
  260. {
  261. updateState = PackageStates.Failed;
  262. }
  263. }
  264. else
  265. {
  266. updateState = PackageStates.Installed; //installed successfully
  267. Debug.Log("<b>[SteamVR Setup]</b> Successfully installed OpenVR Desktop package.");
  268. }
  269. }
  270. break;
  271. case PackageStates.Installed:
  272. UnityEditor.BuildTargetGroup currentTarget = UnityEditor.EditorUserBuildSettings.selectedBuildTargetGroup;
  273. string[] devices = UnityEditorInternal.VR.VREditor.GetVREnabledDevicesOnTargetGroup(currentTarget);
  274. bool hasOpenVR = false;
  275. bool isFirst = false;
  276. if (devices.Length != 0)
  277. {
  278. int index = Array.FindIndex(devices, device => string.Equals(device, openVRString, System.StringComparison.CurrentCultureIgnoreCase));
  279. hasOpenVR = index != -1;
  280. isFirst = index == 0;
  281. }
  282. //list openvr as the first option if it was in the list already
  283. List<string> devicesList = new List<string>(devices);
  284. if (isFirst == false)
  285. {
  286. if (hasOpenVR == true)
  287. devicesList.Remove(openVRString);
  288. devicesList.Insert(0, openVRString);
  289. string[] newDevices = devicesList.ToArray();
  290. UnityEditorInternal.VR.VREditor.SetVREnabledDevicesOnTargetGroup(currentTarget, newDevices);
  291. Debug.Log("<b>[SteamVR Setup]</b> Added OpenVR to supported VR SDKs list.");
  292. }
  293. End();
  294. break;
  295. case PackageStates.Failed:
  296. End();
  297. string failtext = "The Unity Package Manager failed to automatically install the OpenVR Desktop package. Please open the Package Manager Window and try to install it manually.";
  298. UnityEditor.EditorUtility.DisplayDialog("SteamVR", failtext, "Ok");
  299. Debug.Log("<b>[SteamVR Setup]</b> " + failtext);
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. #endif