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