2
0

SteamVR_AutoEnableVR_5.4to2018.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_5_4_OR_NEWER && !UNITY_2018_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 UnityEditor.Callbacks;
  15. namespace Valve.VR
  16. {
  17. public class SteamVR_AutoEnableVR_54to2018
  18. {
  19. [DidReloadScripts]
  20. private static void OnReload()
  21. {
  22. EditorApplication.update += Update;
  23. }
  24. protected const string openVRString = "OpenVR";
  25. private static void End()
  26. {
  27. EditorApplication.update -= Update;
  28. }
  29. public static void Update()
  30. {
  31. if (!SteamVR_Settings.instance.autoEnableVR || Application.isPlaying)
  32. End();
  33. bool enabledVR = false;
  34. int shouldInstall = -1;
  35. if (UnityEditor.PlayerSettings.virtualRealitySupported == false)
  36. {
  37. shouldInstall = UnityEditor.EditorUtility.DisplayDialogComplex("SteamVR", "Would you like to enable Virtual Reality mode?\n\nThis will enable Virtual Reality in Player Settings and add OpenVR as a target.", "Yes", "No, and don't ask again", "No");
  38. switch (shouldInstall)
  39. {
  40. case 0: //yes
  41. UnityEditor.PlayerSettings.virtualRealitySupported = true;
  42. break;
  43. case 1: //no:
  44. UnityEditor.EditorApplication.update -= Update;
  45. return;
  46. case 2: //no, don't ask
  47. SteamVR_Settings.instance.autoEnableVR = false;
  48. SteamVR_Settings.Save();
  49. UnityEditor.EditorApplication.update -= Update;
  50. return;
  51. }
  52. }
  53. UnityEditor.BuildTargetGroup currentTarget = UnityEditor.EditorUserBuildSettings.selectedBuildTargetGroup;
  54. #if UNITY_5_6_OR_NEWER
  55. string[] devices = UnityEditorInternal.VR.VREditor.GetVREnabledDevicesOnTargetGroup(currentTarget);
  56. #else
  57. string[] devices = UnityEditorInternal.VR.VREditor.GetVREnabledDevices(currentTarget);
  58. #endif
  59. bool hasOpenVR = devices.Any(device => string.Equals(device, openVRString, System.StringComparison.CurrentCultureIgnoreCase));
  60. if (hasOpenVR == false || enabledVR)
  61. {
  62. string[] newDevices;
  63. if (enabledVR && hasOpenVR == false)
  64. {
  65. newDevices = new string[] { openVRString }; //only list openvr if we enabled it
  66. }
  67. else
  68. {
  69. List<string> devicesList = new List<string>(devices); //list openvr as the first option if it wasn't in the list.
  70. if (hasOpenVR)
  71. devicesList.Remove(openVRString);
  72. devicesList.Insert(0, openVRString);
  73. newDevices = devicesList.ToArray();
  74. }
  75. int shouldEnable = -1;
  76. if (shouldInstall == 0)
  77. shouldEnable = 0;
  78. else
  79. shouldEnable = UnityEditor.EditorUtility.DisplayDialogComplex("SteamVR", "Would you like to enable OpenVR as a VR target?", "Yes", "No, and don't ask again", "No");
  80. switch (shouldEnable)
  81. {
  82. case 0: //yes
  83. #if UNITY_5_6_OR_NEWER
  84. UnityEditorInternal.VR.VREditor.SetVREnabledDevicesOnTargetGroup(currentTarget, newDevices);
  85. #else
  86. UnityEditorInternal.VR.VREditor.SetVREnabledDevices(currentTarget, newDevices);
  87. #endif
  88. Debug.Log("<b>[SteamVR Setup]</b> Added OpenVR to supported VR SDKs list.");
  89. break;
  90. case 1: //no:
  91. break;
  92. case 2: //no, don't ask
  93. SteamVR_Settings.instance.autoEnableVR = false;
  94. SteamVR_Settings.Save();
  95. break;
  96. }
  97. }
  98. UnityEditor.EditorApplication.update -= Update;
  99. }
  100. }
  101. }
  102. #endif