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