99using System . IO ;
1010using System . Collections . Generic ;
1111using System . Linq ;
12+ using System ;
13+ using System . Reflection ;
14+
15+ using Valve . VR . InteractionSystem ;
16+
17+ #if OPENVR_XR_API
18+ using UnityEditor . XR . Management . Metadata ;
19+ using UnityEngine . XR . Management ;
20+ #endif
21+
22+ #if UNITY_2018_2_OR_NEWER
23+ #pragma warning disable CS0618
24+ #pragma warning disable CS0219
25+ #pragma warning disable CS0414
26+ #endif
1227
1328namespace Valve . VR
1429{
@@ -21,7 +36,13 @@ static SteamVR_AutoEnableVR()
2136 }
2237
2338 protected const string openVRString = "OpenVR" ;
24- protected const string openVRPackageString = "com.unity.xr.openvr.standalone" ;
39+ protected const string unityOpenVRPackageString = "com.unity.xr.openvr.standalone" ;
40+ protected const string valveOpenVRPackageString = "com.valvesoftware.unity.openvr" ;
41+
42+ #if UNITY_2020_1_OR_NEWER || OPENVR_XR_API
43+ protected const string valveEnabledLoaderKeyTemplate = "valve.enablexddrloader.{0}" ;
44+ protected const string valveOpenVRLoaderType = "Unity.XR.OpenVR.OpenVRLoader" ;
45+ #endif
2546
2647#if UNITY_2018_2_OR_NEWER
2748 private enum PackageStates
@@ -41,6 +62,14 @@ private enum PackageStates
4162 private static System . Diagnostics . Stopwatch addingPackageTimeTotal = new System . Diagnostics . Stopwatch ( ) ;
4263 private static float estimatedTimeToInstall = 80 ;
4364 private static int addTryCount = 0 ;
65+
66+ private static string enabledLoaderKey = null ;
67+
68+ private static MethodInfo isLoaderAssigned ;
69+ private static MethodInfo installPackageAndAssignLoaderForBuildTarget ;
70+
71+ private static Type [ ] isLoaderAssignedMethodParameters ;
72+ private static object [ ] isLoaderAssignedCallParameters ;
4473#endif
4574
4675 public static void Update ( )
@@ -49,6 +78,67 @@ public static void Update()
4978 {
5079 bool enabledVR = false ;
5180
81+ #if UNITY_2020_1_OR_NEWER || OPENVR_XR_API
82+ #if ! UNITY_2020_2_OR_NEWER
83+ if ( UnityEditor . PlayerSettings . virtualRealitySupported == true )
84+ {
85+ UnityEditor . PlayerSettings . virtualRealitySupported = false ;
86+ enabledVR = true ;
87+ Debug . Log ( "<b>[SteamVR Setup]</b> Disabled virtual reality support in Player Settings. <b>Because you're using XR Manager. Make sure OpenVR Loader is enabled in XR Manager UI.</b> (you can disable this by unchecking Assets/SteamVR/SteamVR_Settings.autoEnableVR)" ) ;
88+ }
89+ #endif
90+
91+ #if OPENVR_XR_API
92+ //little hacky, but the public methods weren't working.
93+
94+ if ( isLoaderAssignedMethodParameters == null )
95+ isLoaderAssignedMethodParameters = new Type [ ] { typeof ( string ) , typeof ( BuildTargetGroup ) } ;
96+ if ( isLoaderAssignedCallParameters == null )
97+ isLoaderAssignedCallParameters = new object [ ] { valveOpenVRLoaderType , BuildTargetGroup . Standalone } ;
98+ if ( isLoaderAssigned == null )
99+ isLoaderAssigned = GetMethod ( "IsLoaderAssigned" , isLoaderAssignedMethodParameters ) ;
100+
101+ if ( installPackageAndAssignLoaderForBuildTarget == null )
102+ installPackageAndAssignLoaderForBuildTarget = GetMethod ( "InstallPackageAndAssignLoaderForBuildTarget" ) ;
103+
104+ if ( isLoaderAssigned != null && installPackageAndAssignLoaderForBuildTarget != null )
105+ {
106+ bool isAssigned = ( bool ) isLoaderAssigned . Invoke ( null , isLoaderAssignedCallParameters ) ;
107+
108+ if ( isAssigned == false )
109+ {
110+ if ( enabledLoaderKey == null )
111+ enabledLoaderKey = string . Format ( valveEnabledLoaderKeyTemplate , SteamVR_Settings . instance . editorAppKey ) ;
112+
113+ if ( EditorPrefs . HasKey ( enabledLoaderKey ) == false )
114+ {
115+ installPackageAndAssignLoaderForBuildTarget . Invoke ( null , new object [ ] { valveOpenVRPackageString , valveOpenVRLoaderType , BuildTargetGroup . Standalone } ) ;
116+
117+ isAssigned = ( bool ) isLoaderAssigned . Invoke ( null , isLoaderAssignedCallParameters ) ;
118+ if ( isAssigned )
119+ {
120+ EditorPrefs . SetBool ( enabledLoaderKey , true ) ;
121+
122+ Debug . Log ( "<b>[SteamVR Setup]</b> Enabled OpenVR Loader in XR Management" ) ;
123+ UnityEditor . EditorApplication . update -= Update ;
124+ }
125+ }
126+ else
127+ {
128+ UnityEditor . EditorApplication . update -= Update ;
129+ }
130+
131+ }
132+ else
133+ {
134+ UnityEditor . EditorApplication . update -= Update ;
135+ }
136+ }
137+
138+ #elif UNITY_2020_1_OR_NEWER
139+ StartAutoUpdater ( ) ;
140+ #endif
141+ #else
52142 if ( UnityEditor . PlayerSettings . virtualRealitySupported == false )
53143 {
54144 UnityEditor . PlayerSettings . virtualRealitySupported = true ;
@@ -89,10 +179,12 @@ public static void Update()
89179 UnityEditorInternal . VR . VREditor . SetVREnabledDevicesOnTargetGroup ( currentTarget , newDevices ) ;
90180#endif
91181 Debug . Log ( "<b>[SteamVR Setup]</b> Added OpenVR to supported VR SDKs list." ) ;
182+
92183 }
93184
94185#if UNITY_2018_2_OR_NEWER
95186 //2018+ requires us to manually add the OpenVR package
187+ //2020.1+ has a separate script that does installs
96188
97189 switch ( packageState )
98190 {
@@ -111,12 +203,16 @@ public static void Update()
111203 break ;
112204 }
113205
114- bool hasPackage = listRequest . Result . Any ( package => package . name == openVRPackageString ) ;
206+ string packageName = null ;
207+
208+ packageName = unityOpenVRPackageString ;
209+
210+ bool hasPackage = listRequest . Result . Any ( package => package . name == packageName ) ;
115211
116212 if ( hasPackage == false )
117213 {
118214 //if we don't have the package - then install it
119- addRequest = UnityEditor . PackageManager . Client . Add ( openVRPackageString ) ;
215+ addRequest = UnityEditor . PackageManager . Client . Add ( packageName ) ;
120216 packageState = PackageStates . WaitingForAdd ;
121217 addTryCount ++ ;
122218
@@ -126,7 +222,7 @@ public static void Update()
126222 }
127223 else
128224 {
129- //if we do have the package do nothing
225+ //if we do have the package, do nothing
130226 packageState = PackageStates . Installed ; //already installed
131227 }
132228 }
@@ -180,14 +276,15 @@ public static void Update()
180276 packageState = PackageStates . Failed ;
181277 break ;
182278 }
279+ string packageName = unityOpenVRPackageString ;
183280
184- bool hasPackage = listRequest . Result . Any ( package => package . name == openVRPackageString ) ;
281+ bool hasPackage = listRequest . Result . Any ( package => package . name == packageName ) ;
185282
186283 if ( hasPackage == false )
187284 {
188285 if ( addTryCount == 1 )
189286 {
190- addRequest = UnityEditor . PackageManager . Client . Add ( openVRPackageString ) ;
287+ addRequest = UnityEditor . PackageManager . Client . Add ( packageName ) ;
191288 packageState = PackageStates . WaitingForAdd ;
192289 addTryCount ++ ;
193290
@@ -225,7 +322,38 @@ public static void Update()
225322#else
226323 UnityEditor . EditorApplication . update -= Update ;
227324#endif
325+ #endif
326+ }
327+ }
328+ private static void StartAutoUpdater ( )
329+ {
330+ Assembly [ ] assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
331+ for ( int assemblyIndex = 0 ; assemblyIndex < assemblies . Length ; assemblyIndex ++ )
332+ {
333+ Assembly assembly = assemblies [ assemblyIndex ] ;
334+ Type type = assembly . GetType ( "Unity.XR.OpenVR.OpenVRAutoUpdater" ) ;
335+ if ( type != null )
336+ {
337+ MethodInfo preinitMethodInfo = type . GetMethod ( "Start" ) ;
338+ if ( preinitMethodInfo != null )
339+ {
340+ preinitMethodInfo . Invoke ( null , null ) ;
341+ return ;
342+ }
343+ }
228344 }
229345 }
346+
347+ private static Type xrMetadataStoreType ;
348+ private static MethodInfo GetMethod ( string methodName , Type [ ] parameters = null )
349+ {
350+ if ( xrMetadataStoreType == null )
351+ xrMetadataStoreType = SteamVR_Utils . FindType ( "UnityEditor.XR.Management.Metadata.XRPackageMetadataStore" ) ;
352+
353+ if ( parameters == null )
354+ return xrMetadataStoreType . GetMethod ( methodName , BindingFlags . Static | BindingFlags . NonPublic ) ;
355+ else
356+ return xrMetadataStoreType . GetMethod ( methodName , BindingFlags . Static | BindingFlags . NonPublic , null , parameters , null ) ;
357+ }
230358 }
231359}
0 commit comments