Skip to content

Commit 8fdf63c

Browse files
author
Christopher-Marcel Böddecker
committed
feat(SDKManager): simplify single SDK Setup configurations
Supporting multiple SDKs means VRTK had to disable all the SDK Setups that are used automatically to ensure they're usable for runtime switching. This change allows to completely bypass the runtime loading and that requirement in case only one SDK Setup is used. This means users of VRTK don't have to bother with handling when an SDK Setup is loaded and usable, nor do they have to re-enable their SDK Setups game objects again to work on them in the editor while they're visible. The SteamVR SDK Setup in the SDKSetups prefab is changed to allow for this new behavior. This also fixes a bug that resulted in invalid matrix errors being logged to the console by Unity.
1 parent 90ddff1 commit 8fdf63c

File tree

5 files changed

+92
-91
lines changed

5 files changed

+92
-91
lines changed

Assets/VRTK/Editor/VRTK_SDKSetupEditor.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -432,37 +432,39 @@ private static string[] OnWillSaveAssets(string[] paths)
432432

433433
private static void FixOpenAndUnsavedScenes()
434434
{
435-
for (int index = 0; index < EditorSceneManager.loadedSceneCount; index++)
435+
436+
List<VRTK_SDKSetup> setups = Enumerable.Range(0, EditorSceneManager.loadedSceneCount)
437+
.SelectMany(sceneIndex => SceneManager.GetSceneAt(sceneIndex).GetRootGameObjects())
438+
.SelectMany(rootObject => rootObject.GetComponentsInChildren<VRTK_SDKManager>())
439+
.Select(manager => manager.setups.Where(setup => setup != null).ToArray())
440+
.Where(sdkSetups => sdkSetups.Length > 1)
441+
.SelectMany(sdkSetups => sdkSetups)
442+
.Where(setup => setup.gameObject.activeSelf)
443+
.ToList();
444+
if (setups.Count == 0)
436445
{
437-
Scene scene = SceneManager.GetSceneAt(index);
438-
if (scene.isLoaded && scene.isDirty)
439-
{
440-
VRTK_SDKSetup[] setups = FindObjectsOfType<VRTK_SDKSetup>();
441-
foreach (VRTK_SDKSetup setup in setups)
442-
{
443-
setup.gameObject.SetActive(false);
444-
}
446+
return;
447+
}
445448

446-
if (setups.Length != 0)
447-
{
448-
string infoMessage = string.Format("The following game objects have been set inactive to allow for SDK loading and switching using the SDK Setups on them:\n{0}", string.Join(", ", setups.Select(setup => setup.name).ToArray()));
449-
if (EditorApplication.isPlayingOrWillChangePlaymode)
450-
{
449+
setups.ForEach(setup => setup.gameObject.SetActive(false));
450+
451+
string infoMessage = string.Format(
452+
"The following game objects have been set inactive to allow for SDK loading and switching using the SDK Setups on them:\n{0}",
453+
string.Join(", ", setups.Select(setup => setup.name).ToArray()));
454+
if (EditorApplication.isPlayingOrWillChangePlaymode)
455+
{
451456
#if UNITY_5_6_OR_NEWER
452-
SessionState.SetString(
457+
SessionState.SetString(
453458
#else
454-
EditorPrefs.SetString(
459+
EditorPrefs.SetString(
455460
#endif
456-
PreferencesKey,
457-
infoMessage
458-
);
459-
}
460-
else
461-
{
462-
VRTK_Logger.Info(infoMessage);
463-
}
464-
}
465-
}
461+
PreferencesKey,
462+
infoMessage
463+
);
464+
}
465+
else
466+
{
467+
VRTK_Logger.Info(infoMessage);
466468
}
467469
}
468470
}

Assets/VRTK/Examples/ExampleResources/Prefabs/SDKSetups.prefab

Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -809,9 +809,7 @@ GameObject:
809809
serializedVersion: 5
810810
m_Component:
811811
- component: {fileID: 4000010242415096}
812-
- component: {fileID: 20000012316479598}
813812
- component: {fileID: 114000011243730024}
814-
- component: {fileID: 92000014137301068}
815813
m_Layer: 0
816814
m_Name: Camera (head)
817815
m_TagString: MainCamera
@@ -1578,7 +1576,8 @@ GameObject:
15781576
- component: {fileID: 4000012660741642}
15791577
- component: {fileID: 20000011176388890}
15801578
- component: {fileID: 124000012904963108}
1581-
- component: {fileID: 114000012849300506}
1579+
- component: {fileID: 92569260401678672}
1580+
- component: {fileID: 114085069478805808}
15821581
m_Layer: 0
15831582
m_Name: Camera (eye)
15841583
m_TagString: MainCamera
@@ -3427,40 +3426,6 @@ Camera:
34273426
m_StereoConvergence: 10
34283427
m_StereoSeparation: 0.022
34293428
m_StereoMirrorMode: 0
3430-
--- !u!20 &20000012316479598
3431-
Camera:
3432-
m_ObjectHideFlags: 1
3433-
m_PrefabParentObject: {fileID: 0}
3434-
m_PrefabInternal: {fileID: 100100000}
3435-
m_GameObject: {fileID: 1000011840228368}
3436-
m_Enabled: 1
3437-
serializedVersion: 2
3438-
m_ClearFlags: 4
3439-
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0.019607844}
3440-
m_NormalizedViewPortRect:
3441-
serializedVersion: 2
3442-
x: 0
3443-
y: 0
3444-
width: 1
3445-
height: 1
3446-
near clip plane: 0
3447-
far clip plane: 1
3448-
field of view: 60
3449-
orthographic: 1
3450-
orthographic size: 1
3451-
m_Depth: 0
3452-
m_CullingMask:
3453-
serializedVersion: 2
3454-
m_Bits: 0
3455-
m_RenderingPath: -1
3456-
m_TargetTexture: {fileID: 0}
3457-
m_TargetDisplay: 0
3458-
m_TargetEye: 3
3459-
m_HDR: 0
3460-
m_OcclusionCulling: 0
3461-
m_StereoConvergence: 10
3462-
m_StereoSeparation: 0.022
3463-
m_StereoMirrorMode: 0
34643429
--- !u!20 &20000013040785452
34653430
Camera:
34663431
m_ObjectHideFlags: 1
@@ -5053,12 +5018,12 @@ Behaviour:
50535018
m_PrefabInternal: {fileID: 100100000}
50545019
m_GameObject: {fileID: 1000010426818160}
50555020
m_Enabled: 1
5056-
--- !u!92 &92000014137301068
5021+
--- !u!92 &92569260401678672
50575022
Behaviour:
50585023
m_ObjectHideFlags: 1
50595024
m_PrefabParentObject: {fileID: 0}
50605025
m_PrefabInternal: {fileID: 100100000}
5061-
m_GameObject: {fileID: 1000011840228368}
5026+
m_GameObject: {fileID: 1000014132296520}
50625027
m_Enabled: 1
50635028
--- !u!92 &92889010644976706
50645029
Behaviour:
@@ -5964,20 +5929,6 @@ MonoBehaviour:
59645929
m_EffectColor: {r: 0, g: 0, b: 0, a: 0.09803922}
59655930
m_EffectDistance: {x: 1, y: 1}
59665931
m_UseGraphicAlpha: 0
5967-
--- !u!114 &114000012849300506
5968-
MonoBehaviour:
5969-
m_ObjectHideFlags: 1
5970-
m_PrefabParentObject: {fileID: 0}
5971-
m_PrefabInternal: {fileID: 100100000}
5972-
m_GameObject: {fileID: 1000014132296520}
5973-
m_Enabled: 1
5974-
m_EditorHideFlags: 0
5975-
m_Script: {fileID: 11500000, guid: 6bca9ccf900ccc84c887d783321d27e2, type: 3}
5976-
m_Name:
5977-
m_EditorClassIdentifier:
5978-
_head: {fileID: 4000010242415096}
5979-
_ears: {fileID: 4000013003015800}
5980-
wireframe: 0
59815932
--- !u!114 &114000013231417920
59825933
MonoBehaviour:
59835934
m_ObjectHideFlags: 1
@@ -6426,6 +6377,20 @@ MonoBehaviour:
64266377
m_PreferredHeight: -1
64276378
m_FlexibleWidth: -1
64286379
m_FlexibleHeight: -1
6380+
--- !u!114 &114085069478805808
6381+
MonoBehaviour:
6382+
m_ObjectHideFlags: 1
6383+
m_PrefabParentObject: {fileID: 0}
6384+
m_PrefabInternal: {fileID: 100100000}
6385+
m_GameObject: {fileID: 1000014132296520}
6386+
m_Enabled: 1
6387+
m_EditorHideFlags: 0
6388+
m_Script: {fileID: 11500000, guid: 6bca9ccf900ccc84c887d783321d27e2, type: 3}
6389+
m_Name:
6390+
m_EditorClassIdentifier:
6391+
_head: {fileID: 4000010242415096}
6392+
_ears: {fileID: 4000013003015800}
6393+
wireframe: 0
64296394
--- !u!114 &114139617658199918
64306395
MonoBehaviour:
64316396
m_ObjectHideFlags: 1

Assets/VRTK/SDK/Simulator/SDK_SimController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,14 +540,14 @@ protected virtual bool IsButtonPressed(uint index, ButtonPressTypes type, KeyCod
540540

541541
if (index == 1)
542542
{
543-
if (!rightController.Selected)
543+
if (rightController == null || !rightController.Selected)
544544
{
545545
return false;
546546
}
547547
}
548548
else if (index == 2)
549549
{
550-
if (!leftController.Selected)
550+
if (leftController == null || !leftController.Selected)
551551
{
552552
return false;
553553
}

Assets/VRTK/Scripts/Utilities/SDK/VRTK_SDKManager.cs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,20 @@ public static VRTK_SDKManager instance
178178
/// <summary>
179179
/// The loaded SDK Setup. <see langword="null"/> if no setup is currently loaded.
180180
/// </summary>
181-
public VRTK_SDKSetup loadedSetup { get; private set; }
181+
public VRTK_SDKSetup loadedSetup
182+
{
183+
get
184+
{
185+
if (_loadedSetup == null && setups.Length == 1 && setups[0].isValid && setups[0].isActiveAndEnabled)
186+
{
187+
_loadedSetup = setups[0];
188+
}
189+
190+
return _loadedSetup;
191+
}
192+
private set { _loadedSetup = value; }
193+
}
194+
private VRTK_SDKSetup _loadedSetup;
182195
private static HashSet<VRTK_SDKInfo> _previouslyUsedSetupInfos = new HashSet<VRTK_SDKInfo>();
183196

184197
/// <summary>
@@ -332,28 +345,46 @@ public void ManageVRSettings(bool force)
332345
.ToDictionary(grouping => grouping.Key,
333346
grouping => grouping.Select(info => info.description.vrDeviceName)
334347
.Distinct()
335-
.Except(new[] { "None" })
336348
.ToArray());
337349

338350
foreach (BuildTargetGroup targetGroup in VRTK_SharedMethods.GetValidBuildTargetGroups())
339351
{
340352
string[] deviceNames;
341353
deviceNamesByTargetGroup.TryGetValue(targetGroup, out deviceNames);
354+
355+
int setupCount = deviceNames == null ? 0 : deviceNames.Length;
342356
bool vrEnabled = deviceNames != null && deviceNames.Length > 0;
343357

358+
if (deviceNames != null)
359+
{
360+
deviceNames = deviceNames.Except(new[] { "None" }).ToArray();
361+
}
362+
344363
#if UNITY_5_5_OR_NEWER
345364
VREditor.SetVREnabledOnTargetGroup(targetGroup, vrEnabled);
346365
#else
347366
VREditor.SetVREnabled(targetGroup, vrEnabled);
348367
#endif
349368

369+
string[] devices;
370+
if (vrEnabled)
371+
{
372+
devices = setupCount > 1
373+
? new[] { "None" }.Concat(deviceNames).ToArray()
374+
: deviceNames;
375+
}
376+
else
377+
{
378+
devices = new string[0];
379+
}
380+
350381
#if UNITY_5_5_OR_NEWER
351382
VREditor.SetVREnabledDevicesOnTargetGroup(
352383
#else
353384
VREditor.SetVREnabledDevices(
354385
#endif
355386
targetGroup,
356-
vrEnabled ? new[] { "None" }.Concat(deviceNames).ToArray() : new string[0]
387+
devices
357388
);
358389
}
359390
}
@@ -483,18 +514,18 @@ public void TryLoadSDKSetup(int startIndex, bool tryToReinitialize, params VRTK_
483514
previousLoadedSetup.OnUnloaded(this);
484515
}
485516

486-
bool isDeviceAlreadyLoaded = VRSettings.enabled
487-
&& sdkSetups[0].usedVRDeviceNames.Contains(VRSettings.loadedDeviceName);
517+
string loadedDeviceName = string.IsNullOrEmpty(VRSettings.loadedDeviceName) ? "None" : VRSettings.loadedDeviceName;
518+
bool isDeviceAlreadyLoaded = sdkSetups[0].usedVRDeviceNames.Contains(loadedDeviceName);
488519
if (!isDeviceAlreadyLoaded)
489520
{
490-
if (!tryToReinitialize && !VRSettings.enabled && !string.IsNullOrEmpty(VRSettings.loadedDeviceName))
521+
if (!tryToReinitialize && !VRSettings.enabled && !string.IsNullOrEmpty(loadedDeviceName))
491522
{
492-
sdkSetups = sdkSetups.Where(setup => !setup.usedVRDeviceNames.Contains(VRSettings.loadedDeviceName))
523+
sdkSetups = sdkSetups.Where(setup => !setup.usedVRDeviceNames.Contains(loadedDeviceName))
493524
.ToArray();
494525
}
495526

496527
VRTK_SDKSetup[] missingVRDeviceSetups = sdkSetups
497-
.Where(setup => setup.usedVRDeviceNames.Except(VRSettings.supportedDevices).Any())
528+
.Where(setup => setup.usedVRDeviceNames.Except(VRSettings.supportedDevices.Concat(new[] { "None" })).Any())
498529
.ToArray();
499530
foreach (VRTK_SDKSetup missingVRDeviceSetup in missingVRDeviceSetups)
500531
{
@@ -603,7 +634,7 @@ private void OnEnable()
603634

604635
CreateInstance();
605636

606-
if (autoLoadSetup)
637+
if (loadedSetup == null && autoLoadSetup)
607638
{
608639
TryLoadSDKSetupFromList();
609640
}

Assets/VRTK/Scripts/Utilities/SDK/VRTK_SDKSetupSwitcher.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace VRTK
66
using UnityEngine.UI;
77
using System;
88
using System.Collections.Generic;
9+
using System.Linq;
910

1011
/// <summary>
1112
/// The SDK Setup Switcher adds a GUI overlay to allow switching the loaded VRTK_SDKSetup of the the current VRTK_SDKManager.
@@ -112,7 +113,9 @@ protected virtual void Show(ViewingState viewingState)
112113
return;
113114
}
114115

115-
fallbackCamera.gameObject.SetActive(VRTK_DeviceFinder.HeadsetCamera() == null);
116+
bool isAnyOtherCameraUsed = sdkManager.setups.Any(setup => setup != null && setup.gameObject.activeSelf)
117+
|| VRTK_DeviceFinder.HeadsetCamera() != null;
118+
fallbackCamera.gameObject.SetActive(!isAnyOtherCameraUsed);
116119
eventSystem.gameObject.SetActive(EventSystem.current == null || EventSystem.current == eventSystem);
117120
}
118121

0 commit comments

Comments
 (0)