SteamVR_AutoEnableVR.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Prompt developers to use settings most compatible with SteamVR.
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEditor;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. namespace Valve.VR
  12. {
  13. [InitializeOnLoad]
  14. public class SteamVR_AutoEnableVR
  15. {
  16. static SteamVR_AutoEnableVR()
  17. {
  18. EditorApplication.update += Update;
  19. }
  20. protected const string openVRString = "OpenVR";
  21. protected const string openVRPackageString = "com.unity.xr.openvr.standalone";
  22. #if UNITY_2018_1_OR_NEWER
  23. private enum PackageStates
  24. {
  25. None,
  26. WaitingForList,
  27. WaitingForAdd,
  28. WaitingForAddConfirm,
  29. Installed,
  30. Failed,
  31. }
  32. private static UnityEditor.PackageManager.Requests.ListRequest listRequest;
  33. private static UnityEditor.PackageManager.Requests.AddRequest addRequest;
  34. private static PackageStates packageState = PackageStates.None;
  35. private static System.Diagnostics.Stopwatch addingPackageTime = new System.Diagnostics.Stopwatch();
  36. private static System.Diagnostics.Stopwatch addingPackageTimeTotal = new System.Diagnostics.Stopwatch();
  37. private static float estimatedTimeToInstall = 80;
  38. private static int addTryCount = 0;
  39. #endif
  40. public static void Update()
  41. {
  42. if (SteamVR_Settings.instance.autoEnableVR)
  43. {
  44. bool enabledVR = false;
  45. if (UnityEditor.PlayerSettings.virtualRealitySupported == false)
  46. {
  47. UnityEditor.PlayerSettings.virtualRealitySupported = true;
  48. enabledVR = true;
  49. Debug.Log("<b>[SteamVR Setup]</b> Enabled virtual reality support in Player Settings. (you can disable this by unchecking Assets/SteamVR/SteamVR_Settings.autoEnableVR)");
  50. }
  51. UnityEditor.BuildTargetGroup currentTarget = UnityEditor.EditorUserBuildSettings.selectedBuildTargetGroup;
  52. #if (UNITY_5_4 || UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0)
  53. string[] devices = UnityEditorInternal.VR.VREditor.GetVREnabledDevices(currentTarget);
  54. #else
  55. string[] devices = UnityEditorInternal.VR.VREditor.GetVREnabledDevicesOnTargetGroup(currentTarget);
  56. #endif
  57. bool hasOpenVR = devices.Any(device => string.Equals(device, openVRString, System.StringComparison.CurrentCultureIgnoreCase));
  58. if (hasOpenVR == false || enabledVR)
  59. {
  60. string[] newDevices;
  61. if (enabledVR && hasOpenVR == false)
  62. {
  63. newDevices = new string[] { openVRString }; //only list openvr if we enabled it
  64. }
  65. else
  66. {
  67. List<string> devicesList = new List<string>(devices); //list openvr as the first option if it wasn't in the list.
  68. if (hasOpenVR)
  69. devicesList.Remove(openVRString);
  70. devicesList.Insert(0, openVRString);
  71. newDevices = devicesList.ToArray();
  72. }
  73. #if (UNITY_5_6 || UNITY_5_4 || UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0)
  74. UnityEditorInternal.VR.VREditor.SetVREnabledDevices(currentTarget, newDevices);
  75. #else
  76. UnityEditorInternal.VR.VREditor.SetVREnabledDevicesOnTargetGroup(currentTarget, newDevices);
  77. #endif
  78. Debug.Log("<b>[SteamVR Setup]</b> Added OpenVR to supported VR SDKs list.");
  79. }
  80. #if UNITY_2018_1_OR_NEWER
  81. //2018+ requires us to manually add the OpenVR package
  82. switch (packageState)
  83. {
  84. case PackageStates.None:
  85. //see if we have the package
  86. listRequest = UnityEditor.PackageManager.Client.List(true);
  87. packageState = PackageStates.WaitingForList;
  88. break;
  89. case PackageStates.WaitingForList:
  90. if (listRequest.IsCompleted)
  91. {
  92. if (listRequest.Error != null || listRequest.Status == UnityEditor.PackageManager.StatusCode.Failure)
  93. {
  94. packageState = PackageStates.Failed;
  95. break;
  96. }
  97. bool hasPackage = listRequest.Result.Any(package => package.name == openVRPackageString);
  98. if (hasPackage == false)
  99. {
  100. //if we don't have the package - then install it
  101. addRequest = UnityEditor.PackageManager.Client.Add(openVRPackageString);
  102. packageState = PackageStates.WaitingForAdd;
  103. addTryCount++;
  104. Debug.Log("<b>[SteamVR Setup]</b> Installing OpenVR package...");
  105. addingPackageTime.Start();
  106. addingPackageTimeTotal.Start();
  107. }
  108. else
  109. {
  110. //if we do have the package do nothing
  111. packageState = PackageStates.Installed; //already installed
  112. }
  113. }
  114. break;
  115. case PackageStates.WaitingForAdd:
  116. if (addRequest.IsCompleted)
  117. {
  118. if (addRequest.Error != null || addRequest.Status == UnityEditor.PackageManager.StatusCode.Failure)
  119. {
  120. packageState = PackageStates.Failed;
  121. break;
  122. }
  123. else
  124. {
  125. //if the package manager says we added it then confirm that with the list
  126. listRequest = UnityEditor.PackageManager.Client.List(true);
  127. packageState = PackageStates.WaitingForAddConfirm;
  128. }
  129. }
  130. else
  131. {
  132. if (addingPackageTimeTotal.Elapsed.TotalSeconds > estimatedTimeToInstall)
  133. estimatedTimeToInstall *= 2; // :)
  134. string dialogText;
  135. if (addTryCount == 1)
  136. dialogText = "Installing OpenVR from Unity Package Manager...";
  137. else
  138. dialogText = "Retrying OpenVR install from Unity Package Manager...";
  139. bool cancel = UnityEditor.EditorUtility.DisplayCancelableProgressBar("SteamVR", dialogText, (float)addingPackageTimeTotal.Elapsed.TotalSeconds / estimatedTimeToInstall);
  140. if (cancel)
  141. packageState = PackageStates.Failed;
  142. if (addingPackageTime.Elapsed.TotalSeconds > 10)
  143. {
  144. Debug.Log("<b>[SteamVR Setup]</b> Waiting for package manager to install OpenVR package...");
  145. addingPackageTime.Stop();
  146. addingPackageTime.Reset();
  147. addingPackageTime.Start();
  148. }
  149. }
  150. break;
  151. case PackageStates.WaitingForAddConfirm:
  152. if (listRequest.IsCompleted)
  153. {
  154. if (listRequest.Error != null)
  155. {
  156. packageState = PackageStates.Failed;
  157. break;
  158. }
  159. bool hasPackage = listRequest.Result.Any(package => package.name == openVRPackageString);
  160. if (hasPackage == false)
  161. {
  162. if (addTryCount == 1)
  163. {
  164. addRequest = UnityEditor.PackageManager.Client.Add(openVRPackageString);
  165. packageState = PackageStates.WaitingForAdd;
  166. addTryCount++;
  167. Debug.Log("<b>[SteamVR Setup]</b> Retrying OpenVR package install...");
  168. }
  169. else
  170. {
  171. packageState = PackageStates.Failed;
  172. }
  173. }
  174. else
  175. {
  176. packageState = PackageStates.Installed; //installed successfully
  177. Debug.Log("<b>[SteamVR Setup]</b> Successfully installed OpenVR package.");
  178. }
  179. }
  180. break;
  181. }
  182. if (packageState == PackageStates.Failed || packageState == PackageStates.Installed)
  183. {
  184. addingPackageTime.Stop();
  185. addingPackageTimeTotal.Stop();
  186. UnityEditor.EditorUtility.ClearProgressBar();
  187. UnityEditor.EditorApplication.update -= Update; //we're done trying to auto-enable vr
  188. if (packageState == PackageStates.Failed)
  189. {
  190. string failtext = "The Unity Package Manager failed to automatically install the OpenVR package. Please open the Package Manager Window and try to install it manually.";
  191. UnityEditor.EditorUtility.DisplayDialog("SteamVR", failtext, "Ok");
  192. Debug.Log("<b>[SteamVR Setup]</b> " + failtext);
  193. }
  194. }
  195. #else
  196. UnityEditor.EditorApplication.update -= Update;
  197. #endif
  198. }
  199. }
  200. }
  201. }