Skip to content

Commit b24952d

Browse files
committed
2.6.0b4 - RC1 - Fixing auto unity xr plugin installation
1 parent de5f988 commit b24952d

File tree

6 files changed

+233
-157
lines changed

6 files changed

+233
-157
lines changed

Assets/SteamVR/Editor/SteamVR_AutoEnableVR.cs

Lines changed: 97 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
using System;
1313
using 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+
1528
namespace 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
}

Assets/SteamVR/Editor/SteamVR_Editor.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "SteamVR_Editor",
33
"references": [
44
"SteamVR",
5-
"Unity.XR.OpenVR"
5+
"Unity.XR.OpenVR",
6+
"Unity.XR.Management.Editor",
7+
"Unity.XR.Management"
68
],
79
"includePlatforms": [
810
"Editor"

Assets/SteamVR/Input/SteamVR_Input.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static bool isStartupFrame
5050
{
5151
get
5252
{
53-
return Time.frameCount >= (startupFrame-1) && Time.frameCount <= (startupFrame+1);
53+
return Time.frameCount >= (startupFrame - 1) && Time.frameCount <= (startupFrame + 1);
5454
}
5555
}
5656

@@ -1627,7 +1627,7 @@ private static void CheckSetup()
16271627
{
16281628
openingSetup = true;
16291629
UnityEditor.EditorApplication.isPlaying = false;
1630-
Type editorWindowType = FindType("Valve.VR.SteamVR_Input_EditorWindow");
1630+
Type editorWindowType = SteamVR_Utils.FindType("Valve.VR.SteamVR_Input_EditorWindow");
16311631
if (editorWindowType != null)
16321632
{
16331633
var window = UnityEditor.EditorWindow.GetWindow(editorWindowType, false, "SteamVR Input", true);
@@ -1647,19 +1647,6 @@ public static string GetEditorAppKey()
16471647
{
16481648
return SteamVR_Settings.instance.editorAppKey;
16491649
}
1650-
1651-
private static Type FindType(string typeName)
1652-
{
1653-
var type = Type.GetType(typeName);
1654-
if (type != null) return type;
1655-
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
1656-
{
1657-
type = a.GetType(typeName);
1658-
if (type != null)
1659-
return type;
1660-
}
1661-
return null;
1662-
}
16631650
#endif
16641651
}
16651652
}

Assets/SteamVR/Plugins/openvr_api.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7465,6 +7465,7 @@ public static uint GetInitToken()
74657465
return OpenVRInterop.GetInitToken();
74667466
}
74677467

7468+
public const ulong k_ulSharedTextureIsNTHandle = 4294967296;
74687469
public const uint k_nDriverNone = 4294967295;
74697470
public const uint k_unMaxDriverDebugResponseSize = 32768;
74707471
public const uint k_unTrackedDeviceIndex_Hmd = 0;
@@ -7773,6 +7774,7 @@ public static uint GetInitToken()
77737774
public const string k_pchPathBoundTrackerRole = "/bound_tracker_role";
77747775
public const string k_pchPathPoseRaw = "/pose/raw";
77757776
public const string k_pchPathPoseTip = "/pose/tip";
7777+
public const string k_pchPathPoseGrip = "/pose/grip";
77767778
public const string k_pchPathSystemButtonClick = "/input/system/click";
77777779
public const string k_pchPathProximity = "/proximity";
77787780
public const string k_pchPathControllerTypePrefix = "/controller_type/";

0 commit comments

Comments
 (0)