1212using System ;
1313using System . Reflection ;
1414
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
27+
1528namespace Valve . VR
1629{
1730 [ InitializeOnLoad ]
@@ -25,8 +38,11 @@ static SteamVR_AutoEnableVR()
2538 protected const string openVRString = "OpenVR" ;
2639 protected const string unityOpenVRPackageString = "com.unity.xr.openvr.standalone" ;
2740 protected const string valveOpenVRPackageString = "com.valvesoftware.unity.openvr" ;
28- protected const string valveOpenVRPackageStringOld = "com.valve.openvr" ;
29- protected const string valveOpenVRGitURL = "https://github.com/ValveSoftware/steamvr_unity_plugin.git#UnityXRPlugin" ;
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
3046
3147#if UNITY_2018_2_OR_NEWER
3248 private enum PackageStates
@@ -46,6 +62,14 @@ private enum PackageStates
4662 private static System . Diagnostics . Stopwatch addingPackageTimeTotal = new System . Diagnostics . Stopwatch ( ) ;
4763 private static float estimatedTimeToInstall = 80 ;
4864 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 ;
4973#endif
5074
5175 public static void Update ( )
@@ -54,21 +78,73 @@ public static void Update()
5478 {
5579 bool enabledVR = false ;
5680
57- #if OPENVR_XR_API
81+ #if UNITY_2020_1_OR_NEWER || OPENVR_XR_API
82+ #if ! UNITY_2020_2_OR_NEWER
5883 if ( UnityEditor . PlayerSettings . virtualRealitySupported == true )
5984 {
6085 UnityEditor . PlayerSettings . virtualRealitySupported = false ;
6186 enabledVR = true ;
6287 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)" ) ;
6388 }
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
64141#else
65142 if ( UnityEditor . PlayerSettings . virtualRealitySupported == false )
66143 {
67144 UnityEditor . PlayerSettings . virtualRealitySupported = true ;
68145 enabledVR = true ;
69146 Debug . Log ( "<b>[SteamVR Setup]</b> Enabled virtual reality support in Player Settings. (you can disable this by unchecking Assets/SteamVR/SteamVR_Settings.autoEnableVR)" ) ;
70147 }
71- #endif
72148
73149 UnityEditor . BuildTargetGroup currentTarget = UnityEditor . EditorUserBuildSettings . selectedBuildTargetGroup ;
74150
@@ -97,21 +173,18 @@ public static void Update()
97173 newDevices = devicesList . ToArray ( ) ;
98174 }
99175
100-
101- #if OPENVR_XR_API
102- //UnityEditorInternal.VR.VREditor.SetVREnabledDevicesOnTargetGroup(currentTarget, new string[] { });
103- #else
104176#if ( UNITY_5_6 || UNITY_5_4 || UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0 )
105177 UnityEditorInternal . VR . VREditor . SetVREnabledDevices ( currentTarget , newDevices ) ;
106178#else
107179 UnityEditorInternal . VR . VREditor . SetVREnabledDevicesOnTargetGroup ( currentTarget , newDevices ) ;
108180#endif
109181 Debug . Log ( "<b>[SteamVR Setup]</b> Added OpenVR to supported VR SDKs list." ) ;
110- #endif
182+
111183 }
112184
113185#if UNITY_2018_2_OR_NEWER
114186 //2018+ requires us to manually add the OpenVR package
187+ //2020.1+ has a separate script that does installs
115188
116189 switch ( packageState )
117190 {
@@ -131,28 +204,13 @@ public static void Update()
131204 }
132205
133206 string packageName = null ;
134-
135- #if OPENVR_XR_API || UNITY_2020_1_OR_NEWER || XR_MGMT
136- if ( listRequest . Result . Any ( package => package . name == unityOpenVRPackageString ) )
137- UnityEditor . PackageManager . Client . Remove ( unityOpenVRPackageString ) ; //remove the old one or the dlls will clash
138- if ( listRequest . Result . Any ( package => package . name == valveOpenVRPackageStringOld ) )
139- UnityEditor . PackageManager . Client . Remove ( valveOpenVRPackageStringOld ) ; //remove the old one or the dlls will clash
140-
141- packageName = valveOpenVRPackageString ;
142- #else
207+
143208 packageName = unityOpenVRPackageString ;
144- #endif
145209
146210 bool hasPackage = listRequest . Result . Any ( package => package . name == packageName ) ;
147211
148212 if ( hasPackage == false )
149213 {
150- if ( packageName == valveOpenVRPackageString )
151- {
152- packageState = PackageStates . Installed ; //mark it as installed here so the npm installer can take over
153- StartAutoUpdater ( ) ;
154- }
155-
156214 //if we don't have the package - then install it
157215 addRequest = UnityEditor . PackageManager . Client . Add ( packageName ) ;
158216 packageState = PackageStates . WaitingForAdd ;
@@ -164,7 +222,7 @@ public static void Update()
164222 }
165223 else
166224 {
167- //if we do have the package do nothing
225+ //if we do have the package, do nothing
168226 packageState = PackageStates . Installed ; //already installed
169227 }
170228 }
@@ -219,9 +277,6 @@ public static void Update()
219277 break ;
220278 }
221279 string packageName = unityOpenVRPackageString ;
222- #if OPENVR_XR_API
223- packageName = valveOpenVRPackageString ;
224- #endif
225280
226281 bool hasPackage = listRequest . Result . Any ( package => package . name == packageName ) ;
227282
@@ -266,6 +321,7 @@ public static void Update()
266321 }
267322#else
268323 UnityEditor . EditorApplication . update -= Update ;
324+ #endif
269325#endif
270326 }
271327 }
@@ -287,5 +343,17 @@ private static void StartAutoUpdater()
287343 }
288344 }
289345 }
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+ }
290358 }
291359}
0 commit comments